Can I modify outgoing request headers with a Chrome Extension?

后端 未结 4 1084
清歌不尽
清歌不尽 2020-12-02 17:10

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

I want to intercept HTTP requests with a Chrome Extension

4条回答
  •  暖寄归人
    2020-12-02 17:17

    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:

    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: ['']},
      [ 'blocking', 'requestHeaders', 'extraHeaders']
    );
    

    For more info, check the documentation Screenshot from the documentation https://developer.chrome.com/extensions/webRequest#life_cycle_footnote

提交回复
热议问题