2011-06-05 7 views

cevap

3

FF uzantıları hakkında emin değilim, ancak "normal" JS arazisinde, createTextNode işine gerek yoktur. FF uzantılarının dışında Node.textContent —'u kullanabilirsiniz, ancak XPCNativeWrapper türlerinden farklı olabilir.

script.textContent = 'var foo = 1; alert(foo);' 

Ben ana sorun, ancak, aynı zamanda bir değişken ve parametre script adında hem var olduğunu düşünüyorum. Bu deneyin:

function executeJS(document, scriptContent) { 
    var script = document.createElement('script'); 
    script.appendChild(document.createTextNode(scriptContent)); 
    document.head.appendChild(script); 
} 

type nitelik gerçekten BTW, gerekli değildir.

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"); 
+0

Harikasın:


Sadece aradığınız şey olabilir gibi görünüyor, hangi this page geldi! Değişken adlandırma sorun oldu ... – spektom

İlgili konular