简体   繁体   English

在不带SDK的Firefox扩展中导入内容脚本

[英]Import a content script in a firefox extension without sdk

I'm trying to develop a restartless firefox extension without the sdk, and I would like to be able to manipulate the DOM of the page, but either the document, content.document or unsafeWindow.document are returning undefined. 我正在尝试开发没有sdk的无重启firefox扩展,并且我希望能够处理页面的DOM,但是文档,content.document或unsafeWindow.document都返回未定义。

My bootstrap.js code: 我的bootstrap.js代码:

Components.utils.import("resource://gre/modules/Services.jsm");
function startup(data,reason) {
    Components.utils.import("chrome://myextension/content/plugin-min.js");
}
function shutdown(data,reason) {
    Components.utils.unload("chrome://myextension/content/plugin-min.js");
}
function install(data,reason) { }
function uninstall(data,reason) { }

and my plugin-min.js code: 和我的plugin-min.js代码:

document.addEventListener('keydown',activate); // document undefined
content.document.addEventListener('keydown',activate); // content undefined
unsafeWindow.document.addEventListener('keydown',activate); // unsafeWindow undefined

And Mozilla published solution only for SDK users, and google searches I've done brought only these SDKs solutions. Mozilla仅针对SDK用户发布了解决方案,而我所做的Google搜索仅带来了这些SDK解决方案。 :/ :/

But in my case, Does anyone know what I'm missing? 但就我而言,有谁知道我在想什么?

Thank you very much. 非常感谢你。

Main extension script has no direct access to document. 主扩展脚本无法直接访问文档。 You should inject to document own content-script, and exchange with it via async messaging (port object). 您应该注入文档自己的内容脚本,并通过异步消息传递(端口对象)与其进行交换。

Also, you can provide an array of context script -in that case all of them will be imported: 另外,您可以提供一个上下文脚本数组-在这种情况下,所有这些脚本都将被导入:

var panel = panels.Panel({
        contentURL: self.data.url("page.html"),
        contentScriptFile: [self.data.url("script.js"), self.data.url('libs/jss.js')],
        contentStyleFile: self.data.url("style.css"),
        onHide: handleHide
    });

Not sure about loading order, but after documentready evrything accessible. 不确定加载顺序,但在准备好documentready就绪后即可访问。

More reading in official documentation . 官方文档中有更多阅读内容

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

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