3

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

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

根据使用 JavaScript 代码模块一文,我已经content在文件中有一条指令chrome.manifest

content     myaddon   content/

所以,我应该能够导入test.jsmbootstrap.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。

可以从此处下载示例 XPI 文件。

4

1 回答 1

2

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

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

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

于 2015-08-01T21:39:10.367 回答