I’m trying to execute custom JS code from a Firefox extension using:
function executeJS(document, script) {
var script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.appendChild(document.createTextNode(script));
document.getElementsByTagName('head')[0].appendChild(script);
}
The method call looks like:
executeJS(content.document, "$('#" + this.id + "').jixedbar({showOnTop:true});");
And this is the result that I get:
<script type="text/javascript">
[object XPCNativeWrapper [object HTMLScriptElement]]
</script>
What’s wrong with my code?
What’s the proper way of execution arbitrary JS script from Firefox extension?
I’m not sure about FF extensions, but in “normal” JS-land, there’s no need for the
createTextNodebusiness. Outside of FF extensions, you can useNode.textContent— though maybe it’s different with theXPCNativeWrappertypes.I think the main problem, however, is that you’ve also got a variable and a parameter both named
script. Try this:The
typeattribute really is not necessary, BTW.I just came across this page, which looks like it could be what you’re looking for: