如何使用nsicookieservice获取cookie更改列表

时间:2013-08-21 13:20:03

标签: android cookies firefox-addon firefox-addon-restartless

我一直在阅读MDN docs about nsicookieservice,我对此服务的通知感兴趣:Cookie已更改且Cookie已拒绝。这是否意味着我们可以获得已更改或拒绝的Cookie列表?这可能是做这样的事情:

    var cookieSvc = Components.classes["@mozilla.org/cookieService;1"]
              .getService(Components.interfaces.nsICookieService);
    var cookies=cookieSvc.cookie-changed;

感谢您的解释。

我重读了文档(我真的这么做了)并搜索了许多扩展的源代码。我正在构建的扩展为firefox for android但我没有使用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);
    }

这不起作用,我没有在logcat中发现任何让我更加困惑的错误。我真的被困住了,我需要你的帮助。

1 个答案:

答案 0 :(得分:0)

是的,您的加载项可以接收有关Cookie的通知,但您不能通过Cookie管理器接收它们,但通用目的nsIObserverService

  • XUL叠加插件应直接使用nsIObserverService
  • SDK附加组件将需要使用system/events模块,这是一个更高级别,并且会自行清理。

还有a list of the most commonly used notification types