0

I have a FF extension that I want notified when the lightweight theme list is changed. Here's the code:

var PesonaSwitcherObserver = {

    register: function() {
        PersonaSwitcher.log ("in register");
        var observerService =
            Components.classes["@mozilla.org/observer-service;1"]
            .getService(Components.interfaces.nsIObserverService);
        observerService.addObserver(PesonaSwitcherObserver,
            "lightweight-theme-list-change", false);
    },

    observe: function (subject, topic, data) {
        PersonaSwitcher.log ("in observe");
        switch (topic) {
            case 'lightweight-theme-list-change':
                PersonaSwitcher.subMenu();
                break;
        }
    },

    unregister: function() {
        PersonaSwitcher.log ("in unregister");
        var observerService =
            Components.classes["@mozilla.org/observer-service;1"]
            .getService(Components.interfaces.nsIObserverService);
        observerService.removeObserver(PesonaSwitcherObserver,
            "lightweight-theme-list-change");
    }
}

window.addEventListener("load", PesonaSwitcherObserver.register, false);
window.addEventListener("unload", PesonaSwitcherObserver.unregister, false);

The log receives the "in register", but no "in observes" when I add or remove personas. I've even looked at LightweightThemeManager.jsm and

function _updateUsedThemes(aList) {

calls

Services.obs.notifyObservers(null, "lightweight-theme-list-changed", null);

Anyone know why or have a hint?

4

1 回答 1

0

我猜想调用observerService.addObserver()失败 - 请检查错误控制台(按 Ctrl-Shift-J 打开它)。您的观察者没有实现QueryInterface功能,观察者服务将明确检查是否nsIObserver已实现。使用XPCOMUtils.jsm最容易实现此功能。如果您不想将其导入全局范围(您可能不想,因为您的代码似乎是从覆盖层运行的),您可以这样做:

var PesonaSwitcherObserver = {
    QueryInterface: Components.utils.import("resource://gre/modules/XPCOMUtils.jsm", null)
                              .XPCOMUtils
                              .generateQI([Components.interfaces.nsIObserver]),
    ...
于 2012-05-05T16:23:10.200 回答