// ==UserScript==
// @name                Reddit Story Highlighter
// @namespace   http://eschew.org/projects/mozilla/greasemonkey/
// @description Highlights headlines from certain sites on reddit
// @include             http://www.reddit.com/
// @include             http://www.reddit.com/r/*
// ==/UserScript==

// By Ben Karel

(function() {

var xapply = function(xpath, func) { var x = document.evaluate(xpath, document, null, 6, null); for(var i = 0; i < x.snapshotLength; i++) func(x.snapshotItem(i)); };

var textContainsCaseInsensitive = function(needle) {
  var lowercaseAlphabet = 'abcdefghijklmnopqrstuvwxyz';
  var uppercaseAlphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

  return "contains("+
             "translate(text(), '" +uppercaseAlphabet+ "', '" +lowercaseAlphabet+ "'), '"+
             needle.toLowerCase() +"')";
}

var colorGivenSite = function(urlFragment, color) {
  xapply("//div[@class='entry'][ .//a[contains( text(), '"+urlFragment+"')] ]",
  function(node) { node.style.backgroundColor = color; });
}

colorGivenSite('slate', '#eee');
colorGivenSite('nytimes', '#edf');
colorGivenSite('youtube', '#ffe');
colorGivenSite('craigslist', '#eef');
colorGivenSite('diveintomark', '#fee');
colorGivenSite('arstechnica', '#efe');
colorGivenSite('steampowered.com', '#dee');


var colorGivenTitle = function(str, color) {
  xapply("//div[@class='entry'][ .//a[contains(@class, 'title') and "+ textContainsCaseInsensitive(str) +"] ]",
  function(node) { node.style.backgroundColor = color; });
}

colorGivenTitle('Diablo', '#fba');
colorGivenTitle('Blizzard', '#fba');
colorGivenTitle('starcraft', '#fba');
colorGivenTitle('zerg', '#fba');

})();
