Restartless Firefox Add-ons, Part 2: Includes
Today I'm going to write about including files into your bootstrap.js file's scope. There are a few different types of files that you'll want to include, and I'm only going to talk about three of them today, importing javascript modules provided by Firefox or other extensions, and including (using loadSubScript) files packaged with your add-on, or any other.
Importing Modules
This is pretty simple, Components is available, so Components.utils.import is available. So import javascript modules like so:
Note: I highly recommend reviewing the Services.jsm module, it's a handy one.
Includes
There are many reasons why you'd want to access other files packaged with your add-on, one of them being if your bootstrap.js file starts getting large then you'll probably want to break that code up in to multiple files and include them in the bootstrap.js file. Another would be if your add-on needs to get the file location of a image or some other asset packaged with the add-on.
In order to figure out the location of files packaged with an add-on, the AddonManager.jsm & Services.jsm module will be needed, import it with the following code:
Components.utils.import("resource://gre/modules/AddonManager.jsm");
Then add the following implementation of a include function:
*
* @param src (String)
* The url of a javascript file to include.
*/
(function(global) global.include = function include(src) (
Services.scriptloader.loadSubScript(src, global)))(this);
Now in you'll want to call AddonManager.getAddonByID() with the add-on's id, which you can get from the bootstrap.js file's startup method's data param, like so:
function startup(data) AddonManager.getAddonByID(data.id, function(addon) {
// Include some utility functions
include(addon.getResourceURI("includes/utils.js").spec);
// Remember some image's url
img = addon.getResourceURI("images/star.png").spec;
});
I should be blogging about some includes which'll make developing restartless add-ons much easier in following posts!

@erikvold