-
-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy patheval-script.js
48 lines (42 loc) · 1.15 KB
/
eval-script.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
module.exports = function(el) {
var code = el.text || el.textContent || el.innerHTML || "";
var src = el.src || "";
var parent =
el.parentNode || document.querySelector("head") || document.documentElement;
var script = document.createElement("script");
if (code.match("document.write")) {
if (console && console.log) {
console.log(
"Script contains document.write. Can’t be executed correctly. Code skipped ",
el
);
}
return false;
}
script.type = "text/javascript";
script.id = el.id;
/* istanbul ignore if */
if (src !== "") {
script.src = src;
script.async = false; // force synchronous loading of peripheral JS
}
if (code !== "") {
try {
script.appendChild(document.createTextNode(code));
} catch (e) {
/* istanbul ignore next */
// old IEs have funky script nodes
script.text = code;
}
}
// execute
parent.appendChild(script);
// avoid pollution only in head or body tags
if (
(parent instanceof HTMLHeadElement || parent instanceof HTMLBodyElement) &&
parent.contains(script)
) {
parent.removeChild(script);
}
return true;
};