最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Chrome web request blocking not working - Stack Overflow

programmeradmin1浏览0评论

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": [
  "/",
  "activeTab",
  "storage",
  "webRequest",
  "webRequestBlocking"
  ],

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

background.js:

chrome.webRequest.onBeforeRequest.addListener(
    function(details) { return {cancel: true}; },
    {urls: [""]},
    ["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.

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./",
  "activeTab",
  "storage",
  "webRequest",
  "webRequestBlocking"
  ],

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

background.js:

chrome.webRequest.onBeforeRequest.addListener(
    function(details) { return {cancel: true}; },
    {urls: ["https://twitter."]},
    ["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.

Share Improve this question asked Jun 8, 2017 at 1:47 full_retardfull_retard 311 silver badge2 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

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 ponent. 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.' 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./*"]
    }, ["blocking"]
);

Problem #2: No host permission for twitter.

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./*",
    "https://*.twitter./*",
    "activeTab",
    "storage",
    "webRequest",
    "webRequestBlocking"
  ],

  "background": {
    "scripts": ["background.js"],
    "persistent": true
  }
}
发布评论

评论列表(0)

  1. 暂无评论