Join MDN and developers like you at Mozilla's View Source conference, 12-14 September in Berlin, Germany. Learn more at https://viewsourceconf.org

この記事はまだボランティアによって 日本語 に翻訳されていません。ぜひ MDN に参加して翻訳を手伝ってください!

This page describes how to register a new about: URL for your extension.

Setup for Firefox 4+

This is a simple example of a custom about page for bootstrapped addons. This example creates an about page with url about:myaboutpage which takes a user to a page that writes some text out.

Make sure to generate a unique aboutPage_id, see this page to learn more about generating UUID's: UUID.

It is imported to QueryInterface the Components.manager otherwise this will not work.

This example can be easily modified, just modify the global const at in the // globals section.

In Bug 1254752 things changed for Firefox 48 and up. Services.io.newChannel was deprecated, and the signature of the newChannel method changed. This change is reflected in the code below. This code will work for all versions of Firefox 4 and up.

const {classes: Cc, interfaces: Ci, manager: Cm, results: Cr, utils: Cu, Constructor: CC} = Components;
Cm.QueryInterface(Ci.nsIComponentRegistrar);

Components.utils.import("resource://gre/modules/Services.jsm");

// globals
var factory;
const aboutPage_description = 'This is my custom about page';
const aboutPage_id = 'aa132730-2278-11e5-867f-0800200c9a66'; // make sure you generate a unique id from https://www.famkruithof.net/uuid/uuidgen
const aboutPage_word = 'myaboutpage'
const aboutPage_page = 'data:text/html,hi this is the page that is shown when navigate to about:myaboutpage';

function AboutCustom() {}
AboutCustom.prototype = Object.freeze({
    classDescription: aboutPage_description,
    contractID: '@mozilla.org/network/protocol/about;1?what=' + aboutPage_word,
    classID: Components.ID('{' + aboutPage_id + '}'),
    QueryInterface: XPCOMUtils.generateQI([Ci.nsIAboutModule]),

    getURIFlags: function(aURI) {
        return Ci.nsIAboutModule.ALLOW_SCRIPT;
    },

    newChannel: function(aURI, aSecurity_or_aLoadInfo) {
        var channel;
        if (Services.vc.compare(Services.appinfo.version, '47.*') > 0) {
              let uri = Services.io.newURI(aboutPage_page, null, null);
              // greater than or equal to firefox48 so aSecurity_or_aLoadInfo is aLoadInfo
              channel = Services.io.newChannelFromURIWithLoadInfo(uri, aSecurity_or_aLoadInfo); 
        } else {
              // less then firefox48 aSecurity_or_aLoadInfo is aSecurity
              channel = Services.io.newChannel(aboutPage_page, null, null);
        }
        channel.originalURI = aURI;
        return channel;
    }
});

function Factory(component) {
    this.createInstance = function(outer, iid) {
        if (outer) {
            throw Cr.NS_ERROR_NO_AGGREGATION;
        }
        return new component();
    };
    this.register = function() {
        Cm.registerFactory(component.prototype.classID, component.prototype.classDescription, component.prototype.contractID, this);
    };
    this.unregister = function() {
        Cm.unregisterFactory(component.prototype.classID, this);
    }
    Object.freeze(this);
    this.register();
}

 

Instantiation Firefox 4+

Now in the startup procedure of your bootstrapped addon make sure to do register the factory, for example:

function startup(aData, aReason) {
    // ...
    factory = new Factory(AboutCustom);
    // ...
}

And then on shutdown of your bootstrapped addon make sure to destroy the factory:

function shutdown(aData, aReason) {
    if (aReason == APP_SHUTDOWN) { return }
    // ...
    factory.unregister();
    // ....
}

And there you have it, your own custom about page.

See Also

ドキュメントのタグと貢献者

タグ: 
 このページの貢献者: Noitidart, Np, yarrkov, Sheppy, Wikiwide, MichaelKohler
 最終更新者: Noitidart,