Help me make a chromium-based adblocker that blocks domains from a text file named…
- User
Help me make a chromium-based adblocker that blocks domains from a text file named "d3host.txt".
- Assistant
Creating a Chromium-based adblocker that blocks domains listed in a
d3host.txtfile involves developing a Chrome extension. This extension will read the text file, parse the domains, and block requests to those domains.Here’s a step-by-step guide to building such an extension:
-
Setup Project Files
Create a new directory for your Chrome extension, and inside it, create the following files:
manifest.jsonbackground.jsd3host.txt
-
Preparation of
d3host.txtd3host.txtshould contain the list of domains to be blocked, one per line. For example:codeexample.com another-domain.com -
manifest.json
This file is crucial for defining the extension's behavior, permissions, and background scripts.
json{ "manifest_version": 3, "name": "Domain Blocker", "version": "1.0", "description": "Blocks domains listed in d3host.txt", "permissions": [ "webRequest", "webRequestBlocking", "storage", "http://*/", "https://*/" ], "background": { "service_worker": "background.js" }, "host_permissions": [ "*://*/*" ] } -
background.js
This script runs in the background and handles blocking the domains specified in
d3host.txt.javascript
-
- User
Thank you. I have some questions about the getBlockedDomains function: does chrome.runtime.getURL("d3host.txt") fetch the text file because "getURL" makes it seem like it's contacting a URL, not a local file? Also in the "d3host.txt" file, there are comments that start with "#" Does the script handle that?
- Assistant
- User
When i try to load the extension it gives me this error:
Manifest is not valid JSON. Line: 2, column: 6, Dictionary keys must be quoted.
and highlights this line in manifest.json:
“manifest_version”: 3,
- Assistant
It looks like the quotes in your
manifest.jsonfile are using typographic quotes (often known as curly quotes or smart quotes) instead of the standard straight quotes required for JSON formatting. JSON strings should always use straight double quotes (").Here’s the corrected
manifest.jsonfile with proper straight quotes:json - User
Thanks now i get these warnings and error:
(WARNING) 'webRequestBlocking' requires manifest version of 2 or lower. (WARNING) Permission 'http:///' is unknown or URL pattern is malformed. (WARNING) Permission 'https:///' is unknown or URL pattern is malformed. (WARNING) Permission ':///*' is unknown or URL pattern is malformed. (WARNING) Service worker registration failed. Status code: 15
(ERROR) Uncaught SyntaxError: Invalid or unexpected token Context background.js
highlighted text in getBlockedDomains
const response = await fetch(chrome.runtime.getURL(‘d3host.txt’));
- Assistant