我有一个FF扩展,我希望在更改轻量级主题列表时得到通知。这是代码:
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);
日志收到“in register”,但在添加或删除角色时没有“观察”。我甚至看过LightweightThemeManager.jsm和
function _updateUsedThemes(aList) {
呼叫
Services.obs.notifyObservers(null, "lightweight-theme-list-changed", null);
任何人都知道为什么或有提示?
答案 0 :(得分: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]),
...