1

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.

nmaier
  • 32,336
  • 5
  • 63
  • 78
user2102196
  • 99
  • 1
  • 7

2 Answers2

4

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).

nmaier
  • 32,336
  • 5
  • 63
  • 78
  • nmaier thanks for the reply,here how I did things, and i can't figure out where i'm making the mistake (trackdetect is the id of my extension): in chrome.manifest i wrote: content trackdetect chrome/includes/ in the bootstrap.js: Cu.import("chrome://trackdetect/includes/scanner.js"); Thanks again. – user2102196 Aug 12 '13 at 19:41
  • `content something somedir` will register the contents of somedir under `chrome://something/content/`. Hence `somedir/somefile.js` will be made available as `chrome://something/content/somefile.js`. In your case: `chrome/includes/scanner.js` will be available at `chrome://trackdetect/content/scanner.js`. You might refer to the already linked docs for more information. – nmaier Aug 12 '13 at 19:53
  • Sorry for taking so long to reply to your answer, I did the modifications as you explained, but I get this error: E/GeckoConsole(22534): No chrome package registered for chrome://trackdetect/content/scanner.js At the begining, I thought there is a problem with packaging the folder, but after unzipping a packaged extension, i found that the folder is there.So, I don't really know whats the origin of the problem, maybe import doesn't support "chrome://"?I would like to load the resource manually, but to be honest,there are parts of the (previously suggested)code that I don't understand. – user2102196 Aug 16 '13 at 19:20
  • Do you have your code somewhere accessible over the net, so I can have a look? – nmaier Aug 16 '13 at 20:55
  • Well, I know that's totally improper and I really hate asking you this, but can I send it to you in private? because, It's not accessible over the net. And I would totally understand if you refused or the site's admins. And really sorry. – user2102196 Aug 16 '13 at 22:16
  • Nah, that's not a good idea. I added links to a real world example to the answer, though. – nmaier Aug 16 '13 at 22:32
0

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;
user2102196
  • 99
  • 1
  • 7