Я не уверен насчет расширений FF, но в «обычной» JS-стране нет необходимости в createTextNode
бизнесе.Помимо расширений FF, вы можете использовать Node.textContent
- хотя, возможно, он отличается от типов XPCNativeWrapper
.
script.textContent = 'var foo = 1; alert(foo);'
Я думаю, что проблема main ,однако, вы также получили переменную и параметр с именем script
.Попробуйте это:
function executeJS(document, scriptContent) {
var script = document.createElement('script');
script.appendChild(document.createTextNode(scriptContent));
document.head.appendChild(script);
}
Атрибут type
действительно не нужен, кстати.
Я только что натолкнулся на эту страницу , которая выглядит такможет быть то, что вы ищете:
const XUL = Namespace("xul", "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul");
function injectScript(name) {
// Get the current filename
let file = Components.stack.filename;
// Strip off any prefixes added by the sub-script loader
// and the trailing filename
let directory = file.replace(/.* -> |[^\/]+$/g, "");
// Create the script node
let script = document.createElementNS(XUL, "script");
script.setAttribute("type", "application/javascript;version=1.8");
script.setAttribute("src", directory + name);
// Inject it into the top-level element of the document
document.documentElement.appendChild(script);
}
// Inject the script
injectScript("script.js");