We wrote a Chrome-extension that, using the onBeforeSendHeaders event, adds a cookie to each web request:
chrome.webRequest.onBeforeSendHeaders.addListener
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)]
);