// ==UserScript==
// @name           Reddit Auto Downvote
// @namespace      http://arantius.com/misc/greasemonkey/
// @description    Sick of particular topics on reddit?  Get them gone!
// @require        http://arantius.com/misc/greasemonkey/imports/dollarx.js
// @include        http://reddit.com/
// ==/UserScript==

function randomDelay() {
	return Math.floor(Math.random()*7000+1500);
}

// The patterns to match.
var badWords=['scientolog','vote up','OMG','WTF','helen thomas'];
var badLinks=['theonion.com\/'];

// Add an article to the killfile.
var badArticles=[];
function killfile(article) {
	// Save the article for downvoting later.
	badArticles.push(article);
	// Immediately hide it from display.
	console.log('hiding:', article);
	document.getElementById('thingrow'+article.id.substr(5))
		.style.display='none';
	document.getElementById('pre'+article.id.substr(5))
		.style.display='none';
}

// Find the articles we want to downvote.
var articles=$x('//a[starts-with(@id, "title_")]');
for (var i=0, article=null; article=articles[i]; i++) {
	// By words in the title.
	for (var j=0, badWord=null; badWord=badWords[j]; j++) {
		if (article.textContent.match(new RegExp(badWord, 'i'))) {
			killfile(article);
			break;
		}
	}
	// By links.
	for (var j=0, badLink=null; badLink=badLinks[j]; j++) {
		if (article.href.match(new RegExp(badLink, 'i'))) {
			killfile(article);
			break;
		}
	}
}

// Now, randomly in the future, to avoid bot filter, downvote the actual article.
setTimeout(doDownvote, randomDelay());
function doDownvote() {
	var article=badArticles.pop();
	if (!article) return;

	// Downvote this one.
	console.log("downvoting:", article);
	document.location.href=
		document.getElementById('down'+article.id.substr(5))
			.getAttribute('onclick');

	// Downvote the next one later.
	setTimeout(doDownvote, randomDelay());
}
