帮我创建一个 Firefox 扩展(Javascript XPCOM 组件)
我一直在查看不同的教程,我知道我已经很接近了,但我迷失在实现细节中,因为其中一些有点过时,并且自 Firefox 3 以来有些事情已经发生了变化。我已经 为firefox扩展编写了javascript,现在我需要将其制作成XPCOM组件。
这是我需要的功能: 我的 Javascript 文件很简单,有两个函数 startServer()
和 stopServer
。我需要在浏览器启动时运行 startServer()
,在 Firefox 退出时运行 stopServer()
。
编辑:
我已经用一个可行的解决方案更新了我的代码(感谢尼尔)。以下内容位于 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);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,您的所有代码都进入您的组件中。
您需要一个代表您的组件的 JavaScript 对象并将其注册到组件注册器。 (它可以是一个新对象,也可以对现有对象进行多任务处理。)完成此操作的方式取决于您的目标是 Firefox 3.x 还是 Firefox 4。
您需要注册
profile-after-change使用类别管理器的通知。完成此操作的方式还取决于您的目标是 Firefox 3、Firefox 3.5/6 还是 Firefox 4。
当配置文件更改后通知触发时,您的组件将被创建并调用观察方法。这是您启动服务器并要求观察退出应用程序通知的地方。请注意,这也调用了observe方法,因此它必须检查它收到的是哪个通知。
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.