我正在尝试使用我的 Chrome 扩展程序修改 referer-policy header ,但它不会影响响应 header 。
Manifest.json:
{
"name": "My Example Extension",
"version": "1.0",
"description": "",
"permissions": [
"webRequest",
"webRequestBlocking",
"*://*/*"
],
"background": {
"scripts": ["background.js"],
"persistent": true
},
"manifest_version": 2
}
background.js:
chrome.webRequest.onHeadersReceived.addListener(details => {
let header = details.responseHeaders.find(e => e.name.toLowerCase() === 'referrer-policy');
// Check if the header has been defined already
if (typeof header !== 'undefined') {
console.log ('Modifying header');
header.value = 'strict-origin';
}
else {
details.responseHeaders.push({ name: 'referrer-policy', value: 'strict-origin' });
}
return {responseHeaders: details.responseHeaders};
}, {urls: ["*://*/*"]}, ['blocking', 'responseHeaders']);
我添加了调试输出,显示代码相应地修改或添加了 header ,但浏览器本身没有任何影响。
最佳答案
在尝试修改扩展中的响应 header 时,我正在编写完全相同的代码。
// manifest.json
"permissions": [
"webRequest", "webRequestBlocking", "https://developer.chrome.com/*"
]
// background.js
chrome.webRequest.onHeadersReceived.addListener(
function(details) {
const newHeader = {name:"foo", value:"bar"};
const responseHeaders = details.responseHeaders.concat(newHeader);
return { responseHeaders };
},
// filters
{
urls: ["https://developer.chrome.com/*"],
},
// extraInfoSpec
["blocking","responseHeaders"]
);
它根本不起作用。
直到我将最后一个参数更改为:
// extraInfoSpec
["blocking","responseHeaders", "extraHeaders"]
终于成功了。
通过重新访问文档,在 Life cycle of requests 的最后几段中部分:
Starting from Chrome 79, ... On the other hand, response header modifications do not work to deceive CORS checks. If you need to deceive the CORS protocol, you also need to specify '
extraHeaders
' for the response modifications.
关于google-chrome - 修改 Chrome 扩展中的响应 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53172613/