【发布时间】:2014-06-04 19:13:37
【问题描述】:
我正在创建一个 Firefox 扩展程序,我希望它的一个功能是用户能够将脚本或样式表注入特定网站,就像 Greasemonkey 一样(除了这仅适用于一个网站) .我正在为要使用的脚本添加一些功能,我打算将这些功能从内容脚本添加到主(不安全)窗口中。在MDN blog 上,它说他们已经改变了它的实现方式,所以我按照帖子中的建议将我的代码基于新的实现,所以这就是我所拥有的:
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");
但是脚本的执行刚刚停止,并且在控制台中打印Message: TypeError: window is null。
我主要在 Firefox 28 中进行测试(我现在似乎无法让 Firefox for Ubuntu 进行更新,而且一大堆问题迫使我为此在 VM 中使用 Ubuntu),但在 Nightly 31a1(Win7 ) 没有注入任何东西,包括硬编码样式(适用于 FF28),所以我必须在某个时候弄清楚这一点。 (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.
如果有人感兴趣)
编辑:我现在已经在 Firefox 30b 上尝试过,但仍然存在很多问题,尽管它们似乎与 FF28 和 31 略有不同...
【问题讨论】:
-
在玩这些 api 时请使用 Firefox Beta 30。它们非常新,直到 30 点才完全安顿下来:mozilla.org/en-US/firefox/channel/#beta
标签: javascript firefox-addon firefox-addon-sdk content-script