I am developing a Firefox extension and need to access a specific cookie from a specific domain. I have this code which fetches all cookies for all domains, how do I request only the cookie that I am 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 sum up, I am able to find the cookie that I am looking for with the code above but I don't want to have to iterate through all of the cookies from all domains.
You can use
nsICookieManager2
interface (the originalnsICookieManager
interface was frozen and couldn't be changed which is why this extended version was created):Note: the concept of frozen interfaces was dropped in Gecko 2.0 (Firefox 4). Since then some interfaces similar to
nsICookieManager
/nsICookieManager2
have been unified - so in a future Firefox versionnsICookieManager2
might go away as well, all the functionality will be exposed onnsICookieManager
then.