Help me create a Firefox extension (Javascript XPCOM Component)
Posted
by
Johnny Grass
on Stack Overflow
See other posts from Stack Overflow
or by Johnny Grass
Published on 2011-01-29T02:06:54Z
Indexed on
2011/02/01
15:25 UTC
Read the original article
Hit count: 408
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();
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);
}
© Stack Overflow or respective owner