webRequest listener doesn't see headers like 'cookie', 'referer', 'origin'

后端 未结 1 1932
情话喂你
情话喂你 2020-12-12 01:58

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

chrome.webRequest.onBeforeSendHeaders.addListener         


        
相关标签:
1条回答
  • 2020-12-12 02:40

    According to this https://developer.chrome.com/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 compatible listener:

    chrome.webRequest.onBeforeSendHeaders.addListener(
      addCookie,
      {urls: ["<all_urls>"]},
      ["blocking", "requestHeaders",
       chrome.webRequest.OnBeforeSendHeadersOptions.EXTRA_HEADERS].filter(Boolean)]
    );
    
    0 讨论(0)
提交回复
热议问题