I'm making a very simple Chrome extension to block requests to some domains (got tired of slow page loads on many websites, waiting on facebook junk). My question is about how to efficiently load a user-specified list of domains. The Chrome documentation points out that it is more efficient to pass a map containing 'urls' to the addListener call instead of passing in all requests and checking them in my function. How can I do that, but use a user-provided list of domains/expressions?
Here are my manifest and js files so far:
Manifest.json
{
"name": "I Don't Want YOur Social Networking Junk",
"version": "1.0",
"description": "This extension let's you block (Chrome will not download) content from domains. Too many sites slow themselves down by bringing in a bunch of junk from sites like facebook. This will let you block those things.",
"permissions": ["webRequest", "webRequestBlocking", "http://*/*", "https://*/*"],
"background": {
"scripts": ["background.js"]
},
"manifest_version": 2
}
background.js
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
return {cancel: true};
}, { urls: ["*://*.facebook.com/*", "*://*.facebook.net/*"] }, ["blocking"]);
As you can see I have a couple of hard-coded expressions in the urls list for now. That's what I'd like to load from something that a user could populate. Recommendations?
StayFocusd is an extension available on the Google Chrome web browser. This makes installation extremely straightforward – even if you aren't tech-savvy. Pros: Many website blockers have users enter a list of specific websites (often called a “blacklist”) to block for certain time periods.
Self-Control is an app designed to be your own personal distraction blocker. The way it works is pretty simple: It permits you to add certain websites to your “blacklist” that you want to block and allows you to set a timer. Just set it for the amount of time you need to stay away from those sites and go.
StayFocusd is a productivity extension for Google Chrome that helps you stay focused on work by restricting the amount of time you can spend on time-wasting websites. Once your allotted time has been used up, the sites you have blocked will be inaccessible for the rest of the day.
On the Stay Focused main menu, tap on the + button and start adding apps. When you're done adding apps, tap on the cog button on the main menu and configure the “Maximum Time Allowed Per Day” under Block Settings. The app has preset time limits of 10, 20, 30 and 60 minutes.
Use something like this:
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: urls}, ['blocking']);
}
You can provide an options page to let the user to specify domains to block (and preferably save them with chrome.storage API). In your background page, update filters by re-registering the listener when it is initialized or the setting is changed.
By the way, you should use the Declarative Web Request API when it is stable since it is much more efficient and doesn't require a persistent background page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With