简体   繁体   English

使用Firefox附加SDK读取指定主机的cookie

[英]Read cookies of specified host using Firefox Add-on SDK

I am developing a Mozilla Firefox extension using the Add-on SDK. 我正在使用附加SDK开发Mozilla Firefox扩展。 On every tab change event, I want to read cookies of specified host which is open in another tab. 在每个选项卡更改事件中,我想读取在另一个选项卡中打开的指定主机的cookie。 I reached up to tab change, but am trying to figure out the way to get the cookies of specified host in recent activated tab. 我已经达到了更改标签的目的,但是正在尝试找出在最近激活的标签中获取指定主机的cookie的方法。

var tabs = require("sdk/tabs");
tabs.on('activate', function(tab) {
    // want to get cookies here. 
});

Well, from within a tabs.on('activate') event handler, you have the tab . 好吧,从tabs.on('activate')事件处理程序中,您可以看到tab The tab object has a property url from which you can obtain the host. tab对象具有可从中获取主机的属性url Once you have the host, you can get the cookies for that host. 拥有主机后,就可以获取该主机的cookie。 You have not stated what you want to do with them. 您尚未说明您想对他们做什么。 So, here is just a way to enumerate them. 因此,这只是枚举它们的一种方法。

In order to use some methods of Services.cookies (nsICookieManager2) you will also need to require Chrome Authority . 为了使用Services.cookies(nsICookieManager2)的某些方法,您还需要提供Chrome Authority

var domainToUse = 'google.com';
var { Services }  = require("resource://gre/modules/Services.jsm");
var { Cc, Cu, Ci} = require("chrome");
let cookieEnumerator = Services.cookies.getCookiesFromHost(domainToUse);
while (cookieEnumerator.hasMoreElements()) {
  let cookie = cookieEnumerator.getNext().QueryInterface(Ci.nsICookie2); 
  console.log(cookie.host + ";" + cookie.name + "=" + cookie.value + "\n");
}

Update for newer versions of Firefox: Firefox较新版本的更新:
Note: At least by Firefox 50.0a2 (currently Firefox Developer Edition), it is necessary to use a slightly different call to getCookiesFromHost() to obtain the cookieEnumerator . 注意:至少在Firefox 50.0a2(当前为Firefox Developer Edition)上,有必要对getCookiesFromHost()使用稍有不同的调用来获取cookieEnumerator Without the change, the call to getCookiesFromHost() will display a warning message in the Browser Console directing you to visit the nsICookieManager2 MDN documentation page which has no updated information on the warning, or any documentation on the change. 如果不进行更改,对getCookiesFromHost()的调用将在浏览器控制台中显示警告消息,引导您访问nsICookieManager2 MDN文档页面该页面上没有警告的更新信息或更改的任何文档。 I had to look in the source code to determine what was required. 我必须查看源代码以确定所需的内容。 What appears to be desired is to pass in the current content document . 似乎需要传递当前的内容document However, from a background script that did not appear reasonable. 但是,从后台脚本来看似乎不合理。 The other way it is used is to just pass in an empty Object, {} . 使用它的另一种方法是只传入一个空对象{} Thus, that line is changed to: 因此,该行更改为:

let cookieEnumerator = Services.cookies.getCookiesFromHost(domainToUse,{});

It is supposed to be for passing in "the originAttributes of cookies that would be be retrieved." 它应该用于传递“将被检索的cookie的originAttributes ”。

The above code is slightly modified from my answer to "How to set custom cookies using Firefox Add-on SDK (using Services from Firefox Add-on SDK)" . 上面的代码与我对“如何使用Firefox附加SDK(使用Firefox附加SDK的服务)设置自定义cookie”的回答略作修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM