Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome extension to efficiently block domains

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?

like image 297
user605331 Avatar asked Apr 02 '13 13:04

user605331


People also ask

Is there a Chrome extension to block websites?

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.

How do I block a website at a certain amount of time?

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.

What is StayFocusd?

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.

How do you use Stayfocused?

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.


1 Answers

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.

like image 143
方 觉 Avatar answered Oct 06 '22 23:10

方 觉