翻译不完整。 请帮助我们翻译这篇文章!

我们即将放弃这篇文档中描述的 Firefox 附加组件技术。

请勿使用下列技术开发新的附加组件。请改用 WebExtension 代替。

如果您在维护使用下列技术的附加组件,请考虑将其迁移为使用 WebExtensions 技术。

使用下列技术开发的附加组件可能不适用于多进程 Firefox(e10s),它已在 Firefox Nightly 和 Firefox Developer Edition 中默认启用,并很快将遍及 Firefox 的公测版(Beta)和正式版。我们准备了制作多进程兼容的附加组件的文档,但迁移到 WebExtension 是更加着眼于未来的选择。

有关的 wiki 页面写有协助开发人员过渡到新技术的有关资源、迁移路径、办公时间等信息


Contract IDs



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.

Accessing XPCOM components from JavaScript

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.

Creating an instance of a component

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"]

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.

Getting an XPCOM service

var preferences = Components.classes["@mozilla.org/preferences-service;1"]

You can then call any methods in the nsIPrefService interface on the preferences object.

Getting a different interface for a component

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.

Determining which interfaces an XPCOM component supports

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.

XPCOMUtils - About protocol handler

This example implements a quick about protocol handler in JS using 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]);