3

I'm trying to use the webrequest api and I'm having trouble using it to block a website.

manifest.json

{
  "manifest_version": 2,

  "name": "blocktwitter",
  "description": "block twitter",
  "version": "1.0",

  "permissions": [
  "https://ajax.googleapis.com/",
  "activeTab",
  "storage",
  "webRequest",
  "webRequestBlocking"
  ],

"background": {
"scripts": ["background.js"],
"persistent": true
  }
}

background.js:

chrome.webRequest.onBeforeRequest.addListener(
    function(details) { return {cancel: true}; },
    {urls: ["https://twitter.com"]},
    ["blocking"]);

I copied the copy + pasted the url from twitter, and copied the code from the docs, but it's still not working. I'm not sure what I'm doing wrong. Help would be appreciated.

full_retard
  • 31
  • 1
  • 2

1 Answers1

7

You have two problems.

Problem #1: Invalid match pattern

The first problem is that the URL you are passing to chrome.webRequest.onBeforeRequest.addListener() is not a valid match pattern. Match patterns require a path component. If you had looked at the console for your background page/script you would have seen the following error:

_generated_background_page.html:1 Unchecked runtime.lastError while running webRequestInternal.addEventListener: 'https://twitter.com' is not a valid URL pattern.

Your background.js should be something like:

chrome.webRequest.onBeforeRequest.addListener(
    function(details) {
        console.log('In webRequest');
        return {cancel: true};
    }, {
        urls: ["https://*.twitter.com/*"]
    }, ["blocking"]
);

Problem #2: No host permission for twitter.com

You need to have permission for the host that you are blocking. You have to declare the host in your permissions array in manifest.json. Something like:

{
  "manifest_version": 2,

  "name": "blocktwitter",
  "description": "block twitter",
  "version": "1.0",

  "permissions": [
    "https://ajax.googleapis.com/*",
    "https://*.twitter.com/*",
    "activeTab",
    "storage",
    "webRequest",
    "webRequestBlocking"
  ],

  "background": {
    "scripts": ["background.js"],
    "persistent": true
  }
}
Makyen
  • 31,849
  • 12
  • 86
  • 121
  • I need your help reagarding problem...i did the same as u wrote above but website is not getting block – twana eng Apr 29 '20 at 15:01
  • @twanaeng There's little or nothing that anyone can do without seeing your code. I suggest you create a new question and include enough code in the question such that readers can duplicate the problem you're having. Definitely include your *manifest.json*. – Makyen Apr 29 '20 at 15:05
  • i need your suggestion can we change the window desktop wallpaper thorugh extension ? – twana eng Apr 29 '20 at 15:28
  • That's going to depend on what you mean by "window desktop wallpaper". If you mean the wallpaper that Windows uses for the desktop, the answer is: not without installing a program which is designed to both make that change and communicate with your extension, and then communicating with that program. – Makyen Apr 29 '20 at 15:32
  • ok and there are some apis which says This API is only available to extensions installed by enterprise policy. What does it mean? – twana eng Apr 29 '20 at 15:45
  • @twanaeng You need to be writing these up as actual questions, not asking them in comments. – Makyen Apr 29 '20 at 15:46
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/212798/discussion-between-twana-eng-and-makyen). – twana eng Apr 29 '20 at 15:48