CHROME WebRequest API示例错误:"onBeforeRequest"只能用于扩展进程

Hua*_*ang 5 webrequest google-chrome-extension

我尝试测试WebRequestAPI的示例,但抛出错误:

"onBeforeRequest"只能用于扩展进程.manifest.json的:

{
    "name": "example",
   "version": "1.0",
  "permissions": [
    "experimental",
    "http://*/*", "https://*/*"
  ],
    "content_scripts": [ {
      "js": [ "foo.js" ],
      "matches": [ "http://*/*", "https://*/*" ],
      "run_at": "document_start"
   } ]
}
Run Code Online (Sandbox Code Playgroud)

foo.js 正是这个例子1

Mih*_*ita 11

Chrome扩展程序功能(包括webRequest API)无法在内容脚本中使用(foo.js在您的示例中).如果您希望从内容脚本中使用webRequest,则可以使用消息传递功能与扩展的后台页面进行通信.后台页面可以直接使用webRequest,并将响应(如果有)中继回内容脚本.