After playing around with bootstrap (restartless) add-ons a bit more, I came to the point where I needed some extra resources in the add-on. For images, I could have stuck with the data: URI approach I mentioned previously, but I also wanted to add an options UI for my add-on. Turns out it was pretty simple. Thanks to Vlad for getting the ball rolling.
The trick I am using now is to programmatically add a resource:
alias for my add-on. A resource: alias creates a handy way for you to create URIs pointing to resources, like scripts and images, in your add-on bundle. It’s pretty simple to add a resource: URI pointing to the add-on’s root folder or XPI bundle. Here is the code snippet taken from my bootstrap.js
file:
function startup(aData, aReason) {
let resource = Services.io.getProtocolHandler("resource").QueryInterface(Ci.nsIResProtocolHandler);
let alias = Services.io.newFileURI(aData.installPath);
if (!aData.installPath.isDirectory())
alias = Services.io.newURI("jar:" + alias.spec + "!/", null, null);
resource.setSubstitution("myaddonpackage", alias);
...
}
function shutdown(aData, aReason) {
if (aReason == APP_SHUTDOWN) return;
let resource = Services.io.getProtocolHandler("resource").QueryInterface(Ci.nsIResProtocolHandler);
resource.setSubstitution("myaddonpackage", null);
...
The isDirectory
check is needed because it is possible for add-ons to be installed in folders or XPI bundles, which require the jar:
URI syntax. The code adds a resource alias, after which you can use URIs like this:
resource://myaddonpackage/images/someimage.png
resource://myaddonpackage/content/somescript.js
In these examples, images
and content
are folders inside your add-on bundle. Now you can add URI-based resources to your install.rdf
like this:
...
resource://myaddonpackage/content/icon.png
resource://myaddonpackage/content/options.xul
...
Yes, the em:optionsURL
works just fine for providing an options UI in your add-on. I hope this tip gives you more help converting your traditional add-on to a restartless add-on.
Updated: I added the removal of the resource: alias on shutdown based on feedback from Nils in comments. Check out his link to some great bootstrap add-on information too. The part about stale caches after an add-on update presents a painful problem. Waldimir has also previously posted about the limitations of bootstrapped add-ons.