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

javascript - webRequest listener doesn't see headers like 'cookie', 'referer', '

programmeradmin3浏览0评论

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};
    }
}

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.

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

Any ideas? Thanks in advance!

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};
    }
}

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.

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

Any ideas? Thanks in advance!

Share Improve this question edited Sep 15, 2020 at 7:02 woxxom 74k14 gold badges156 silver badges160 bronze badges asked Nov 6, 2018 at 15:58 Roy MilloRoy Millo 1031 silver badge5 bronze badges 1
  • @wOxxOm isn't Set-Cookie a response header? We're trying to modify request headers – Roy Millo Commented Nov 6, 2018 at 16:17
Add a ment  | 

1 Answer 1

Reset to default 12

According to this https://developer.chrome./extensions/webRequest

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

    • Accept-Language
    • Accept-Encoding
    • Referer
    • Cookie
  • since Chrome 79:

    • Origin
    • CORS preflight requests

Response headers for other listeners like onHeadersReceived:

  • since Chrome 72:
    • Set-Cookie
    • any header you want to modify before CORB is applied
  • since Chrome 79:
    • CORS preflight responses

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

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 patible listener:

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论