简体   繁体   English

我可以使用 Chrome 扩展程序修改传出请求标头吗?

[英]Can I modify outgoing request headers with a Chrome Extension?

I can't see an answer to this in the Developer's Guide , though maybe I'm not looking in the right place.我在Developer's Guide中看不到这个问题的答案,但也许我没有找对地方。

I want to intercept HTTP requests with a Chrome Extension, and then forward it on, potentially with new/different HTTP headers - how can I do that?我想使用 Chrome 扩展拦截 HTTP 请求,然后转发它,可能使用新的/不同的 HTTP 标头 - 我该怎么做?

PS: I am the author of this extension so you can blame me for anything you don't like :) PS:我是这个扩展的作者所以你可以责怪我任何你不喜欢的东西:)

It was certainly not possible when OP asked the question but soon later Chrome released experimental WebRequest API .当 OP 提出这个问题时,这当然是不可能的,但不久之后 Chrome 发布了实验性WebRequest API But now they have been included officially in Chrome Extension.但现在它们已正式包含在 Chrome 扩展程序中。 You can use it modify request and response headers in Chrome.您可以使用它来修改 Chrome 中的请求和响应标头。

Look at this example:看这个例子:

chrome.webRequest.onBeforeSendHeaders.addListener(
  function(details) {
    for (var i = 0; i < details.requestHeaders.length; ++i) {
      if (details.requestHeaders[i].name === 'User-Agent') {
        details.requestHeaders.splice(i, 1);
        break;
      }
    }
    return { requestHeaders: details.requestHeaders };
  },
  {urls: ['<all_urls>']},
  ['blocking', 'requestHeaders' /* , 'extraHeaders' */]
  // uncomment 'extraHeaders' above in case of special headers since Chrome 72
  // see https://developer.chrome.com/extensions/webRequest#life_cycle_footnote
);

If you want to use Chrome Extension, you can use Requestly which allows you to modify request and response headers as you wish.如果您想使用 Chrome 扩展程序,您可以使用Requestly ,它允许您根据需要修改请求和响应标头。 Have a look at this snapshot:看看这个快照:

标题规则

chrome 17 支持修改请求标头 ( https://developer.chrome.com/extensions/webRequest )。

您正在寻找正确的地方,但拦截 HTTP 请求尚不存在,但扩展团队知道这是一个流行的请求,并希望在不久的将来某个时候使用它。

Keep in mind that starting from chrome 72, some headers are not allowed unless you add extraHeaders in opt_extraInfoSpec So the above example in @sachinjain024's answer will look something like this:请记住,从铬72开始,一些头不准,除非你加extraHeadersopt_extraInfoSpec所以在@ sachinjain024的回答会是这个样子上面的例子:

chrome.webRequest.onBeforeSendHeaders.addListener(
  function(details) {
    for (var i = 0; i < details.requestHeaders.length; ++i) {
      if (details.requestHeaders[i].name === 'User-Agent') {
        details.requestHeaders.splice(i, 1);
        break;
      }
    }
    return { requestHeaders: details.requestHeaders };
  },
  {urls: ['<all_urls>']},
  [ 'blocking', 'requestHeaders', 'extraHeaders']
);

For more info, check the documentation Screenshot from the documentation https://developer.chrome.com/extensions/webRequest#life_cycle_footnote有关更多信息,请查看文档https://developer.chrome.com/extensions/webRequest#life_cycle_footnote 中的文档截图

You could install ModHeader extension and add headers:您可以安装ModHeader扩展并添加标头:

在此处输入图片说明

For extensions using manifest version 3 , you can no longer use chrome.webRequest.onBeforeSendHeaders.* .对于使用清单版本 3的扩展,您不能再使用chrome.webRequest.onBeforeSendHeaders.* The alternative is chrome.declarativeNetRequest另一种方法是chrome.declarativeNetRequest

Make the following changes in your manifest.json :manifest.json中进行以下更改:

{
  ...
  "manifest_version": 3,
  "background": {
    "service_worker": "background.js"
  },
  "host_permissions": ["<all_urls>"],
  "permissions": [
    "declarativeNetRequest"
  ],
  ...
}
  • "<all_urls>" is for modifying all outgoing urls's headers. "<all_urls>"用于修改所有传出 url 的标题。 Restrict this for your scope of your work限制您的 scope 的工作

Make the following changes in your background.js :在您的background.js中进行以下更改:

// ...

const MY_CUSTOM_RULE_ID = 1

chrome.declarativeNetRequest.updateDynamicRules({
    removeRuleIds: [MY_CUSTOM_RULE_ID],
    addRules: [
        {
            id: MY_CUSTOM_RULE_ID,
            priority: 1,
            action: {
                type: "modifyHeaders",
                requestHeaders: [
                    {
                        operation: "set",
                        header: "my-custom-header",
                        value: "my custom header value"
                    }
                ]
            },
            condition: {
                "resourceTypes": ["main_frame", "sub_frame"]
            },
        }
    ],
});

Result结果

在此处输入图像描述

Read the docs https://developer.chrome.com/docs/extensions/reference/declarativeNetRequest/阅读文档https://developer.chrome.com/docs/extensions/reference/declarativeNetRequest/

You can use WebRequest API which is now deprecated it allows you to modify request/response headers.您可以使用现已弃用的WebRequest API,它允许您修改请求/响应标头。 You can upgrade your extension to Manifest V3 to be able to use DeclativeNetRequest which also supports modifying request/response headers.您可以将您的扩展升级到Manifest V3 ,以便能够使用DeclativeNetRequest ,它也支持修改请求/响应标头。

Or you can install Inssman chrome extension.或者你可以安装Inssman chrome 扩展。 It allows you to modify HTTP(S) request/response headers, reqirect and block request, return custom data like HTML/CSS/JS/JSON and more.它允许您修改 HTTP(S) 请求/响应标头、请求和阻止请求、返回自定义数据(如 HTML/CSS/JS/JSON 等)。 And it is open source project而且是开源项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Extension Google Chrome中查看Flash的请求标头? - How I can to see request headers of Flash in Extension Google Chrome? Chrome扩展程序可基于公共IP修改请求标头 - Chrome Extension to Modify request headers based on Public IP 如何根据 Chrome 扩展中的用户选项修改 http 请求标头? - How to modify http request headers based on user options in a Chrome extension? 使用 chrome 45 修改带有扩展程序的应用程序的标题 - Modify headers of a app with extension using chrome 45 如何通过Chrome扩展程序修改请求正文 - How to modify request bodies by chrome extension Chrome扩展程序:如何将资源请求重定向到文件系统:url? - Chrome Extension: how can I redirect a resource request to a filesystem: url? 如何在 twitter 上运行的 chrome 扩展中发出 POST 请求? - How can I make a POST request in a chrome extension that runs on twitter? 如何在 service worker chrome 扩展中发出 http 请求? - How can I make an http request in the service worker chrome extension? Chrome扩展程序chrome.webRequest API-请求标题中未包含一些http请求标题 - Chrome extension chrome.webRequest API - Some http request headers not coming in request headers 如何在Chrome扩展程序中的onHeadersReceived事件中访问请求标头 - How to access request headers in onHeadersReceived event in Chrome extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM