javascript - 从 chrome.webRequest.onBeforeSendHeaders 中提取 cookie

标签 javascript cookies firefox-addon firefox-addon-webextensions

我正在开发一个 Firefox 插件来拦截 HTTP 请求并提取 cookie。我能够从 header 中提取“用户代理”,但无法提取 cookie。我使用的代码如下。

chrome.webRequest.onBeforeSendHeaders.addListener(function(details){
  var headers = details.requestHeaders,
  blockingResponse = {};

  for( var i = 0, l = headers.length; i < l; ++i ) {
    window.alert("Checking headers");
    if( headers[i].name == 'Cookie' ) {
       headers[i].value = 'twid=notsecret';
       window.alert("Cookie Changed");
       console.log(headers[i].value);
       break;
    }
  }

  blockingResponse.requestHeaders = headers;
  return blockingResponse;
},
{urls: [ "<all_urls>" ]},['requestHeaders','blocking']);

为什么这不起作用?是否有替代方法?

最佳答案

您可能正在 49.0a 之前的 Firefox 版本中进行测试:
这对您不起作用的最可能原因是您正在 49.0a 版本之前的 Firefox 版本(目前为测试版)中进行测试。如果是这样,当您尝试使用 window.alert() 时,您的代码将引发错误。到 Firefox 49.0a 版本,alert() 不再抛出错误,而是打开 Browser Console并打印以下行(除了您尝试在 alert() 中显示的文本之外):

alert() is not supported in background windows; please use console.log instead.

在 49.0a 之前的 Firefox 版本中,当您尝试 alert() 时,您将在浏览器控制台中看到以下错误:

NS_ERROR_NOT_AVAILABLE: Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIDOMWindowUtils.isParentWindowMainWidgetVisible]                   nsPrompter.js:346
uncaught exception: unknown (can't convert to string)                                                                                                              (unknown)

在您的实例中,在 webRequest 监听器中使用 alert() 时,错误消息会有所不同:

NS_ERROR_NOT_AVAILABLE: Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIDOMWindowUtils.isParentWindowMainWidgetVisible]                                                                                                                                                                 nsPrompter.js:346
[Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIDOMWindowUtils.isParentWindowMainWidgetVisible]"  nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)"  location: "JS frame :: resource://gre/components/nsPrompter.js :: openModalWindow :: line 346"  data: no]             (unknown)

您的代码按编写方式工作:
除此之外,您的代码按照问题中所写的方式工作。但是,当您通过 console.log() 输出新的 cookie 值时,不会验证请求 header 是否确实已更改。为此,您需要在 next event to fire in the chain 上添加一个额外的监听器。 :webRequest.onSendHeaders

这里是一些修改后的代码,用于验证 cookie header 是否确实已更改。经过测试并验证可在 FF48.0.2+ 中运行:

manifest.json:

{
    "description": "Demonstrate changing the cookie of a WebRequest",
    "manifest_version": 2,
    "name": "change-webrequest-cookie-demo",
    "version": "0.1",

    "permissions": [
        "webRequest",
        "webRequestBlocking",
        "<all_urls>" //Required for Google Chrome. Not, currently, needed for Firefox.
    ],

    "background": {
        "scripts": ["background.js"]
    }
}

background.json:

/*Demonstrate changing the cookie of a WebRequet */
// For testing, open the Browser Console
try{
    //Alert is not supported in Firefox. In in FF49.0+, forces the Browser Console open.
    alert('Open the Browser Console.');
}catch(e){
    //alert throws an error in Firefox versions earlier than 49.0
    console.log('Alert threw an error. Probably, the version of Firefox is less than 49.');
}

chrome.webRequest.onBeforeSendHeaders.addListener(function(details){
  var blockingResponse = {};
  //console.log("Checking headers");
  details.requestHeaders.some(function(header){
      if( header.name == 'Cookie' ) {
          console.log("Original Cookie value:" + header.value);
          header.value = 'twid=notsecret';
          console.log("Cookie Changed");
          return true;
      }
      return false;
  });
  blockingResponse.requestHeaders = details.requestHeaders;
  return blockingResponse;
}, {urls: [ "<all_urls>" ]},['requestHeaders','blocking']);

chrome.webRequest.onSendHeaders.addListener(function(details){
  details.requestHeaders.some(function(header){
      if( header.name == 'Cookie' ) {
          console.log("New Cookie value:" + header.value);
          return true;
      }
      return false;
  });
}, {urls: [ "<all_urls>" ]},['requestHeaders']);

有关在 Firefox 中测试和开发 WebExtensions 的一般说明

使用Firefox Developer Edition ,或Firefox Nightly :
WebExtensions API 仍在开发中。每个版本的 Firefox 的工作原理都有所改进。目前,您可能最好使用 Firefox Developer Edition 来开发和测试您的 WebExtension 附加组件。 ,或Firefox Nightly 。您还应该仔细记下您想要使用的功能所需的 Firefox 版本。此信息包含在 MDN 文档页面的“浏览器兼容性”部分中。

使用 Browser Console :
您应该使用 Browser Consoleview console.log() output from you WebExtension background scripts 。如果您一直在查看浏览器控制台,您就会看到错误消息。您仍然必须弄清楚错误消息的含义。但是,您至少可以搜索更多内容并包含在您的问题中

关于javascript - 从 chrome.webRequest.onBeforeSendHeaders 中提取 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39070904/

相关文章:

下载附件的javascript事件?

bash - 如何从 Chrome 复制 cookie?

Javascript cookie 不适用于表单提交

javascript - linux 共享库可以打开、显示和 printf 到终端吗?

javascript - 使用 firefox 启动在系统 PATH 中找到的外部程序

javascript - Chart.js 在工具提示上添加百分号

javascript - 未捕获的类型错误 : Object #<HTMLFormElement> has no method onkeydown

php - 在 URL 中保留一个 GET 变量

javascript - 如何检测 firefox 扩展中的 firefox 启动事件?

javascript - 如何让 Greasemonkey 脚本同时在@run-at document-start 和@run-at document-end 运行?