我一直在阅读有关nsicookieservice的MDN文档,我对该服务的通知很感兴趣:cookie已更改和cookie被拒绝。这是否意味着我们可以获得更改或拒绝的cookie列表?这样做可能吗:
var cookieSvc = Components.classes["@mozilla.org/cookieService;1"]
.getService(Components.interfaces.nsICookieService);
var cookies=cookieSvc.cookie-changed;
谢谢你的解释。
我重读了文档(我真的读了),并搜索了许多扩展的源代码。问题是,我正在构建的扩展是为firefoxforandroid引导的,但我没有使用SDK。根据我的理解,我写下了这篇文章:
var cookieMonster = {
//cookieManager: null,
current: null,
observerService: null,
cookieService: null,
init: function() {
this.observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
this.observerService.addObserver(this, "cookie-changed", false);
this.cookieService = Components.classes["@mozilla.org/cookieService;1"].getService(Components.interfaces.nsICookieService);
// this.cookieManager = Components.classes["@mozilla.org/cookiemanager;1"].getService(Components.interfaces.nsICookieManager);
},
uninit: function() {
//var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
this.observerService.removeObserver(this, "cookie-changed", false);
//this.cookieManager = Components.classes["@mozilla.org/cookiemanager;1"].getService(Components.interfaces.nsICookieManager);
},
observe: function(subject, topic, data) {
if (topic != 'cookie-changed') { this.current= "no cookie changed"; return; }
try {
// if (!ghostery.prefs.cookieProtect) { return; }
this.current="there is a chnged cookie!!!";
if ( (data == 'added') || (data == 'changed') ) {
var cookie = subject.QueryInterface(Components.interfaces.nsICookie2);
this.current = "name "+cookie.name+" host "+cookie.host;
}
} catch (err) {}
}
};
然后我这样做是为了在加载页面时添加观察者:
let addListener = function() {
window.BrowserApp.deck.addEventListener("load", cookieMonster.init, false);
window.NativeWindow.toast.show(cookieMonster.current, "long");
};
if(window.BrowserApp.deck) {
// BrowserApp.deck (and maybe whole BrowserApp?) has been initialized
addListener();
}
else {
// use the chrome window to wait for BrowserApp to initialize
window.BrowserApp.deck.addEventListener("UIReady", addListener);
}
这不起作用,我在日志中没有发现任何错误,这让我更加困惑。我真的很困,我需要你的帮助。
是的,您的插件可以接收有关cookie的通知,但您不会通过cookie管理器接收,而是通过通用nsIObserverService
接收。
- XUL覆盖加载项应该直接使用
nsIObserverService
- SDK加载项将希望使用
system/events
模块,这是一个更高级别的模块,并且会自行清理
还有一个最常用的通知类型列表。