Tip
Also check out quenhus/uBlock-Origin-dev-filter on GitHub and iorate/ublacklist. These options may be better as these filters can be difficult to maintain in the long run.

Lately, I’ve noticed a lot of spam and content farms showing up in my web search results. Based on Reddit comments1 2 on the uBlock Origin subreddit, I managed to set up custom filters to prevent these results from showing up on the search page. However, my filter list was getting longer and thus more difficult to maintain. So, I decided to use a few tools to automate the process.

Info
According to its wiki, uBlockOrigin “supports most of EasyList filter syntax”, so the Adblock Plus and AdGuard filter syntax documentation are useful references.

  1. Create a private GitHub Gist and clone it to your local machine. The ${gistid} can be found in the Gist’s URL, i.e. https://gist.github.com/${username}/${gistid}.

    Clone via HTTPS:

    git clone https://gist.github.com/${gistid}.git
    

    Clone via SSH:

    git clone git@gist.github.com:${gistid}.git
    
  2. Navigate to the cloned Gist’s directory. Create a new text file (I called mine searchfilter.txt) and list the domains that you want to block in your search results. Each unique domain should occupy a new line. For example:

    example.com
    .example.net
    www.example.xyz
    

    Note
    .example.net will block domains with prefixes like www.example.net and docs.example.net.

  3. In the same directory, create a new text file to store awk scripts that will convert these domains into uBlock Origin filters. The example script below, which I’ve named searchfilter.sh, shows the awk commands for creating DuckDuckGo, Ecosia, and Google filters.

    #!/bin/sh
    
    # DuckDuckGo
    echo "! Title: Custom filters - DuckDuckGo search results
    " > ddg.txt
    awk '{
    print "\
    duckduckgo.com##a[data-testid=\"result-title-a\"][href*=\""$1"\"]\
    :upward(.nrn-react-div)\n\
    duckduckgo.com##.tile-wrap a[href*=\""$1"\"]:upward(.tile)"
    }' searchfilter.txt >> ddg.txt
    
    # Ecosia
    echo "! Title: Custom filters - Ecosia search results
    " > ecosia.txt
    awk '{
    print "ecosia.org##a[href*=\""$1"\"]:upward(2)"
    }' searchfilter.txt >> ecosia.txt
    
    # Google
    echo "! Title: Custom filters - Google search results
    " > google.txt
    awk '{
    print "google.*##a[href*=\""$1"\"]:upward(2)"
    }' searchfilter.txt >> google.txt
    
  4. Run the awk script in the terminal within the same directory (note that you’ll need to run it using an environment like Git Bash, Cygwin, or MSYS on Windows):

    ./searchfilter.sh
    

    This will create a new text file for each search engine you have defined in the script.

  5. Commit and push the changes to the remote Gist.

  6. Open your browser’s uBlock Origin dashboard. Click on the ‘Filter lists’ tab and scroll down to ‘Custom’. Check the ‘Import’ box and add the filters’ GitHub Gist URLs, which will be of the following form (replace ${filename} with ddg.txt, ecosia.txt, or google.txt):

    https://gist.github.com/${username}/${gistid}/raw/${filename}
    

    Once added, click ‘Apply changes’, and your filters should be active right away.

    If you make any changes to the list, just run the awk script again, commit, and push to the remote as usual. Force update your custom filters in the uBlock Origin dashboard for the updated filters to take effect immediately.

Info
See uBlock Origin’s wiki on Reddit and GitHub for more information about creating and applying filters.

Footnotes

Comments

Comments are closed