Help me make a Google Chrome Adblocker that blocks domains from the text file…
- User
Help me make a Google Chrome Adblocker that blocks domains from the text file "d3host.txt" and ignores comments (comments start with #).
- Assistant
Creating a Google Chrome ad blocker extension that blocks domains listed in a text file (
d3host.txt) and ignores comments is a multi-step process. Here’s a step-by-step guide on how to achieve this:Step 1: Setup Your Extension Directory
- Create a new directory for your extension, e.g.,
chrome-adblocker. - Inside that directory, create a file named
manifest.json.
Step 2: Create the Manifest File
The
manifest.jsonfile describes your extension and its capabilities. Here’s an example configuration:json{ "manifest_version": 3, "name": "Custom Domain Blocker", "version": "1.0", "description": "Blocks domains listed in d3host.txt.", "permissions": [ "webRequest", "webRequestBlocking", "storage", "https://*/*", "http://*/*" ], "background": { "service_worker": "background.js" }, "action": { "default_popup": "popup.html" }, "icons": { "48": "icon.png" } }Step 3: Create the
background.jsFileThis file will handle processing the
d3host.txtfile and blocking the listed domains. Create a file namedbackground.jsin your extension directory:javascript// background.js chrome.runtime.onInstalled.addListener(() => { fetch(chrome.runtime.getURL('d3host.txt')) .then(response => response.text()) .then(data => { const lines = data.split('\n'); const blockedDomains = lines .map(line => line.trim()) .filter(line => line && !line.startsWith('#')); const rules = blockedDomains.map(domain => ({ id: domain, priority: 1, action: { type: 'block' }, condition: { urlFilter: `*://${domain}/*` } })); chrome.declarativeNetRequest.updateDynamicRules({ addRules: rules, removeRuleIds: rules.map(rule => rule.id) }); }) .catch(error => console.error('Failed to load d3host.txt:', error)); }); - Create a new directory for your extension, e.g.,
- User
Make sure the extension is Manifest version 2
- Assistant
- User
Thanks now i get this warning:
Permission ':///*' is unknown or URL pattern is malformed.
Context line 10 of manifest.json
- Assistant
It looks like there's an issue with the URL patterns in the
permissionsarray in themanifest.jsonfile. The correct syntax should explicitly state the schemes (http, https) along with the wildcard (* to match all domains).Let's correct the
manifest.jsonto use the appropriate URL patterns:Updated
manifest.jsonjson{ "manifest_version": 2, "name": "Custom Domain Blocker", "version": "1.0", "description": "Blocks domains listed in d3host.txt.", "permissions": [ "webRequest", "webRequestBlocking", "storage", "http://*/", "https://*/" ], "background": { "scripts": ["background.js"] }, "browser_action": { "default_popup": "popup.html" }, "icons": { "48": "icon.png" } }