chrome.webRequest不工作​​? [英] chrome.webRequest not working?

查看:261
本文介绍了chrome.webRequest不工作​​?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在扩展中实现chrome.webRequest API,但由于某种原因,无论我做什么,它都无法正常工作。有人可以发布使用示例吗?或纠正我的错误?基本上我试图做的是拦截从响应收到的头。

这是一个onBeforeSendHeaders的实现,但我也想使用OnHeadersRecieved以及

  var requestFilter = {
网址:[< all_urls> ]
},
//'extraInfoSpec'参数修改Chrome调用
//监听器函数的方式。 'requestHeaders'确保'details'
//对象有一个名为'requestHeaders'的关键字,包含头部,
//和'blocking'确保函数返回的对象是
/ /用于覆盖头文件
extraInfoSpec = ['requestHeaders','blocking'],
// Chrome将调用您的侦听器函数来响应每个
// HTTP请求
handler = function(details){
alert(details);
var headers = details.requestHeaders,
lockResponse = {};

//每个头部参数都存储在一个数组中。由于Chrome
//不能保证这个数组的内容/顺序,所以
//你必须遍历它才能找到
//'User-Agent'元素
for(var i = 0,l = headers.length; i< l; ++ i){
if(headers [i] .name =='User-Agent'){
headers [i] .value ='>>>您的新用户代理字符串位于<<<
休息;
}
//如果你想修改其他头文件,这是
//做的地方。删除'break;'语句并添加更多
//条件或在'headers [i] .name'
}

blockingResponse上使用'switch'语句。 requestHeaders =头文件;
返回blockingResponse;
};

chrome.webRequest.onBeforeSendHeaders.addListener(handler,requestFilter,extraInfoSpec);

这是我的清单文件:

 {
background_page:iRBackground.html,
browser_action:{
default_icon:Off.png,
popup:iRMenu.html
},
content_scripts:[{
js:[Content.js],
matches: [http:// * / *],
run_at:document_start
}],
description:***,
图标:{
128:On128x128.png,
16:On.png,
48:On48x48.png
},
key:****,
manifest_version:2,
name:***,
permissions:[tabs通知,unlimitedStorage,webRequest,webRequestBlocking,< all_urls>],
update_url:*** / Chrome / UpdateVersion.xml,
version:1.3
}

我从Chrome获得的错误是: Uncaught TypeError:无法读取属性'onBeforeSendHeaders'o f undefined



任何人都可以看到任何错误?谢谢

解决方案

好的,我可以给你这个工作代码的例子。我是这样写的,因为另一种方式似乎倒退了,但这只是我个人的偏好,它们应该都是相同的。


$ b

Manifest

  {
name:Chrome webrequest test,
version:0.1 ,
description:webrequest测试,
manifest_version:2,
permissions:[
< all_urls>,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 = {};

//每个头部参数都存储在一个数组中,因为Chrome
//不保证这个数组的内容/顺序
//你必须迭代e通过它找到
//'User-Agent'元素
(var i = 0,l = headers.length;我<升; ++ i){
if(headers [i] .name =='User-Agent'){
headers [i] .value ='>>>您的新用户代理字符串位于<<<
console.log(headers [i] .value);
休息;
}
//如果你想修改其他头文件,这是
//做的地方。删除'break;'语句并添加更多
//条件或在'headers [i] .name'
}

blockingResponse上使用'switch'语句。 requestHeaders =头文件;
返回blockingResponse;
},
{url:[< all_urls> ]},[ requestHeaders’,阻断]);


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 correct my mistakes? Basically what I'm trying to do is to intercept the recieved headers from a response.

This is an implementation for onBeforeSendHeaders but I'd like to use OnHeadersRecieved as well :

var requestFilter = {
    urls: [ "<all_urls>" ]
  },
  // The 'extraInfoSpec' parameter modifies how Chrome calls your
  // listener function. 'requestHeaders' ensures that the 'details'
  // object has a key called 'requestHeaders' containing the headers,
  // and 'blocking' ensures that the object your function returns is
  // used to overwrite the headers
  extraInfoSpec = ['requestHeaders','blocking'],
  // Chrome will call your listener function in response to every
  // HTTP request
  handler = function( details ) {
    alert(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 <<<';
        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;
  };

chrome.webRequest.onBeforeSendHeaders.addListener( handler, requestFilter, extraInfoSpec );

this is my manifest file:

    {
   "background_page": "iRBackground.html",
   "browser_action": {
      "default_icon": "Off.png",
      "popup": "iRMenu.html"
   },
   "content_scripts": [ {
      "js": [ "Content.js" ],
      "matches": [ "http://*/*" ],
      "run_at": "document_start"
   } ],
   "description": "***",
   "icons": {
      "128": "On128x128.png",
      "16": "On.png",
      "48": "On48x48.png"
   },
   "key": "****",
   "manifest_version": 2,
   "name": "***",
   "permissions": [ "tabs", "notifications", "unlimitedStorage", "webRequest", "webRequestBlocking", "<all_urls>"],
   "update_url": "***/Chrome/UpdateVersion.xml",
   "version": "1.3"
}

the error I get from Chrome is: Uncaught TypeError: Cannot read property 'onBeforeSendHeaders' of undefined

Anyone see anything wrong??? thanks

解决方案

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

这篇关于chrome.webRequest不工作​​?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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