49 lines
1.2 KiB
JavaScript
49 lines
1.2 KiB
JavaScript
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;
|
||
};
|