最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Access specific cookies by domainname in Firefox extension - Stack Overflow

programmeradmin3浏览0评论

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/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.

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/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.

Share Improve this question edited Feb 28, 2012 at 7:49 Manatok asked Feb 28, 2012 at 6:57 ManatokManatok 5,7163 gold badges25 silver badges32 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You can use nsICookieManager2 interface (the original nsICookieManager interface was frozen and couldn't be changed which is why this extended version was created):

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

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 version nsICookieManager2 might go away as well, all the functionality will be exposed on nsICookieManager then.

发布评论

评论列表(0)

  1. 暂无评论