declarativeNetRequest doesn't provide a way to match requests based on the value of a
request header. You can open a new issue in
https://crbug.com suggesting to implement it. Currently I see only issues about conditional handling of
response headers.
Assuming these requests are made by a script running in the extension process (i.e. not a content script) like the background/popup/options script, the workaround is to use initiatorDomains to strip the header on all requests made by the extension.
If your extension id is pinned to a constant value, you can do it in rules.json:
[{
"id": 1,
"action": {
"type": "modifyHeaders",
"requestHeaders": [{ "header": "Origin", "operation": "remove" }]
},
"condition": {
"initiatorDomains": ["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"],
"requestMethods": ["post"],
"resourceTypes": ["xmlhttprequest"]
}
}]
Otherwise this must be done in the extension script like the background script using chrome.declarativeNetRequest.updateDynamicRules and not inside rules.json because json doesn't provide a placeholder for the current extension id.
chrome.runtime.onInstalled.addListener(async () => {
const rules = [{
id: 1,
action: {
type: 'modifyHeaders',
requestHeaders: [{ header: 'Origin', operation: 'remove' }],
},
condition: {
initiatorDomains: [chrome.runtime.id],
requestMethods: ['post'],
resourceTypes: ['xmlhttprequest'],
},
}];
await chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: rules.map(r => r.id),
addRules: rules,
});
});
Another workaround is to force-install it via policies so that it can keep using webRequestBlocking with the old MV2 code.