// ==UserScript==
// @name        Facebook Ad Relief
// @author      Kees Cook
// @email       outflux.net | kees
// @version     1.3
// @namespace   http://outflux.net/greasemonkey
// @description Removes News Feed items that don't have feedback buttons
// @include     http://facebook.com/*
// @include     http://*.facebook.com/*
// @license     GNU General Public License
// ==/UserScript==

(function() {
  window.addEventListener("load", function(e) {

    var removed = 0;

    function xpath(query, context) {
      context = context ? context : document;
      return document.evaluate(query, context, null,
                               XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
                               null);
    }

    function drop_node(node) {
        node.parentNode.removeChild(node);
        removed += 1;
    }

    function drop_nodes(nodes) {
        for (var i = 0; i < nodes.snapshotLength; i++) {
            drop_node(nodes.snapshotItem(i));
        }
    }

    drop_nodes(xpath("//div[contains(@id,'ssponsor')]"));
    drop_nodes(xpath("//div[contains(@class,'ad_capsule')]"));
    drop_nodes(xpath("//div[contains(@class,'social_ad')]"));
//    drop_nodes(xpath("//div[contains(@class,'social_ad_sponsored_label')]"));

    var news = xpath("//div[contains(@id,'home_main')]/div[contains(@class,'home_main_item')]/div/h2[.='News Feed']");
    //var news = xpath("//div[contains(@id,'page_body')]");
    if (news.snapshotLength) {
        title = news.snapshotItem(0);

/*
        // ignore any divs with "_header" in the class name
        var nodes = xpath("div[not(contains(@class,'_header'))]",title.parentNode.parentNode);
        for (var i = 0; i < nodes.snapshotLength; i++) {
            var node = nodes.snapshotItem(i);

            // keep nodes with a feedback clicker
            okay = xpath("div//a[contains(@onclick,'feedback_click')]",node);
            if (!okay.snapshotLength) {
                drop_node(node);
            }
        }
*/
        if (removed) {
            title.childNodes[0].nodeValue += " (Ads filtered: " + removed + ")";
        }
    }

  }, false);
})();

