Skip to content

Instantly share code, notes, and snippets.

@erikvold
Created June 14, 2011 03:20
Show Gist options
  • Save erikvold/1024242 to your computer and use it in GitHub Desktop.
Save erikvold/1024242 to your computer and use it in GitHub Desktop.
A include() function for restartless Firefox add-ons
/* Includes a javascript file with loadSubScript
* By Erik Vold <erikvvold@gmail.com> http://erikvold.com/
*
* @param src (String)
* The url of a javascript file to include.
*/
(function(global) global.include = function include(src) {
var o = {};
Components.utils.import("resource://gre/modules/Services.jsm", o);
var uri = o.Services.io.newURI(
src, null, o.Services.io.newURI(__SCRIPT_URI_SPEC__, null, null));
o.Services.scriptloader.loadSubScript(uri.spec, global);
})(this);
@johan
Copy link

johan commented Aug 17, 2011

A thought / question: with loadSubScript being synchronous, I take it it sort of defeats a core idea (making the startup process faster / more responsive despite a ton of add-ons installed) of the new extension setup if we use this in bootstrap.js?

@erikvold
Copy link
Author

The core reason for using a bootstrap.js is to have a restartless addon, the performance goal is completely separate. Also, one doesn't need to include a script until it is needed, so it won't effect the startup time unless the code is needed at startup.

@johan
Copy link

johan commented Aug 19, 2011

I didn't consider the second part, but that's very true.

My reflection on the "core idea" was referring to Mozilla's reasons behind bootstrap.js and the new way of registering, not an individual add-on developer's motivation for using the system devised. Us add-on developers will probably always stick to whichever paved path we find that works for us. :-)

(Just in case it isn't obvious from my question, I am not insinuating being right, but trying to verify whether I had understood their underlying motives right, and if this synchronous way slightly veers in the other direction or is entirely inconsequential.)

@erikvold
Copy link
Author

I'm pretty certain Mozilla's main motivation for creating the bootstrap.js is solely to support restartless addons as best they could design such a thing at the time.

bootstrap.js files have to be evaluated during startup (if enabled) which I suspect would diminish startup times for most addons by default. Since most old school addons usually only evaluate code after a chrome window has been overlayed.

Sync code is worse than async code though, and Mozilla is trying converting to the latter style quite a bit, mainly because it will/might be required for e10s. But this doesn't have much to do with bootstrap.js.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment