javascript - Chrome WebAPIchrome.webRequest.onBeforeSendHeaders 不更改传出请求中的参数

标签 javascript google-chrome google-chrome-extension

我尝试将 IP 添加到 requestHeader 中的 X-Forwarded-For 参数,但我的 Chrome API 示例代码并未执行此操作。

我有以下代码:

var requestFilter = {
    urls: [ "<all_urls>" ]
  },

  extraInfoSpec = ['requestHeaders', 'blocking'],

  handler = function( details ) {

var headers = details.requestHeaders,
  blockingResponse = {};

  var isXForwardedForSet = false;

for (var i = 0, l = headers.length; i < l; ++i) {
  if (headers[i].name === 'X-Forwarded-For') {
      headers[i].value = "42.104.0.0";
      isXForwardedForSet = true;
      break;
  }
}

if (!isXForwardedForSet) {
    headers.push({
        name: "X-Forwarded-For",
        value: "42.104.0.0"
    });
}

blockingResponse.requestHeaders = headers;

   return blockingResponse;

  };

chrome.webRequest.onBeforeSendHeaders.addListener( handler, requestFilter,      extraInfoSpec );

最佳答案

与代理相关的 header 可能会被忽略。

引用docs :

Note that the web request API presents an abstraction of the network stack to the extension. Internally, one URL request can be split into several HTTP requests (for example to fetch individual byte ranges from a large file) or can be handled by the network stack without communicating with the network. For this reason, the API does not provide the final HTTP headers that are sent to the network. For example, all headers that are related to caching are invisible to the extension.

没有被忽略的 header 的完整列表,但文档中列出了一些 header 。常识性标准:如果它代表有关网络的知识(缓存、代理等),则 API 无法触及它。

关于javascript - Chrome WebAPIchrome.webRequest.onBeforeSendHeaders 不更改传出请求中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29809200/

相关文章:

javascript - 如何检查我的谷歌分析是否正在使用 chrome 扩展程序?

javascript - 根据滚动淡入淡出

javascript - javascript对html进行更改后如何获取html源(无需浏览器)

css - 线性渐变在 Chrome 中不起作用

html - 为什么滚动条消失了?

css - 带有指针事件和鼠标滚动的 Chrome 错误?

javascript - 使用 chrome.identity.launchWebAuthFlow 时如何防止弹出窗口提前关闭?

windows - 如何在 firefox 和 chrome 中安装解压的扩展?

javascript - 使用 lodash 链接进行字符串操作

javascript - 如何清空div的内容