Importing Javascript module in Firefox extension

2.3k views Asked by At

I am developing a simple Firefox extension but am having trouble importing my custom Javascript module file. The bootstrap.js file is at the root and test.jsm is inside the content folder. test.jsm exports 2 symbols foo and bar as:

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

As per the Using JavaScript code modules article, I already have a content instruction in the chrome.manifest file:

content     myaddon   content/

So, I should be able to import test.jsm into bootstrap.js as:

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

But I see the following 2 warnings in the Browser Console:

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/[email protected]!/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/[email protected]!/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/[email protected]!/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/[email protected]:16:5 <
[email protected]:4451:9 <
AI_startInstall/<@XPIProvider.jsm:5864:13

What am I doing wrong? I am using Firefox 39.0.

A sample XPI file can be downloaded from here.

1

There are 1 answers

3
Noitidart On BEST ANSWER

Custom JSM modules need to be imported in the startup function of your bootstrap.js file, i dont know why this is, but it just works like that. It wont work if you import it at the top of your file.

Be sure to unload it it on shutdown, this is a very simple demo and guide on how to do it:

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