chrome.webRequest not working?

后端 未结 4 1039
滥情空心
滥情空心 2020-12-24 07:31

I\'m trying to implement the chrome.webRequest API in my extension but for some reason it\'s just not working no matter what I do. Can someone post an example of usage? or c

4条回答
  •  旧巷少年郎
    2020-12-24 08:10

    Well for an example of usage I can give you this working code. I wrote it this way because the other way seems backwards to me but that is just my personal preference, they should both work the same.

    Manifest

    {
      "name": "Chrome webrequest test",
      "version": "0.1",
      "description": "A test for webrequest",
      "manifest_version": 2,
      "permissions": [
        "","webRequest","webRequestBlocking"
      ],
      "background": {
        "scripts": ["bgp.js"],
        "persistent": true
      }
    }
    

    bgp.js

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

提交回复
热议问题