本文介绍了为什么requestHeaders未定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在制作一个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中处理特殊的标头(cookiereferer等),您还需要指定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未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 09:45