Reputation: 31
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.
Upvotes: 3
Views: 6921
Reputation: 33296
You have two problems.
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"]
);
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
}
}
Upvotes: 8