Support for extensions using XUL/XPCOM or the Add-on SDK was removed in Firefox 57, released November 2017. As there is no supported version of Firefox enabling these technologies, this page will be removed by December 2020.
Add-ons using the techniques described in this document are considered a legacy technology in Firefox. Don't use these techniques to develop new add-ons. Use WebExtensions instead. If you maintain an add-on which uses the techniques described here, consider migrating it to use WebExtensions.
Starting from Firefox 53, no new legacy add-ons will be accepted on addons.mozilla.org (AMO) for desktop Firefox and Firefox for Android.
Starting from Firefox 57, only extensions developed using WebExtensions APIs will be supported on Desktop Firefox and Firefox for Android.
Even before Firefox 57, changes coming up in the Firefox platform will break many legacy extensions. These changes include multiprocess Firefox (e10s), sandboxing, and multiple content processes. Legacy extensions that are affected by these changes should migrate to use WebExtensions APIs if they can. See the "Compatibility Milestones" document for more information.
A wiki page containing resources, migration paths, office hours, and more, is available to help developers transition to the new technologies.
Here are a few useful snippets of code for dealing with XPCOM components in JavaScript.
A contract ID is a unique name for an XPCOM object. They are used to create or access well-known objects in XPCOM.
Every XPCOM object implements one or more interfaces. An interface is simply a list of constants and methods that can be called on the object, an example is nsIFile
. Every XPCOM object must implement the nsISupports
interface.
XPCOM objects are either created as new instances (each creation gives you a completely new COM object) or as services (each access gives you the same COM object, often called a singleton). Whether you must create a new instance or access as a service depends on the contract. In order to get an XPCOM object you need to know the contract ID of the object and the interface that you wish to use on it.
The preferred method of creating XPCOM instances is via the Components.Constructor
helper. For example,
var nsFile = Components.Constructor("@mozilla.org/file/local;1", "nsIFile", "initWithPath"); var file = new nsFile(filePath);
They can also be created and initialized manually:
var file = Components.classes["@mozilla.org/file/local;1"] .createInstance(Components.interfaces.nsIFile); file.initWithPath(filePath);
This creates a new instance of the object with contract ID @mozilla.org/file/local;1
and allows you to call methods from the nsIFile
interface on it.
var preferences = Components.classes["@mozilla.org/preferences-service;1"] .getService(Components.interfaces.nsIPrefService);
You can then call any methods in the nsIPrefService
interface on the preferences object.
Some components implement more than one interface. Sometimes JavaScript is clever enough to know all the interfaces available on a component, but in most cases you will have to explicitly check for an interface. With the preferences service from the previous example we can do the following:
var preferences = preferences.QueryInterface(Components.interfaces.nsIPrefBranch2);
This allows you to use the methods in the nsIPrefBranch2
interface.
To display a list of all interfaces that an XPCOM component supports, do the following:
// |c| is the XPCOM component instance for each (i in Components.interfaces) { if (c instanceof i) { alert(i); } }
In this context, instanceof
is the same as QueryInterface
except that it returns false instead of throwing an exception when |c|
doesn't support interface |i|
. Another difference is that QueryInterface
returns an object, where as instanceof
returns a boolean
.
This example implements a quick about protocol handler in JS using XPCOMUtils.jsm.
Components.utils.import("resource://gre/modules/Services.jsm"); Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); const Cc = Components.classes; const Ci = Components.interfaces; function AboutHandler() {} AboutHandler.prototype = { newChannel: function(uri) { var channel = Services.io.newChannel("chrome://mystuff/content/mystuff.xul", null, null); channel.originalURI = uri; return channel; }, getURIFlags: function(uri) { // Do NOT return Ci.nsIAboutModule.URI_SAFE_FOR_UNTRUSTED_CONTENT unless // you make sure to set a non-system principal in newChannel. return 0; }, classDescription: "About MyStuff Page", classID: Components.ID("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"), contractID: "@mozilla.org/network/protocol/about;1?what=mystuff", QueryInterface: XPCOMUtils.generateQI([Ci.nsIAboutModule]) } var NSGetModule = XPCOMUtils.generateNSGetModule([AboutHandler]); }