Friday, April 11, 2008

Greasemonkey

05/20/2008 UPDATE: Check out the new version of this which is bigger, faster, stronger, and easier to configure.

So I know Greasemonkey is like a billion years old, but I finally found a use for it today and wrote my first GM script.

There was a vote today on Hacker News to ban submissions from Valleywag, mainly because they are all linkbait. For some time I have been avoiding links on HN to certain sites because my experience with them in the past has been less than great. So why not save myself the time of actually avoiding those sites and just hide them to begin with?

I put the script up on userscripts.org as HN Blacklist. Here is the source:

// ==UserScript==
// @name HN Blacklist
// @namespace http://news.ycombinator.com
// @description Removes blacklisted links from Hacker News
// @include http://news.ycombinator.com/*
// ==/UserScript==

// Written by Xichekolas ... do whatever you want with it.
// http://news.ycombinator.com/user?id=Xichekolas

// Edit this list of blacklist terms.
var blacklist = new Array('valleywag.com', 'nytimes.com');

// Just assume the stuff below will work.
var xpathnodes = document.evaluate("//a", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

for(var n = 0; n < xpathnodes.snapshotLength; n++) {
var thisnode = xpathnodes.snapshotItem(n);
for (var i = 0; i < blacklist.length; i++) {
if (thisnode.href.toLowerCase().indexOf(blacklist[i].toLowerCase()) > -1) {
var grandpa = thisnode.parentNode.parentNode;
grandpa.style['display'] = 'none';
grandpa.nextSibling.style['display'] = 'none';
grandpa.nextSibling.nextSibling.style['display'] = 'none';
}
}
}

I started out using jQuery to do this, but converting it to pure javascript was a fun experience and a worthwhile effort in itself, even if nobody actually uses this thing but me.

4 comments:

Anonymous said...

I've been thinking of doing the same for TechCrunch but put it off since won't have any effect on the news.yc RSS feed.

What's the license for this script?

Andrew said...

The license is literally "do whatever you want with it"

If you decide to use it, make sure you grab the updated version... Either http://www.andrewfarmer.name/2008/04/hn-blacklist-now-with-ui.html or on the userscripts.org site.

Anonymous said...

I saw the "do whatever you want" text in the source, but I wanted to ask since it's not clear to me if you've put this in the public domain or not.

Meanwhile, pg is now considering banning TechCrunch: http://news.ycombinator.com/item?id=181539

Andrew said...

That was a user suggestion. I'd highly doubt he would ban TC. I would rather people use a clientside blacklist like mine if they were going to use anything, so that way one person's choices don't affect others.