开发者

Help me create a Firefox extension (Javascript XPCOM Component)

开发者 https://www.devze.com 2023-02-07 22:01 出处:网络
I\'ve been looking at different tutorials and I know I\'m close but I\'m getting lost in implementation details because some of them are a little bit dated and a few things have changed since Firefox

I've been looking at different tutorials and I know I'm close but I'm getting lost in implementation details because some of them are a little bit dated and a few things have changed since Firefox 3. I have already written the javascript for the firefox extension, now I need to make it into an XPCOM component.

This is the functionality that I need: My Javascript file is simple, I have two functions startServer() and stopServer. I need to run startServer() when the browser starts and stopServer() when firefox quits.

Edit:

I've updated my code with a working solution (thanks to Neil). The following is in MyExtension/components/myextension.js.

Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
const CI = Components.interfaces, CC = Components.classes, CR = Components.results;

// class declaration
function MyExtension() {}

MyExtension.prototype = {
    classDescription: "My Firefox Extension",
    classID:          Components.ID("{xxxx-xxxx-xxx-xxxxx}"),
    contractID:       "@example.com/MyExtension;1",
    QueryInterface: XPCOMUtils.generateQI([CI.nsIObserver]),

    // add to category manager
    _xpcom_categories: [{
        category: "profile-after-change"
    }],

    // start socket server
    startServer: function () { /* socket initialization code */ },

    // stop socket server
    stopServer: function () { /* stop server */ },


    observe: function(aSubject, aTopic, aData) 
    {
        var obs = CC["@mozilla.org/observer-service;1"].getService(CI.nsIObserverService);

        switch (aTopic) 
        {
            case "quit-application":
                this.stopServer();
                obs.removeObserver(this, "quit-application");
                break;
            case "profile-after-change":
                this.startServer();
               开发者_如何学Go obs.addObserver(this, "quit-application", false);
                break;
            default:
                throw Components.Exception("Unknown topic: " + aTopic);
        }
    }
};

var components = [MyExtension];

function NSGetModule(compMgr, fileSpec) {
    return XPCOMUtils.generateModule(components);
}


As far as I can tell, all of your code goes into your component.

You need a JavaScript object that represents your component and register it with the component registrar. (It can be a new object or you can multitask an existing object.) The way this is done depends on whether you're targetting Firefox 3.x or Firefox 4.

You need to register for the profile-after-change notification using the category manager. The way this is done also depends on whether you're targetting Firefox 3, Firefox 3.5/6 or Firefox 4.

When the profile-after-change notification fires, your component is then created and the observe method is called. This is where you start your server and also ask to observe the quit-application notification. Note that this also calls the observe method, so it has to check which notification it's getting.

function myExt() {}
myExt.prototype = {
  observe: function(aSubject, aTopic, aData) {
    switch (aTopic) {
      case "quit-application":
        stopServer();
        obs.removeObserver(this, "quit-application");
        break;
      case "profile-after-change":
        startServer();
        obs.addObserver(this, "quit-application", false);
        break;
    }
  }
};
0

精彩评论

暂无评论...
验证码 换一张
取 消