-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinjector.js
More file actions
91 lines (69 loc) · 2.63 KB
/
injector.js
File metadata and controls
91 lines (69 loc) · 2.63 KB
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
89
90
91
var injector = {}
function injectScript(source)
{
var isFunction = function (arg) {
return (Object.prototype.toString.call(arg) == "[object Function]");
};
var jsEscape = function (str) {
if (!str || !str.length) return str;
var r = /['"<>\/]/g, result = "", l = 0, c;
do{ c = r.exec(str);
result += (c ? (str.substring(l, r.lastIndex-1) + "\\x" +
c[0].charCodeAt(0).toString(16)) : (str.substring(l)));
} while (c && ((l = r.lastIndex) > 0))
return (result.length ? result : str);
};
var bFunction = isFunction(source);
var elem = document.createElement("script");
var script, ret, id = "";
if (bFunction)
{
var args = [];
for (var i = 1; i < arguments.length; i++)
{
var raw = arguments[i];
var arg;
if (isFunction(raw))
arg = "eval(\"" + jsEscape("(" + raw.toString() + ")") + "\")";
else if (Object.prototype.toString.call(raw) == '[object Date]')
arg = "(new Date(" + raw.getTime().toString() + "))";
else if (Object.prototype.toString.call(raw) == '[object RegExp]')
arg = "(new RegExp(" + raw.toString() + "))";
else if (typeof raw === 'string' || typeof raw === 'object')
arg = "JSON.parse(\"" + jsEscape(JSON.stringify(raw)) + "\")";
else
arg = raw.toString();
args.push(arg);
}
while (id.length < 16) id += String.fromCharCode(((!id.length || Math.random() > 0.5) ?
0x61 + Math.floor(Math.random() * 0x19) : 0x30 + Math.floor(Math.random() * 0x9 )));
script = "(function(){var value={callResult: null, throwValue: false};try{value.callResult=(("+
source.toString()+")("+args.join()+"));}catch(e){value.throwValue=true;value.callResult=e;};"+
"document.getElementById('"+id+"').innerText=JSON.stringify(value);})();";
elem.id = id;
}
else
{
script = source;
}
elem.type = "text/javascript";
elem.innerHTML = script;
document.head.appendChild(elem);
if (bFunction)
{
ret = JSON.parse(elem.innerText);
elem.parentNode.removeChild(elem);
delete (elem);
if (ret.throwValue)
throw (ret.callResult);
else
return (ret.callResult);
}
else
return (elem);
}
injector.onLoad = function()
{
injectScript("");
}
injector.onLoad();