// ==UserScript==
// @name          NoPolitics
// @namespace     http://emmettshear.com/
// @description   Automatically calls 'hide' on any post whose title matches a word in the word list.
// @include       http://reddit.com/*
// @include       http://www.reddit.com/*
// @include       http://reddit.com/
// @include       http://www.reddit.com/
// @include       http://reddit.tld/*
// @include       http://www.reddit.tld/*
// ==/UserScript==
// Notes:
//   * is a wildcard character
//   .tld is magic that matches all top-level domains (e.g. .com, .co.uk, .us, etc.)

function Set(){
  for(var i=0; i<arguments.length; i++) this[arguments[i]] = true;
}

Set.from_array = function(arr){
  var s = new Set();
  for(var i in arr) s[arr[i]] = true;
  return s;
}

word_list = new Set('voter', 'election', 'bush', 'clinton', 'dictator', '\\scia\\s', 'torture', 'republican', 'democrat', 'judge');

function map(f, xs){
  if(!xs) return {};
  switch(xs.constructor){
  case Set:
    result = new Set(); 
    for(var x in xs) result[f(x)] = true;
    return result;
    break;
  case Array:
    result = [];
    for(var i in xs) result[i] = f(xs[i]);
    return result;
    break;
  case Object:
  default:
    result = {};
    for(var i in xs) result[i] = f(xs[i]);
    return result;
    break;
  }
}

/*
id
index
title
domain
score
time_ago_in_words
user
comments
*/

function RedditLink(id){
  this.id = id;
  var re = new RegExp('onmousedown="return rwt\\(this, ' + this.id + ', \'\\w+\', \'.*\'\\)"\\s*>\\s*([^<]+)</a>');
  var match = document.body.innerHTML.match(re);
  this.title = match ? match[1] : 'no title found for ' + this.id;
}

RedditLink.prototype.hide = function(){
  unsafeWindow.removeSiteDom(this.id);
}

reddit_link_id = /site(\d+)/g;
function reddit_links(){
  var links = map(function(id){return new RedditLink(id.replace('site', ''));}, 
                  document.body.innerHTML.match(reddit_link_id));
  return links;
}

window.addEventListener("load", function(e) {
  var links = reddit_links();
  for(var i in links){
    var link = links[i];
    for(word in word_list) {
      if(new RegExp(word, "i").test(link.title)) {
        link.hide();
      }
    }
  }
}, false);