webRequest侦听器看不到标题,例如“ cookie”,“ referer”,“ origin” [英] webRequest listener doesn't see headers like 'cookie', 'referer', 'origin'

查看:161
本文介绍了webRequest侦听器看不到标题,例如“ cookie”,“ referer”,“ origin”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们编写了一个Chrome扩展程序,该扩展程序使用onBeforeSendHeaders事件向每个Web请求添加一个cookie:

We wrote a Chrome-extension that, using the onBeforeSendHeaders event, adds a cookie to each web request:

chrome.webRequest.onBeforeSendHeaders.addListener(addCookie, {
    urls: ["<all_urls>"]
}, ["blocking", "requestHeaders"]);

function addCookie(details) {
    if (details.url.match(/ourWebsite/)) {
        details.requestHeaders.forEach(function (requestHeader) {
            if (requestHeader.name.toLowerCase() === "cookie") {
                //Code that adds a cookie with a value
            }
        });
        return {requestHeaders: details.requestHeaders};
    }
}

在所有人的Chrome(我自己的Chrome)上都可以正常运行。在调试扩展时,我注意到 details.requestHeaders 数组没有 cookie 标头(这始终是false: requestHeader.name.toLowerCase()=== cookie )。

It works fine on everyone's Chrome but my own. While debugging the extension, I noticed that the details.requestHeaders array doesn't have the cookie header (this is always false: requestHeader.name.toLowerCase() === "cookie").

我的第一个念头是另一个扩展名正与我们的扩展名混淆,因此我尝试了隐身(不允许其他扩展名),但没有用。

My first thought was another extension is messing up with ours, so I tried in incognito (where no other extensions are allowed) but it didn't work.

在扩展程序清单中,在权限下,我们同时具有 cookies和 webRequest。

In the extension's manifest we have both "cookies" and "webRequest" under permissions.

有什么想法吗?

推荐答案

根据此 https://developer.chrome.com/extensions/webRequest



  • 从Chrome 72开始,未提供以下请求标头,并且如果未在opt_extraInfoSpec中指定'extraHeaders',则无法修改或删除以下请求标头:

  • Starting from Chrome 72, the following request headers are not provided and cannot be modified or removed without specifying 'extraHeaders' in opt_extraInfoSpec:


  • 接受语言

  • 接受编码

  • 推荐人

  • Cookie

  • Accept-Language
  • Accept-Encoding
  • Referer
  • Cookie

自Chrome 79起:

since Chrome 79:


  • 来源

  • CORS飞行前请求



其他监听器的响应标头,例如onHeadersReceived:

Response headers for other listeners like onHeadersReceived:


  • 从Chrome 72开始:
    • since Chrome 72:
      • Set-Cookie
      • any header you want to modify before CORB is applied

      • CORS飞行前响应

      因此,您应该添加 extraHeaders对于webRequest侦听器的第三个参数,对于您的示例,该参数应为 [ blocking, requestHeaders, extraHeaders]

      So you should add "extraHeaders" to the third parameter of the webRequest listener and it should be ["blocking", "requestHeaders", "extraHeaders"] for your example.

      请注意,它不会在72之前的旧版Chrome中运行,而Chrome并不了解 extraHeaders ,因此您可以使用以下技巧通用兼容的侦听器:

      Note that it won't run in old pre-72 Chrome, which doesn't know about extraHeaders, so you can use the following trick to have a universally compatible listener:

      chrome.webRequest.onBeforeSendHeaders.addListener(
        addCookie,
        {urls: ["<all_urls>"]},
        ["blocking", "requestHeaders",
         chrome.webRequest.OnBeforeSendHeadersOptions.EXTRA_HEADERS].filter(Boolean)]
      );
      

      这篇关于webRequest侦听器看不到标题,例如“ cookie”,“ referer”,“ origin”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆