在Firefox扩展中通过域/名称访问特定的Cookie [英] Access specific cookies by domain/name in Firefox extension
问题描述
我开发了一个Firefox扩展,需要访问来自特定域的特定cookie。我有这个代码,它获取所有域的所有cookie,我如何请求只有我正在寻找的cookie。
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);
}
}
总而言之,我正在寻找上面的代码,但我不想要遍历所有的域中的所有cookie。
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.
推荐答案
您可以使用 nsICookieManager2
介面(原始 nsICookieManager
界面已冻结,无法更改,这是为什么创建此扩展版本):
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.org/cookiemanager;1"]
.getService(Ci.nsICookieManager2);
var count = cookieManager.getCookiesFromHost("example.com");
注意:冻结接口的概念在Gecko 2.0从那时起,与 nsICookieManager
/ nsICookieManager2
类似的一些接口已统一 - 因此在未来的Firefox版本 nsICookieManager2
也可能会消失,所有的功能将暴露在 nsICookieManager
。
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.
这篇关于在Firefox扩展中通过域/名称访问特定的Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!