Friday, April 25, 2008

HN Blacklist Now With UI

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

So someone said something about having a user interface for my little Greasemonkey script that acts as a blacklist on Hacker News.

Done. With it installed, a 'blacklist' link will appear in the top bar next to the 'submit' link, which will allow you to edit the list of blocked domains.

// ==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

// Just assume the stuff below will work.
var blacklist = GM_getValue('hnblacklist', '')
if (blacklist.length > 0) {
blacklist = blacklist.split(' ');
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 ((blacklist[i].length > 0) && (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';
break;
}
}
}
}

// Add link to edit the blacklist.
var submitlink = document.evaluate("//a[@href='submit']", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
if (submitlink.snapshotLength > 0) {
var slparent = submitlink.snapshotItem(0).parentNode; // We only care about the first one.
var editlink = " | <a id=\"blacklist\" href=\"#\">blacklist</a>";
slparent.innerHTML = slparent.innerHTML + editlink;

var link = document.getElementById('blacklist');
link.addEventListener('click', function(ev) {
var newlist = prompt('Enter a space-delimited list of domains:', GM_getValue('hnblacklist', ''));
if (newlist != null) { GM_setValue('hnblacklist', newlist); window.location.reload(); }
}, true);
}

You can install it from here.

0 comments: