2014-05-04 08:44:18 +02:00
|
|
|
|
module.exports = function(el) {
|
|
|
|
|
|
var code = (el.text || el.textContent || el.innerHTML || "")
|
2018-02-02 09:52:44 -05:00
|
|
|
|
var src = (el.src || "")
|
2017-09-18 14:13:39 +02:00
|
|
|
|
var parent = el.parentNode || document.querySelector("head") || document.documentElement
|
2014-05-04 08:44:18 +02:00
|
|
|
|
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"
|
2017-11-02 12:40:30 +01:00
|
|
|
|
|
2018-03-20 18:43:34 -04:00
|
|
|
|
/* istanbul ignore if */
|
2018-02-02 09:52:44 -05:00
|
|
|
|
if (src !== "") {
|
|
|
|
|
|
script.src = src
|
|
|
|
|
|
script.async = false // force synchronous loading of peripheral JS
|
2014-05-04 08:44:18 +02:00
|
|
|
|
}
|
2017-11-02 12:40:30 +01:00
|
|
|
|
|
2018-02-02 09:52:44 -05:00
|
|
|
|
if (code !== "") {
|
2017-11-02 12:40:30 +01:00
|
|
|
|
try {
|
|
|
|
|
|
script.appendChild(document.createTextNode(code))
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (e) {
|
2018-03-20 18:43:34 -04:00
|
|
|
|
/* istanbul ignore next */
|
2017-11-02 12:40:30 +01:00
|
|
|
|
// old IEs have funky script nodes
|
|
|
|
|
|
script.text = code
|
|
|
|
|
|
}
|
2014-05-04 08:44:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// execute
|
2018-02-02 09:52:44 -05:00
|
|
|
|
parent.appendChild(script)
|
2017-09-18 14:13:39 +02:00
|
|
|
|
// avoid pollution only in head or body tags
|
2018-03-20 18:42:39 -04:00
|
|
|
|
if (parent instanceof HTMLHeadElement || parent instanceof HTMLBodyElement) {
|
2017-09-18 14:13:39 +02:00
|
|
|
|
parent.removeChild(script)
|
|
|
|
|
|
}
|
2014-05-04 08:44:18 +02:00
|
|
|
|
|
2018-02-02 09:52:44 -05:00
|
|
|
|
return true
|
2014-05-04 08:44:18 +02:00
|
|
|
|
}
|