Skip to content

Instantly share code, notes, and snippets.

@tranch
Created December 10, 2014 19:24
Show Gist options
  • Select an option

  • Save tranch/9550b2f51b1b128283b7 to your computer and use it in GitHub Desktop.

Select an option

Save tranch/9550b2f51b1b128283b7 to your computer and use it in GitHub Desktop.
Hacker News Spam Blocker
// ==UserScript==
// @name block it
// @namespace tranch
// @description block item by domain on hacknews
// @include http://news.dbanotes.net/*
// @version 0.1
// @grant none
// ==/UserScript==
(function() {
var DB_NAME = 'HackerNewBlackList';
var items = document.querySelectorAll('td.title a'), item;
var black_list = JSON.parse(localStorage.getItem(DB_NAME)) || [];
var block_button;
window.tch_block_item = function (item_id) {
item = document.querySelector('#' + item_id);
var domain = item.href.split('/')[2];
var json = localStorage.getItem('HackerNewBlackList') || '[]';
var black_list = JSON.parse(json);
black_list.push(domain);
localStorage.setItem('HackerNewBlackList', JSON.stringify(black_list));
tch_hide_item(item);
};
window.tch_hide_item = function (node) {
node.parentNode.parentNode.nextSibling.nextSibling.style.display = 'none';
node.parentNode.parentNode.nextSibling.style.display = 'none';
node.parentNode.parentNode.style.display = 'none';
};
for (var i=0; i<items.length; i++) {
item = items[i];
item.id = 'tread_' + i;
block_button = document.createElement('a');
block_button.href = 'javascript:tch_block_item("'+item.id+'")';
block_button.innerHTML = 'block';
option_container = item.parentNode.parentNode.nextSibling.firstChild.nextSibling;
option_container.innerHTML += ' | ';
option_container.appendChild(block_button);
for (var ib=0; ib<black_list.length; ib++) {
if (item.href.contains(black_list[ib])) {
tch_hide_item(item);
}
}
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment