如何使用chrome扩展中的WebRequest API修改主机字段

时间:2018-06-09 07:23:20

标签: javascript google-chrome google-chrome-extension chrome-webrequest

我正在使用以下示例WebRequest API代码。我检查了收到的详细信息变量。它没有Host字段。如何修改请求的主机字段?我可以自己在chrome扩展中编写完整的HTTP请求吗?

chrome.webRequest.onBeforeSendHeaders.addListener(function(details){
  console.log(JSON.stringify(details));
  var headers = details.requestHeaders,
  blockingResponse = {};

  // Each header parameter is stored in an array. Since Chrome
  // makes no guarantee about the contents/order of this array,
  // you'll have to iterate through it to find for the
  // 'User-Agent' element
  for( var i = 0, l = headers.length; i < l; ++i ) {
    //console.log(headers[i].name);
    if( headers[i].name == 'User-Agent' ) {
      headers[i].value = '>>> Your new user agent string here <<<';
      console.log(headers[i].value);
      break;
    }
    // If you want to modify other headers, this is the place to
    // do it. Either remove the 'break;' statement and add in more
    // conditionals or use a 'switch' statement on 'headers[i].name'
  }

  blockingResponse.requestHeaders = headers;
  return blockingResponse;
},
{urls: [ "http://*/*" ]},['requestHeaders','blocking']);

0 个答案:

没有答案