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.
-
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
-
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 likewww.example.net
anddocs.example.net
. -
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 namedsearchfilter.sh
, shows theawk
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
-
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.
-
Commit and push the changes to the remote Gist.
-
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}
withddg.txt
,ecosia.txt
, orgoogle.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.
Comments
Masen Furer
Thanks for this post, method is working great.
I created a repo instead of a gist, so that I can automatically rebuild the list files using github actions: https://github.com/masenf/ublock-shitty-content/blob/main/.github/workflows/update_lists.yml
Hopefully I have preserved your license terms and sufficiently credited you in that repo, if you have any requested changes, please let me know.
Comments are closed