Chrome extension: block custom websites not working - javascript

I tried the code from this SO to block certain websites:
manifest.json:
{
"name": "StudyBuddy",
"description": "Helps you study by blocking distracting websites",
"version": "2.0",
"permissions": [
"webRequestBlocking",
"webRequest",
"activeTab",
"tabs",
"http://*/*",
"https://*/*"
],
"background" : {
"scripts": [
"background.js",
"persistent": true
]
},
"manifest_version": 2
}
Background.js:
console.log("Loaded extension");
function blockRequest(details) {
return {cancel: true};
}
function updateFilters(urls) {
if(chrome.webRequest.onBeforeRequest.hasListener(blockRequest))
chrome.webRequest.onBeforeRequest.removeListener(blockRequest);
chrome.webRequest.onBeforeRequest.addListener(blockRequest, {urls: ["*://blank.org/"] }, ['blocking']);
}
updateFilters();
I don't get any error but I can easily access blank.org despite the code.

Related

Chrome extension page_action icon not hidden

I am using background.js script to hide my page_action icon with the following code:
chrome.pageAction.hide(sender.tab.id, function(some) {
console.log(sender.tab.id);
});
However, the icon is always 'on' and never gets hidden. I do not have any other functionality which enables the icon, and according to page_action documentation, the icon should be hidden by default.
If I remove my content script from manifest.json, indeed, the icon is disabled by default.
Here is manifest.json:
{
"manifest_version": 2,
"name": "Chrome Extension",
"description": "Chrome Extension",
"minimum_chrome_version": "10.0",
"version": "1.0",
"page_action": {
"default_icon": "logo.png",
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [
"js/vendor.js",
"js/content_script.js"
],
"css": [
"js/content_script.css"
]
}
],
"background": {
"scripts": [
"js/vendor.js",
"js/background.js"
]
},
"permissions": [
"declarativeContent",
"tabs",
"webNavigation",
"storage",
"activeTab",
"<all_urls>"
]
}
Any thoughts?

Reading manifest: Error processing content_script: An unexpected property was found in the WebExtension manifest

I am trying to create a simple firefox addon that will modify page after XHR requests on pages. Unfortunately, after loading script, it shows error on about:debugging "Reading manifest: Error processing content_script: An unexpected property was found in the WebExtension manifest." It seems the content script then is not working at all.
Tried to change matches property to and inside content_scripts but it didn't work
{
"manifest_version": 2,
"name": "Some Name",
"version": "0.01a",
"applications": {
"gecko": {
"id": "some id"
}
},
"description": "Some Description",
"author": "Some Author",
"icons": {
"48": "icon.png",
"96": "icon.png"
},
"background": {
"scripts": ["jquery.min.js","declarations.js","bg.js"]
},
"content_script": [
{
"matches": ["*://somewebsite/folder/*"],
"js": ["jquery.min.js", "content.js"]
}
],
"permissions": [
"storage",
"*://somewebsite/folder/*",
"webRequest",
"webRequestBlocking"
]
}
What is wrong with the manifest.json? Where is error?
BTW, content.js:
console.log("CONTENT_SCRIPT");
function someFunction(request, sender, sendresponse) { somecode... }
browser.runtime.onMessage.addListener(someFunction);
The first is console.log and it doesn't show CONTENT_SCRIPT neither on debugging console nor the web console.
bg.js:
browser.runtime.sendMessage({
action: "timetodo",
result: requestDetails
});
The problem is that "content_script" key should be "content_scripts" (as it written in the documentation).
So use:
{
"manifest_version": 2,
"name": "Some Name",
"version": "0.01a",
"applications": {
"gecko": {
"id": "some id"
}
},
"description": "Some Description",
"author": "Some Author",
"icons": {
"48": "icon.png",
"96": "icon.png"
},
"background": {
"scripts": ["jquery.min.js","declarations.js","bg.js"]
},
"content_scripts": [
{
"matches": ["*://somewebsite/folder/*"],
"js": ["jquery.min.js", "content.js"]
}
],
"permissions": [
"storage",
"*://somewebsite/folder/*",
"webRequest",
"webRequestBlocking"
]
}

Chrome Extension - Background/Event JavaScript Not Responding or Loading

New to making extensions, I origally was attempting to load cookies but realised my EventPage.js didn't seem to be loading - since a basic console.log("Hello") wasn't loading.
I am loading the extension via Chrome "Load unpacked extensions", where manifest,content and background scripts are.
The code attached below is what I've been using to see if I can even get a console log from the event page. Only the content script loads, even If I press a key and that is logged.
CHROME:58
Any suggestions?
Manifest.SON
{
"manifest_version": 2,
"name": "Facebook Extension ",
"version": "1.0",
"description": "",
"permissions": ["storage", "webNavigation", "activeTab", "tabs", "cookies",
"*://*.facebook.com/*"],
"background": [
{
"scripts": ["event_Page.js"],
"persistent" : false
}
],
"content_scripts": [
{
"matches": ["*://*.youtube.com/*"],
"css": ["style.css"],
"js": ["jquery-2.1.0.min.js", "talkToEvent.js"]
}
]
talkToEvent.js
console.log("Hello World!s");
$(document).ready(function() {
console.log("DOM READY!");
$(document.documentElement).keydown(function (e) {
console.log("Key Has Been Pressed!");
chrome.runtime.sendMessage({Message: "getTextFile"}, function (response) {
;
})
})
});
event_Page.js
console.log("Atleast reached background.js")
chrome.runtime.onMessage.addListener (
function (request, sender, sendResponse) {
console.log("Reached Background.js");
if (request.Message == "getTextFile") {
console.log("Entered IF Block");
$.get("http://localhost:63342/Projects/StackOverflow/ChromeEXT/helloWorld1", function(response) {
console.log(response);
// to send back your response to the current tab
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {fileData: response}, function(response) {
;
});
});
})
}
else {
console.log("Did not receive the response!!!")
}
}
);
You have an incorrect manifest.json.
According event pages docs it should be an object, but in your manifest it is an array.
Correct manifest.json:
{
"manifest_version": 2,
"name": "Facebook Extension ",
"version": "1.0",
"description": "",
"permissions": [
"storage",
"webNavigation",
"activeTab",
"tabs",
"cookies",
"*://*.facebook.com/*"
],
"background": {
"scripts": [
"event_Page.js"
],
"persistent": false
},
"content_scripts": [
{
"matches": [
"*://*.youtube.com/*"
],
"css": [
"style.css"
],
"js": [
"jquery-2.1.0.min.js",
"talkToEvent.js"
]
}
]
}
Also fixed missed comma and close bracket.

chrome.tabs.executeScript event don't work anymore?

I have this code below that until last week was working, but now when I will go execute again, this not works.
Based in this question and tested by me, using chrome.tabs.onUpdated.addListener event, works fine, but I have some strings in msgBox.js file, that cause errors relative to encoding (UTF-8/ANSI) and because this, all script inside of msgBox.js file is necessary execute using chrome.tabs.executeScript.
msgBox.js
function msg()
{
alert("hello!");
}
msg();
event.js
chrome.webRequest.onCompleted.addListener(
function onWindowLoad() {
chrome.tabs.executeScript(null, {
file: "msgBox.js"
}, function() {
});
}
,
{
urls: ["<all_urls>"],
types: ["main_frame"]
},
["responseHeaders"]
);
manifest.json
{
"background": {
// "page": "popup.html"
"scripts": ["event.js"]
},
"description": "Media Player for Flash",
"manifest_version": 2,
"name": "Media Player",
"icons": {
"128" : "picture/flash128.png" ,
"48" : "picture/flash48.png"
},
"web_accessible_resources": [
"event.js"
],
"content_scripts": [
{
"matches": ["<all_urls>", "*://*/*", "http://*/*", "https://*/*"],
"js": ["event.js"],
"run_at": "document_end",
"all_frames": true
}
],
"permissions": [ "tabs", "background", "activeTab", "<all_urls>", "webNavigation", "webRequest", "http://*/*", "https://*/*", "*://*/*" ],
"version": "1.0"
}
What can is wrong?
Any help will welcome.
According to https://developer.chrome.com/extensions/tabs#method-executeScript
null is not a valid option for object in "executeScript", Try using "string" in it's place. It worked for me at least when trying to reproduce and fix
Edit: although that does not explain why it worked before but stopped

How to Block All Webpages from Loading using chrome.webRequest?

Here's my manifest.json:
{
"manifest_version": 2,
"name": "Resource Blocker",
"description": "Blocks resources loading from websites.",
"version": "1.0",
"background": {
"scripts": ["main.js"]
},
"permissions": [
"tabs", "http://*/*", "https://*/*", "webRequest", "webRequestBlocking"
]
}
And here's main.js:
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
return {cancel: details.url.indexOf("://www.facebook.com/") != -1};
},
{urls: ["<all_urls>"]},
["blocking"]);
This is clearly blocking all requests to facebook.com. However, I want to block requests to all websites. I tried replacing ://facebook.com/ with <all_urls> and ://*/* to no avail. Does anyone know the solution?

Categories

Resources