帮我创建一个 Firefox 扩展(Javascript XPCOM 组件)

发布于 2024-10-14 14:03:26 字数 2004 浏览 6 评论 0原文

我一直在查看不同的教程,我知道我已经很接近了,但我迷失在实现细节中,因为其中一些有点过时,并且自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望笑 2024-10-21 14:03:26

据我所知,您的所有代码都进入您的组件中。

您需要一个代表您的组件的 JavaScript 对象并将其注册到组件注册器。 (它可以是一个新对象,也可以对现有对象进行多任务处理。)完成此操作的方式取决于您的目标是 Firefox 3.x 还是 Firefox 4。

您需要注册 profile-after-change使用类别管理器的通知。完成此操作的方式还取决于您的目标是 Firefox 3、Firefox 3.5/6 还是 Firefox 4。

当配置文件更改后通知触发时,您的组件将被创建并调用观察方法。这是您启动服务器并要求观察退出应用程序通知的地方。请注意,这也调用了observe方法,因此它必须检查它收到的是哪个通知。

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;
    }
  }
};

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;
    }
  }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文