如何使用“chrome.webRequest.onBeforeRequest”?

时间:2012-04-23 08:31:58

标签: javascript json google-chrome-extension

Chrome一直说:“无法读取未定义的'onBeforeRequest'属性”。我无法弄清楚原因。

background.html:

<html>
  <head>
    <script type="text/javascript">// <![CDATA[     
         try 
            {
                chrome.webRequest.onBeforeRequest.addListener(function(e){alert("onBeforeRequest")},{urls: ["http://*/*", "https://*/*"]}, ["blocking"]) ;
                 }
            catch (ErrorMessage)
            {
              alert('page:'+ErrorMessage) ;
            }       


    // ]]></script>
    </head>
</html>

的manifest.json:

{
  "name": "first extension",
  "version": "1.0",
  "description": "first extension",
  "browser_action": {
    "default_icon": "icon.gif",
    "popup": "popup.html"
  },

  "permissions":
  ["tabs", "chrome.webRequest", "webNavigation", "management", "http://*/*", "https://*/*"],
  "background_page": "background.html"
}

1 个答案:

答案 0 :(得分:2)

webRequest API请求权限的正确方法是webRequest(不含chrome.)。

"permissions": [
    "tabs",
    "webRequest",
    "webNavigation",
    "management",
    "http://*/*",
    "https://*/*"
],