从Firefox扩展中执行JS [英] Execute JS from Firefox extension

查看:215
本文介绍了从Firefox扩展中执行JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 函数executeJS(document,script){ 
var script = document.createElement('script');
script.setAttribute('type','text / javascript');
script.appendChild(document.createTextNode(script));
document.getElementsByTagName('head')[0] .appendChild(script);





方法调用如下所示:

  executeJS(content.document,$('#+ this.id +').jixedbar({showOnTop:true});); 

这就是我得到的结果:

 < script type =text / javascript> 
[object XPCNativeWrapper [object HTMLScriptElement]]
< / script>

我的代码有什么问题?
从Firefox扩展执行任意JS脚本的正确方法是什么?

解决方案

我不确定FF扩展,但是在普通的JS-land中,不需要 createTextNode 业务。在FF扩展之外,您可以使用 Node.textContent —不过也许和 XPCNativeWrapper 类型不一样。

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

我认为 main 是你也有一个变量和一个名为 script 的参数。试试这个:

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

类型属性真的没有必要,顺便说一句。




我刚刚遇到此页面,看起来可能是您要找的内容:

  const XUL = Namespace(xul,http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul); 
$ b函数injectScript(name){
//获取当前文件名
let file = Components.stack.filename;
//删除由子脚本加载器
//和尾部文件名
添加的任何前缀let directory = file.replace(/.* - > | [^ \ / ] + $ / g,);

//创建脚本节点
let script = document.createElementNS(XUL,script);
script.setAttribute(type,application / javascript; version = 1.8);
script.setAttribute(src,directory + name);

//将其注入文档的顶层元素
document.documentElement.appendChild(script);
}

//注入脚本
injectScript(script.js);


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 createTextNode business. Outside of FF extensions, you can use Node.textContent — though maybe it's different with the XPCNativeWrapper types.

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

I think the main problem, however, is that you've also got a variable and a parameter both named script. Try this:

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

The type attribute really is not necessary, BTW.


I just came across this page, which looks like it could be what you're looking for:

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

这篇关于从Firefox扩展中执行JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆