Chrome web request blocking not working - javascript

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.

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
}
}

Related

Why is chrome.runtime undefined in the content script?

I have a very simple chrome extension, where I'm trying to pass a message from the background script to the content script. But chrome.runtime is undefined.
Here's literally all the code, as you can see there's almost nothing to it. In the content script, runtime is undefined.
Background Script:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.runtime.sendMessage({action: 'someAction'},
function(response) {
console.log('Response: ', response);
});
});
Content Script:
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
sendResponse({
user: 'user'
});
});
manifest.json
{
"manifest_version": 2,
"name": "My Extension",
"version": "1.0",
"description": "Some stupid extension",
"browser_action": {
"default_icon": "icons/MyExtensionIcon.png"
},
"icons": {
"48": "icons/MyExtensionIcon.png"
},
"permissions": [
"tabs",
"storage",
"https://*/",
"http://*/"
],
"content_scripts": [
{
"matches": ["*://*.twitter.com/*", "https://twitter.com/*"],
"js": ["js/content.js"]
}
],
"background": {
"scripts": ["js/background.js"],
"persistent": true
},
"web_accessible_resources": [
"js/*",
"css/*"
]
}
Other Info:
Chrome Version 58.0.3029.110 (64-bit)
Installing my extension as an
"Unpacked extension" with developer mode
Ok I figured it out. It's absolutely stupid, but it appears this is simply a Heisenbug. By adding a breakpoint, or debugger statement, it causes that value to be undefined. Maybe a chrome bug?
I swear, every day Chrome feels more, and more like Internet Explorer.

How to block some urls with chrome extension

I 'm trying to make my own chrome extension
to block the "seen" and "typing" status of facebook.
But it seems my way doesnt work
Can someone help me find my error?
manifest.json
{
"name": "Block Seen Typing",
"description": "Block Seen",
"version": "1.0",
"manifest_version": 2,
"background": {
"scripts": [
"background.js"
]
},
"permissions": [
"webRequest",
"*://facebook.com/*",
"*://www.facebook.com/*",
"webRequestBlocking"
]
}
background.js
chrome.webRequest.onBeforeRequest.addListener(
{
urls: [
"https://www.facebook.com/ajax/messaging/typ.php", "https://www.facebook.com/ajax/mercury/mark_seen.php", "https://www.facebook.com/ajax/mercury/change_read_status.php" // here you put the URL that you want to block.
],
types: ["main_frame", "sub_frame", "stylesheet", "script", "image", "object", "xmlhttprequest", "other"]
},
["blocking"]);
Generaly i want to know how to block any webrequest that i want.
It looks like you forgot to actually include any code for a listener. Try something like this
chrome.webRequest.onBeforeRequest.addListener(function(d){
return {cancel:true};
},{urls:["https://www.facebook.com/ajax/messaging/typ.php",
"https://www.facebook.com/ajax/mercury/mark_seen.php",
"https://www.facebook.com/ajax/mercury/change_read_status.php"]},
["blocking"]);

Chrome extension webRequest.onBeforeRequest

I am trying to create an extension to analyse requests made on the chrome browser but I can't put it work. The alert never fires.
manifest.json
{
"name": "Test",
"description": "Test",
"version": "1.0",
"manifest_version": 2,
"permissions": ["background", "tabs", "webRequest", "webRequestBlocking", "*://*/*"],
"background": {
"scripts": ["background.js"],
"persistent": true
}
}
background.js
var callback = function(details) {
alert("hello");
};
var filter = { "*://*/*" };
var opt_extraInfoSpec = [];
chrome.webRequest.onBeforeRequest.addListener(
callback, filter, opt_extraInfoSpec);
Why is it my alert not firing?
Your filter is the wrong format - it's not a valid object at all. Addtionally it needs to contain at least the 'url' property. If you wan't all URL's, use this:
var filter = {urls: ["<all_urls>"]};
Check out this for exact details on the format for the filter: https://developer.chrome.com/extensions/webRequest#type-RequestFilter

Screenshot using chrome.tabs.captureVisibleTab

I'm trying to capture the visible area of a page using chrome.tabs.captureVisibleTab. Here is the code that makes the call:
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
if (request.name == 'screenshot') {
chrome.tabs.captureVisibleTab(null, null, function(dataUrl) {
sendResponse({ screenshotUrl: dataUrl });
});
}
});
But when I try to capture the tab I get this error:
Unchecked runtime.lastError while running tabs.captureVisibleTab: The 'activeTab' permission is not in effect because this extension has not been in invoked.
Here is my manifest file:
{
"manifest_version": 2,
"name": "Empathy",
"version": "0.1",
"description": "Simulate accessibility issues for websites.",
"browser_action": {
"default_icon": "empathy19.png",
"default_title": "Empathy!"
},
"permissions": [
"activeTab",
"contextMenus",
"desktopCapture",
"tabCapture",
"tts" // Text-to-speech
],
"background": {
"scripts": [
"boot.js"
],
"persistent": false
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": [
"src/helpers.js",
"src/colorblindness.js",
"lib/colorvision.js",
"lib/html2canvas.js"
]
}
]
}
I have active tab permissions
The call is being made from a background script
I'm matching <all_urls>
Why do I get that error?
There are things that talk about <all_urls> as something to match, but what I was missing was the <all_urls> permission. After I added the permission, it worked.

Content Script Notification

Having trouble, and wasnt finding anything else on here that answered what I have :/
Manifest:
{
"name": "Item Sniper",
"version": "1.0",
"description": "Sniper",
"browser_action": {
"default_icon": "face.png",
"default_title": "Sniper"
},
"background": {
"scripts": ["background.js"]
},
"permissions": [
"tabs",
"notifications",
"http://*/*"
]
}
Background.js:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(null,{file: "buy.js"});
}
);
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
var notify = webkitNotifications.createNotification(
'face.png', // icon url - can be relative
'Hello!', // notification title
'Oh hellow!' // notification body text
);
});
Buy.js [There's more to it, but this is the notification part]:
chrome.extension.sendRequest({msg: "Sup?"}, function(response) { // optional callback - gets response
console.log(response.returnMsg);
});
I basically want the content script to create a notification, but I didnt know if it was possible while sticking with a js script as background :/
Thanks for any help,
Alex
The background property is only available for manifests using version 2. If you want to support this you'll need to update your manifest to the following;
{
"name": "Item Sniper",
"version": "1.0",
"description": "Sniper",
"manifest_version": 2,
"minimum_chrome_version": "18",
"browser_action": {
"default_icon": "face.png",
"default_title": "Sniper"
},
"background": {
"scripts": ["background.js"]
},
"permissions": [
"tabs",
"notifications",
"http://*/*"
]
}
Notice that I also set the minimum_chrome_version property to 18 as manifest version 2 can only be used when targeting this version of Chrome or newer.
I think you missed to call notify.show(); in your background.js
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
var notify = webkitNotifications.createNotification(
'face.png', // icon url - can be relative
'Hello!', // notification title
'Oh hellow!' // notification body text
);
notify.show();
});
http://code.google.com/chrome/extensions/notifications.html#api

Categories

Resources