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.
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://www.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:
// ==UserScript== // @name RemoveBadHyperlinks // @namespace https://www.scottlilly.com // @description Removes hyperlinks to websites with annoying policies // @version 1 // @grant none // ==/UserScript==
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:
// ==UserScript== // @name RemoveBadHyperlinks // @namespace https://www.scottlilly.com // @description Removes hyperlinks to websites with annoying policies // @version 1 // @grant none // ==/UserScript== var linkList = document.querySelectorAll ("a"); Array.prototype.forEach.call (linkList, function (link) { if (link.hostname.includes("nytimes.com") || link.hostname.includes("wired.com") || link.hostname.includes("forbes.com") || link.hostname.includes("wsj.com") || link.hostname.includes("buzzfeed.com")) { //-- Block the link link.href = "javascript:void(0)"; link.style = "text-decoration: none;background: #f3f315;"; } } );
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.
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 Comments