forked from OpenGenus/quark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent-frame.js
88 lines (72 loc) · 2.51 KB
/
content-frame.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"use strict";
frameScript();
function frameScript()
{
if (document.readyState != "loading") onLoadPage();
else
{
window.addEventListener("load",
function(event)
{
if (document.readyState != "loading") onLoadPage();
},false);
}
function onLoadPage()
{
addListeners();
}
function addListeners()
{
chrome.runtime.onMessage.addListener(
function(message,sender,sendResponse)
{
var doctype,htmltext;
switch (message.type)
{
case "requestCrossFrames":
if (document.defaultView.frameElement == null)
{
nameCrossFrames(0,window,document.documentElement);
if (window.name != "")
{
doctype = document.doctype;
if (doctype != null)
{
htmltext = '<!DOCTYPE ' + doctype.name + (doctype.publicId ? ' PUBLIC "' + doctype.publicId + '"' : '') +
((doctype.systemId && !doctype.publicId) ? ' SYSTEM' : '') + (doctype.systemId ? ' "' + doctype.systemId + '"' : '') + '>';
}
else htmltext = "";
htmltext += document.documentElement.outerHTML;
htmltext = htmltext.replace(/<head([^>]*)>/,"<head$1><base href=\"" + document.baseURI + "\">");
chrome.runtime.sendMessage({ type: "replyCrossFrame", name: window.name, url: document.baseURI, html: htmltext });
}
}
break;
}
});
}
function nameCrossFrames(depth,frame,element)
{
var i;
if (element.localName == "iframe" || element.localName == "frame")
{
try
{
if (element.contentDocument.documentElement != null)
{
nameCrossFrames(depth+1,element.contentWindow,element.contentDocument.documentElement);
}
}
catch (e)
{
if (!element.name) element.setAttribute("name","savepage-frame-" + Math.trunc(Math.random()*100000000));
}
}
else
{
for (i = 0; i < element.children.length; i++)
if (element.children[i] != null)
nameCrossFrames(depth,frame,element.children[i]);
}
}
}