Skip to content

Instantly share code, notes, and snippets.

@jaredhirsch
Last active January 15, 2016 19:34
Show Gist options
  • Save jaredhirsch/8fa2be280f48ef96c1b0 to your computer and use it in GitHub Desktop.
Save jaredhirsch/8fa2be280f48ef96c1b0 to your computer and use it in GitHub Desktop.
loading code in one addon from another addon
// skipping the imports of Cu and AddonManager here:
// get an addon pointer
var addon;
AddonManager.getAddonByID('@idea-town-addon', function(x) { addon = x; });
// get the local machine's file path of the addon
var addonURI = addon.getResourceURI().asciiSpec;
// ugh, wrap the `file:///` URI in a `jar:///` URI,
// to get access to the metrics.jsm file inside the zipped idea town xpi.
// figured this out after a long search in dxr:
var xpiURI = 'jar:' + addonURI + '!/' + 'modules/metrics.jsm';
// finally, load the file, importing a Metrics object into the context
Cu.import(xpiURI);
// :grimacing:
@jaredhirsch
Copy link
Author

I guess somebody will probably google for this at some point in the future, so I'll also record here that I figured out, after a lot of looking, how you can get access to SDK's loader from non-SDK addons:

const { require } = Cu.import("resource://gre/modules/commonjs/toolkit/require.js");

You can wrap that in an if (typeof require === 'undefined') check to build up libraries that can be shared between SDK and non-SDK addons.

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