在Firefox扩展中导入Javascript模块

时间:2015-08-01 21:30:43

标签: firefox-addon javascript firefox-addon-bootstrap

我正在开发一个简单的Firefox扩展程序,但导入我的自定义Javascript模块文件时遇到问题。 bootstrap.js文件位于根目录,test.jsm位于content文件夹中。 test.jsm将2个符号foobar导出为:

this.EXPORTED_SYMBOLS = ["foo", "bar"];

根据Using JavaScript code modules文章,我在content文件中已经有chrome.manifest条指令:

content     myaddon   content/

因此,我应该可以将test.jsm导入bootstrap.js作为:

 Components.utils.import("chrome://myaddon/content/test.jsm");

但我在浏览器控制台中看到以下2个警告:

addons.xpi
WARN
Error loading bootstrap.js for myaddon@oyenamit: [Exception... "Component returned failure code: 0x80520012 (NS_ERROR_FILE_NOT_FOUND) [nsIXPCComponents_Utils.import]"
nsresult: "0x80520012 (NS_ERROR_FILE_NOT_FOUND)"
location: "JS frame :: resource://gre/modules/addons/XPIProvider.jsm ->
jar:file:///Users/oyenamit/Library/Application%20Support/Firefox/Profiles/profilename/extensions/myaddon@oyenamit.xpi!/bootstrap.js :: <TOP_LEVEL> :: line 1"
data: no] Stack trace: resource://gre/modules/addons/XPIProvider.jsm ->
jar:file:///Users/oyenamit/Library/Application%20Support/Firefox/Profiles/profilename/extensions/myaddon@oyenamit.xpi!/bootstrap.js:1
< resource://gre/modules/addons/XPIProvider.jsm:4348
< XPI_loadBootstrapScope()@resource://gre/modules/addons/XPIProvider.jsm:4348
< XPI_callBootstrapMethod()@resource://gre/modules/addons/XPIProvider.jsm:4423
< AI_startInstall/<()@resource://gre/modules/addons/XPIProvider.jsm:5849
< next()@self-hosted:675 < TaskImpl_run()@resource://gre/modules/Task.jsm:330
< Handler.prototype.process()@resource://gre/modules/Promise.jsm ->
resource://gre/modules/Promise-backend.js:867 <
this.PromiseWalker.walkerLoop()@resource://gre/modules/Promise.jsm ->
resource://gre/modules/Promise-backend.js:746 <
this.PromiseWalker.scheduleWalkerLoop/<()@resource://gre/modules/Promise.jsm ->
resource://gre/modules/Promise-backend.js:688 < <file:unknown>




addons.xpi
WARN
Exception running bootstrap method startup on myaddon@oyenamit: ReferenceError: foo is not defined (resource://gre/modules/addons/XPIProvider.jsm ->
jar:file:///Users/oyenamit/Library/Application%20Support/Firefox/Profiles/profilename/extensions/myaddon@oyenamit.xpi!/bootstrap.js:16:4) JS Stack trace:
startup@resource://gre/modules/addons/XPIProvider.jsm ->
jar:file:///Users/oyenamit/Library/Application%20Support/Firefox/Profiles/profilename/extensions/myaddon@bootstrap.js:16:5 <
XPI_callBootstrapMethod@XPIProvider.jsm:4451:9 <
AI_startInstall/<@XPIProvider.jsm:5864:13

我做错了什么?我使用的是Firefox 39.0。

可以从here下载示例XPI文件。

1 个答案:

答案 0 :(得分:2)

需要在bootstrap.js文件的startup函数中导入自定义JSM模块,我不知道为什么会这样,但它就是这样的。如果您将其导入文件顶部,它将无法工作。

请务必在关机时将其卸载,这是一个非常简单的演示和如何操作指南:

https://gist.github.com/Noitidart/9045387#file-bootstrap-js-L6