firefox 插件:无法监听 firefox“退出应用程序”事件

发布于 2024-12-20 20:05:46 字数 2525 浏览 1 评论 0原文

我有以下代码片段可以在 Firefox 退出后启用扩展,

observe: function (subject, topic, data) {

     if (topic == "quit-application") {
         LOG("inside quit-application Testing ");
         var os = Components.classes["@mozilla.org/observer-service;1"]
             .getService(Components.interfaces.nsIObserverService);

         //os.addObserver(this, "http-on-examine-cached-response", false);
         os.addObserver(this, "quit-application", false);
         var appInfo = Components.classes["@mozilla.org/xre/app-info;1"]
             .getService(Components.interfaces.nsIXULAppInfo);
         var tempappVersion = appInfo.version;
         var appVersion = tempappVersion.split(".");
         // adding add-on listener dsable event on add-on for FF4 and later versions.
         if (appVersion[0] >= 4) {
             setAddonEnableListener();
             LOG("\napp-startup Testing from javascript file....");
         }
         return;
     }
}

并且在 setAddonEnableListener 内我尝试启用这样的扩展:

function setAddonEnableListener() {
    try {
    alert("setAddonEnableListener akbar nsListener called from ");
        Components.utils.import("resource://gre/modules/AddonManager.jsm");
    AddonManager.getAddonByID("[email protected]", function(addon)
    {
    if (addon.userDisabled)
        addon.userDisabled = false;
    });

    } catch (ex) {
    }
}

并且我正在注册 quit-application 事件var 像这样:

myModule = {
    registerSelf: function (compMgr, fileSpec, location, type) {
        var compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
        compMgr.registerFactoryLocation(this.myCID,
                                        this.myName,
                                        this.myProgID,
                                        fileSpec,
                                        location,
                                        type);

        var catMgr = Components.classes["@mozilla.org/categorymanager;1"].getService(Components.interfaces.nsICategoryManager);
        catMgr.addCategoryEntry("app-startup", this.myName, this.myProgID, true, true);
        catMgr.addCategoryEntry("quit-application", this.myName, this.myProgID, true, true);
    }

当 Firefox 退出时,inside quit-application Testing 消息不会显示。我在这里做错了什么?

I have the following code snippet to enable extension after firefox quit,

observe: function (subject, topic, data) {

     if (topic == "quit-application") {
         LOG("inside quit-application Testing ");
         var os = Components.classes["@mozilla.org/observer-service;1"]
             .getService(Components.interfaces.nsIObserverService);

         //os.addObserver(this, "http-on-examine-cached-response", false);
         os.addObserver(this, "quit-application", false);
         var appInfo = Components.classes["@mozilla.org/xre/app-info;1"]
             .getService(Components.interfaces.nsIXULAppInfo);
         var tempappVersion = appInfo.version;
         var appVersion = tempappVersion.split(".");
         // adding add-on listener dsable event on add-on for FF4 and later versions.
         if (appVersion[0] >= 4) {
             setAddonEnableListener();
             LOG("\napp-startup Testing from javascript file....");
         }
         return;
     }
}

And inside the setAddonEnableListener I am trying to enable the extension like this:

function setAddonEnableListener() {
    try {
    alert("setAddonEnableListener akbar nsListener called from ");
        Components.utils.import("resource://gre/modules/AddonManager.jsm");
    AddonManager.getAddonByID("[email protected]", function(addon)
    {
    if (addon.userDisabled)
        addon.userDisabled = false;
    });

    } catch (ex) {
    }
}

And I am registering the quit-application event var like this:

myModule = {
    registerSelf: function (compMgr, fileSpec, location, type) {
        var compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
        compMgr.registerFactoryLocation(this.myCID,
                                        this.myName,
                                        this.myProgID,
                                        fileSpec,
                                        location,
                                        type);

        var catMgr = Components.classes["@mozilla.org/categorymanager;1"].getService(Components.interfaces.nsICategoryManager);
        catMgr.addCategoryEntry("app-startup", this.myName, this.myProgID, true, true);
        catMgr.addCategoryEntry("quit-application", this.myName, this.myProgID, true, true);
    }

When Firefox quits, inside quit-application Testing message is not getting displayed. What am I doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

酒绊 2024-12-27 20:05:46

没有类别quit-application。您应该收到 app-startup 通知(或者从 Firefox 4 开始,profile-after-change)并注册您的观察者 quit-application

observe: function (subject, topic, data) {
   if (topic == "app-startup" || topic == "profile-after-change") {
     var observerService = Components.classes["@mozilla.org/observer-service;1"]
                                     .getService(Components.interfaces.nsIObserverService);
     observerService.addObserver(this, "quit-application", false);
   }
   else if (topic == "quit-application") {
     var observerService = Components.classes["@mozilla.org/observer-service;1"]
                                     .getService(Components.interfaces.nsIObserverService);
     observerService.removeObserver(this, "quit-application");
     ...
   }
}

引用文档

除非另有说明,否则您将使用 nsIObserverService 注册主题。

并且在任何关闭通知上都没有关于通过类别管理器注册的注释。

顺便说一句,我真诚地推荐 XPCOMUtils 进行组件注册。您不需要自己编写模块定义。

There is no category quit-application. You should receive app-startup notification (or rather profile-after-change starting with Firefox 4) and register your observer for quit-application:

observe: function (subject, topic, data) {
   if (topic == "app-startup" || topic == "profile-after-change") {
     var observerService = Components.classes["@mozilla.org/observer-service;1"]
                                     .getService(Components.interfaces.nsIObserverService);
     observerService.addObserver(this, "quit-application", false);
   }
   else if (topic == "quit-application") {
     var observerService = Components.classes["@mozilla.org/observer-service;1"]
                                     .getService(Components.interfaces.nsIObserverService);
     observerService.removeObserver(this, "quit-application");
     ...
   }
}

To quote the documentation:

Unless otherwise noted you register for the topics using the nsIObserverService.

And there is no note about registration via category manager on any shutdown notifications.

Btw, I sincerely recommend XPCOMUtils for component registration. You shouldn't need to write the module definition yourself.

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