firefox插件:无法监听firefox“quit-application”事件

时间:2011-12-12 16:10:31

标签: javascript macos firefox-addon

我有以下代码片段在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("somename@extension.com", 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消息未显示。我在这里做错了什么?

1 个答案:

答案 0 :(得分:2)

没有类别quit-application。您应该收到app-startup通知(或者更确切地说是profile-after-change从Firefox 4开始)并注册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");
     ...
   }
}

引用the documentation

  

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

在任何关机通知中,没有关于通过类别经理注册的说明。

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