0

I'm trying to let onBeforeRequest trigger but it doesn't trigger once.

The background page console displays an error:

Invalid value for argument 1. Expected 'object' but got 'array'

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"]);

What am I doing wrong or am I just expecting something onBeforeRequest isn't supposed to do? For example I expect the following:

  1. I enter url into address bar
  2. I press enter
  3. onBeforeRequest triggers before website is shown
  4. User gets message that website is blocked
Community
  • 1
  • 1
Stanko
  • 4,275
  • 3
  • 23
  • 51

1 Answers1

1

According to the documentation:

In addition to specifying a callback function, you have to specify a filter argument

chrome.webRequest.onBeforeRequest.addListener(
    function(info) {
        console.log(info);
        return {cancel: true};
    }, {
        urls: ['<all_urls>'],
    },
    ['blocking']
);
wOxxOm
  • 65,848
  • 11
  • 132
  • 136