0

I am trying to create a Firefox extension to block search terms on school computers. I'd like to prohibit a list of keywords, but the blocking doesn't seem to be working.

I found an example through a plugin gallery here: https://github.com/mdn/webextensions-examples/blob/master/proxy-blocker/background/proxy-handler.js

This plugin listens to blocked hosts, and then basically returns localhost. I'd like to do the same, but when search terms are added in. I used the code in the example above as a starting point.

Here is the code I have so far:

// Initialize the list of blocked hosts
let blockedHosts = ["www.duckduckgo.com", "www.google.com"];
let blockedTerms = ["games", "minecraft", "legos"];

// Set the default list on installation.
browser.runtime.onInstalled.addListener(details => {
  browser.storage.local.set({
    blockedHosts: blockedHosts
  });
});

// Get the stored list
browser.storage.local.get(data => {
  if (data.blockedHosts) {
    blockedHosts = data.blockedHosts;
  }
});

// Listen for changes in the blocked list
browser.storage.onChanged.addListener(changeData => {
  blockedHosts = changeData.blockedHosts.newValue;
});

// Managed the proxy

// Listen for a request to open a webpage
browser.proxy.onRequest.addListener(handleProxyRequest, {urls: ["<all_urls>"]});

function handleProxyRequest(requestInfo) {
  let urlToCheck = new URL(requestInfo.documentUrl)
  let searchString = urlToCheck.search;
  const url = new URL(requestInfo.url);
  let found;

  blockedTerms.map((term) =>{
    if(searchString.search(term) != -1){
      found = true
    }
  })

  if ( blockedHosts.indexOf(url.hostname) != -1 & found) {
    return {type: "https", host: "127.0.0.1", port: 65535};
  }

// Return instructions to open the requested webpage
  return {type: "direct"};
}

// Log any errors from the proxy script
browser.proxy.onError.addListener(error => {
  console.error(`Proxy error: ${error.message}`);
});

The URL that the browser creates is https://duckduckgo.com/?t=ffab&q=games&ia=web for example. I can determine that the term "games" was found, and that it was found in a duck duck go search, but the proxy wont work and the browser wont stop the user from going to the page.

Any help would be appreciated!

foo_bar_zing
  • 113
  • 1
  • 9

1 Answers1

0

To start with, in a school environment, I suppose they have to use school net connection. It would be a lot easier to block at the main internet connection instead of creating and installing an addon on each computer (that might be altered or bypassed with another browser).

However, to answer your question, the following would be one (simpler) way of doing that using webRequest.onBeforeRequest:

// add a listener for web requests
browser.webRequest.onBeforeRequest.addListener(process, {
    urls: ['*://*/*']
  },
  ['blocking']
);

function process(e) {
  // e.url is the target url
  // no need for storage as the filter-list is hard-coded
  const blockedHosts = ['www.duckduckgo.com', 'www.google.com'];
  const blockedTerms = ['games', 'minecraft', 'legos'];

  const hostRegExp = new RegExp(`^https?://(${blockedHosts.join('|')})/`, 'i');
  const termRegExp = new RegExp(`(${blockedTerms.join('|')})`, 'i');

  // if matches above criteria, redirect to 127.0.0.1
  if (hostRegExp.test(e.url) && termRegExp.test(e.url)) {
    return {redirectUrl: 'https://127.0.0.1:65535/'};
  }
}
erosman
  • 7,094
  • 7
  • 27
  • 46
  • This seems to work for my use case. * Edit * I checked unrelated terms and they are also blocked. If I search for "cheese" or "hotel" it gets blocked, not just the items in the blocklist. Is that intentional? – foo_bar_zing Jul 27 '22 at 20:41
  • @foo_bar_zing Once you block `google.com` any google search will be blocked. If you give clear examples, then the code can be adjusted. Above code was simply an example code. – erosman Jul 28 '22 at 03:29
  • I gave a clear example in the original description. I want to block specific search terms, and subsequently URL's, from proceeding. `https://duckduckgo.com/?t=ffab&q=games&ia=web` is a search term for games via duck duck go. I don't want to block duckduckgo, rather any searches for specific terms via duckduckgo – foo_bar_zing Jul 28 '22 at 14:46
  • @foo_bar_zing Code updated. Please note that above is a SAMPLE code, as a starting point. The regex will catch terms that maybe part of other legitimate terms e.g. if you block `sex` it will also block `middlesex`. It requires more customization to suit the final requirements. – erosman Jul 28 '22 at 14:54