Access specific cookies by domain / name in Firefox extension

I am developing a Firefox extension and need access to a specific cookie from a specific domain. I have this code that retrieves all cookies for all domains, as I can only request the cookie I'm looking for.

var {Cc, Ci} = require("chrome");

var cookieManager = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager);

var count = cookieManager.enumerator;

while (count.hasMoreElements()){
    var cookie = count.getNext();
    if (cookie instanceof Ci.nsICookie){
        console.log(cookie.host);
        console.log(cookie.name);
        console.log(cookie.value);
    }
}

To summarize, I can find the cookie I'm looking for with the code above, but I don't want it to repeat all cookies from all domains.

+5
source share
1 answer

You can use the nsICookieManager2interface (the original nsICookieManagerinterface was frozen and cannot be changed, therefore this extended version was created):

var cookieManager = Cc["@mozilla.org/cookiemanager;1"]
                      .getService(Ci.nsICookieManager2);
var count = cookieManager.getCookiesFromHost("example.com");

. Gecko 2.0 (Firefox 4). , nsICookieManager/nsICookieManager2, , Firefox nsICookieManager2 , nsICookieManager.

+6

All Articles