我可以使用 Chrome 扩展程序修改传出请求标头吗? [英] Can I modify outgoing request headers with a Chrome Extension?

查看:24
本文介绍了我可以使用 Chrome 扩展程序修改传出请求标头吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开发者指南中找不到答案,虽然我可能没有找对地方.

I can't see an answer to this in the Developer's Guide, though maybe I'm not looking in the right place.

我想使用 Chrome 扩展程序拦截 HTTP 请求,然后转发它,可能使用新的/不同的 HTTP 标头 - 我该怎么做?

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?

推荐答案

PS:我是这个扩展的作者,所以你可以责怪我的任何你不'不喜欢:)

PS: I am the author of this extension so you can blame me for anything you don't like :)

当 OP 提出这个问题时,这当然是不可能的,但不久之后 Chrome 发布了实验性的 WebRequest API.但现在它们已正式包含在 Chrome 扩展程序中.您可以使用它在 Chrome 中修改请求和响应标头.

It was certainly not possible when OP asked the question but soon later Chrome released experimental WebRequest API. But now they have been included officially in Chrome Extension. You can use it modify request and response headers in Chrome.

看这个例子:

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
);

如果您想使用 Chrome 扩展程序,您可以使用 Requestly 允许您可以根据需要修改请求和响应标头.看看这个快照:

If you want to use Chrome Extension, you can use Requestly which allows you to modify request and response headers as you wish. Have a look at this snapshot:

这篇关于我可以使用 Chrome 扩展程序修改传出请求标头吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆