我正在尝试让onBeforeRequest触发,但不会触发一次。

background page console显示错误:


  参数1的值无效。应为“对象”,但得到“数组”


manifest.json:

{
  "name": "Blocker",
  "version": "1.0",
  "description": "Blocks all websites",
  "permissions": ["webRequest", "webRequestBlocking", "<all_urls>"],
  "background": {
    "scripts": ["background.js"]
  },

  "manifest_version": 2
}


background.js:

chrome.webRequest.onBeforeRequest.addListener(
  function(info) {
    console.log("TRIGGERED")
    return {cancel: true};
  },
  // extraInfoSpec
  ["blocking"]);


我在做什么错或者我只是在期待onBeforeRequest不应该做的事情?例如,我期望以下内容:


我在网址列输入网址
我按回车
onBeforeRequest在显示网站之前触发
用户收到网站被阻止的消息

最佳答案

根据documentation


  除了指定回调函数外,还必须指定过滤器参数


chrome.webRequest.onBeforeRequest.addListener(
    function(info) {
        console.log(info);
        return {cancel: true};
    }, {
        urls: ['<all_urls>'],
    },
    ['blocking']
);

07-26 05:42
查看更多