user3004946
user3004946

Reputation: 1

nsICookieManager XPCOM Interface does not work properly

I am developing an add-on for Mozilla. I use this function when I visit a site to list all the cookies contained there. At first, the code was working properly. When I visited a site, I was getting a list with the cookies contained in there, then in a second site I was getting the cookies contained ONLY in the second site. Now, without changing anything, when I visit a second site, I get some cookies from the previous site. I need to hit the new site 2 times in a row to get only its cookies. Are there any recent changes in the way that cookieManager works or any other ideas what is wrong with the code?

//Mozilla 39.0
var{ToggleButton}=require("sdk/ui/button/toggle");
var prefService=require("sdk/preferences/service");
var imagePermission="permissions.default.image";
var windows = require("sdk/windows").browserWindows;
var Request = require("sdk/request").Request;
var window = require('sdk/window/utils').getMostRecentBrowserWindow();
require("sdk/tabs").on("load",logURL);
function logURL(tab) {
    console.log(tab.url);
    func();
    func(); //i use this to check if the removeAll() works.
};
function func() {
    var {Cc, Ci} = require("chrome");
    var cookieManager = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager);
    var iter = cookieManager.enumerator;
    var cookie_count = 0;
    var hosts = [];
    var names = [];
    console.log(hosts.length);
    while (iter.hasMoreElements()) {
        cookie_count++;
        var cookie = iter.getNext();
        if (cookie instanceof Ci.nsICookie) {
            hosts.push(cookie.host);
            names.push(cookie.name);
        }
    }
    //if a call this function twice without changing the page the cookie list seems empty
    cookieManager.removeAll();
    console.log(cookie_count);
    console.log(hosts);
    console.log(names);
};

The sites I visit are first youtube.com, which gives 6 cookies, then I hit intel.com, which gives 29 cookies, but there are leftovers from youtube.com. If I hit intel.com again, I get 26 cookies with no trace of leftovers from previous site (youtube.com). The number of the cookies you will get may vary a bit.

Upvotes: 0

Views: 85

Answers (0)

Related Questions