简体   繁体   English

使用内容脚本定义全局变量

[英]Use content script to define global variables

I am creating a Firefox extension, and one feature of it that I would like is the ability for the user to inject a script or stylesheet into a specific website, rather like Greasemonkey (except that this will only be for one site). 我正在创建一个Firefox扩展,我希望它的一个功能是用户能够将脚本或样式表注入到特定的网站中,而不是像Greasemonkey一样(除了仅用于一个站点)。 I am adding some functions for the scripts to make use of, which I intended to add from the Content Script into the main (unsafe) window. 我正在为脚本添加一些功能,这些功能我打算从内容脚本添加到主(不安全)窗口中。 On the MDN blog , it says that they have made changes to how it should be implemented, so I have based my code on the new implementation as advised in the post, so this is what I have: MDN博客上 ,它说他们已经更改了实施方式,因此我按照帖子中的建议将代码基于新的实施方式,因此,我拥有以下内容:

var $jq = jQuery.noConflict();//Yes, I am also injecting jQuery at the same time
console.log("created jquery object"); //This works
exportFunction($jq, unsafeWindow, {defineAs: "$jq"});
console.log("This will never be called");

But execution of the script just stops, and in the console it prints Message: TypeError: window is null . 但是脚本的执行只是停止了,并且在控制台中显示了Message: TypeError: window is null I am testing in Firefox 28 predominantly (I can't seem to get Firefox for Ubuntu to update beyond that right now, and a whole load of issues are forcing me to use Ubuntu in a VM for this), but in Nightly 31a1 (Win7) nothing is ever injected, including a hardcoded style (that works on FF28) so I will have to figure that out at some point. 我主要在Firefox 28中进行测试(我现在似乎无法让Firefox for Ubuntu进行更新,并且大量的问题迫使我为此目的在VM中使用Ubuntu),但是在Nightly 31a1(Win7)中)不会注入任何东西,包括硬编码样式(可在FF28上使用),因此我必须在某个时候弄清楚这一点。 (The PageMod code is here: (PageMod代码在这里:

var lttWorker = sdk.pageMod.PageMod({
    include:["*"],
    /*contentScriptFile: [sdk.data.url("jquery.large.js"), sdk.data.url("scripts/bootstrapper.js")],
    contentScriptWhen: "ready",*/ //This is commented to test whether it was an issue with the script. It's not.
    contentStyle: "#header_bar{background-color:green;}", //This is injected in FF28 but not 31
    attachTo: ["existing", "top"],
    onAttach: function(){desktopNotifications({title:"attached content worker", text:"The content worker has been successfully attached"})} //This is called in FF28 but not 31
});
lttWorker.on("error", function(){callError("pageWorker failed");}); //This never gets called. Ever.

if anybody is interested) 如果有人有兴趣)

EDIT: I have now tried it on Firefox 30b and there are still a load of issues, although they seem to be slightly different to both FF28 and 31... 编辑:我现在已经在Firefox 30b上尝试过,仍然有很多问题,尽管它们似乎与FF28和31略有不同...

First of all: These new functions are supported in Firefox 30 and later. 首先:Firefox 30及更高版本支持这些新功能。 See @canuckistani answer. 参见@canuckistani答案。

The exportFunction API is way too limited to actually inject something like jQuery with all the complex objects being or containing DOM nodes. exportFunction API太局限了,无法实际注入jQuery之类的东西,而所有复杂对象都是DOM节点或包含DOM节点。 That simply won't fly with the structured-clone algorithm that is applied to arguments. 应用于参数的结构化克隆算法根本无法实现。 The API is meant as a way for add-ons to communicate with pages bi-directionally, and not to inject complex libraries. 该API的目的是使附加组件与页面进行双向通信,而不注入复杂的库。

Your best bet is actually creating a script tag using the DOM APIs and putting jQuery there. 最好的选择实际上是使用DOM API创建脚本标签并将jQuery放在那。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM