问题描述
我正在制作一个chrome扩展名,用于记录请求标头.
I am making a chrome extension which records request headers.
在我的background.js文件中,我有这段代码
In my background.js file I have this code
chrome.webRequest.onSendHeaders.addListener(function(res){
res.requestHeaders.forEach(header => {
headers.push(header)
})
}, {urls : ["<all_urls>"]})
但是res.requestHeaders返回未定义(无法读取每个未定义的属性)
But res.requestHeaders returns undefined (cannot read property forEach of undefined)
推荐答案
应在addListener的第三个参数中指定它:
It should be specified in the third parameter of addListener:
chrome.webRequest.onSendHeaders.addListener(
fn,
{urls: ['<all_urls>']},
['requestHeaders']);
要修改响应,您需要在参数中添加阻止",并在清单的权限中添加"webRequestBlocking",请参见网络请求文档以获取更多信息.
To modify the response you'll need to add "blocking" in the parameter and "webRequestBlocking" in manifest's permissions, see the webRequest documentation for more info.
要在现代版本的Chrome中处理特殊的标头(cookie
,referer
等),您还需要指定extraheaders
,如果要支持旧版本的Chrome,请按以下步骤进行操作:
To process special headers (cookie
, referer
, etc.) in modern versions of Chrome you'll need to specify extraheaders
as well, and if you want to support old versions of Chrome do it like this:
chrome.webRequest.onSendHeaders.addListener(fn, {
urls: ['<all_urls>'],
}, [
'blocking',
'requestHeaders',
chrome.webRequest.OnSendHeadersOptions.EXTRA_HEADERS,
].filter(Boolean));
这篇关于为什么requestHeaders未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!