[Greasemonkey] Block links to annoying websites

Normally, I write about C# here. However, I want to share this little utility I wrote to fix an annoying problem I encounter almost every day.

Some websites won’t let you view their pages if you have an ad-blocker installed for your browser. When I visit sites like Hacker News, there are usually a few links to articles on these sites. So, when I visit them, I can’t view them.

So, I wrote this Greasemonkey script to remove the links to these articles, and highlight them in yellow – so I know I can’t click on that text.

 

Installing Greasemonkey

Greasemonkey is a web browser add-on. It lets you write JavaScript scripts that modify the web pages you visit.

I use Firefox, and installed the Greasemonkey plug-in from here: https://addons.mozilla.org/en-US/firefox/addon/greasemonkey/

After you install Greasemonkey, you should see a monkey icon in your browser’s toolbar.

Click on the arrow to the right of the monkey icon, to see the Greasemonkey menu

 

Script code

To create this script, click on the arrow (next to the monkey) and select “New User Script…” from the menu.

I named this script “RemoveBadHyperlinks”.

For the namespace, select something that is unique, in case you ever import a script with the same name. I used my website name “https://scottlilly.com”.

I used “Removes hyperlinks to websites with annoying policies” for the description.

The Includes and Excludes boxes are empty.

 

Clicking on the “OK” will open a script editor, with the script’s information in it:

If you want to copy-paste this code into the script editor, you need to type “allow pasting” first. I typed this after the last line of the script header information. Or, you could manually type the code below into your editor window.

If you paste the code, remove the “allow pasting” line and your code should look like this:

 

When you visit a page, while this script is activated, it will get a list of all hyperlinks (the “a” values). The script will look at each hyperlink. If any of them include the text I’ve specified, it will replace the link with “javascript:void(0)”. change the link text’s background to yellow, and remove the underline you would normally see on a hyperlink.

You can remove, or add, websites to your version of this script. Just delete, or add, the “link.hostname.includes” lines.

 

Now, browsing is a little less frustrating.

 

What if you want to click on one of the links?

You can disable the script by clicking on the arrow next to the monkey, to show the Greasemonkey menu, and un-checking the script(s) you want to turn off. Check them, to turn the script on again.

From the Greasemonkey menu, you can enable and disable scripts, when needed

 

Do you have suggestions to improve this script?

I don’t write much JavaScript, so this could probably be improved. If you know a better way to do this, please leave a comment below.

 

Thanks!

2 thoughts on “[Greasemonkey] Block links to annoying websites

  1. Good work Scott! Simple yet important thing.

    Can you tell me if this code blogs all links of those sites you specified in the code (nytimes, wired, buzzfeed), or only after ensuring that these sites are using adblock bypass in them. Just wondering.

    Cheers!

    1. Thanks! This will block every link to the listed sites, but does not automatically add sites to the list, or verify that they are trying to block adblock users. It’s a manual process. But, that also lets me add sites that use other practices I don’t like.

Leave a Reply

Your email address will not be published. Required fields are marked *