including a js file in bootstrap.js firefox for android extension

989 views Asked by At

I'm working on a bootstrapped extension for firefox for android and I want to import a js file to the bootstap.js (like importing a component in xul extensions). The classic method of using the chrome.manifest to use a resource alia is not allowed for bootstrapped extensions. I tried this code of Erik Vold but it doesn't seem to work

function startup(data) AddonManager.getAddonByID(data.id, function(addon) {
// Include some utility functions
include(addon.getResourceURI("includes/scanner.js").spec);
});

I really appreciate some help and thank you.

2

There are 2 answers

6
nmaier On BEST ANSWER

Actually, content is supported in bootstrapped add-ons since quite some time (first in Gecko 8 and starting with Gecko 10, it will auto load manifests, IIRC). You can load js code modules from chrome://yourpackage/content/... not just from resource-URIs (since Gecko 4, IIRC). So there is nothing holding you back from using mozilla-style code modules. Also, a couple of add-ons manually add resource substitutions.

The stuff that Erik wrote uses a custom include function, implemented in the corresponding bootstrap.js using loadSubScript. That's an option, too. This scheme was invented when it wasn't yet possible to use js code modules from bootstrapped add-ons properly, e.g. because there was no Cu.unload yet.

Using content + Cu.import + Cu.unload is likely the easiest approach.

Real world example in one of my own add-ons (Desktop + Android) (the rest of the add-on is written using a custom commonjs-style require loader, so don't get distracted by that).

0
user2102196 On

I found this way to do it:

    var IncludedFile={};
    Services.scriptloader.loadSubScript(aData.resourceURI.spec + "content/fileName.js", IncludedFile);

Then you can access the methods like this:

    IncludedFile.someFunction;