abhishekcghosh / De-S-a-r-a-h-a-h Facebook

// ==UserScript==
// @name         De-S-a-r-a-h-a-h Facebook
// @include      http://*.facebook.com/*
// @include      https://*.facebook.com/*
// @version      0.01
// @description  Reduce the number of S-a-r-a-h-a-h related posts on your Facebook News Feed, because confession pages are so 2012
// @author       Abhishek Ghosh (abhishek.c.ghosh@gmail.com)
// ==/UserScript==

// This is a very basic, sub-optimal, stupid implementation that *reduces* (not eliminates) the number
// of (probable) Sarahah related posts on your Facebook news feed. This does a basic keyword scan is not bulletproof
//
// So don't blame the guy for writing it in 2 mins

(function() {
    'use strict';

    // Make this configurable and block Donald Trump posts tomorrow as well
    var regex = /(sarahah)/gim;

    var markerClass = '__sarahah_marked__';
    var showText = 'Show hidden probable Sarahah post';
    var contentArea = document.getElementById('contentArea');

    if (!contentArea) return;

    function createShowLink(post) {
        var showLink = document.createElement('div');
        var showLinkText = document.createTextNode(showText);
        showLink.style.textDecoration = 'underline';
        showLink.style.cursor = 'pointer';
        showLink.style.paddingBottom = '10px';
        showLink.appendChild(showLinkText);
        showLink.addEventListener('click', function () {
            post.style.display = 'block';
            showLink.style.display = 'none';
        });
        return showLink;
    }

    function suppressPost(post) {
        if (post.classList.contains(markerClass)) return;
        post.classList.add(markerClass);
        post.parentNode.insertBefore(createShowLink(post), post);
        post.style.display = 'none';
    }

    function process() {
        var posts = contentArea.querySelectorAll('[data-testid="fbfeed_story"]');
        for(var c = 0; c < posts.length; c++) {
            if (posts[c].textContent.search(regex) > -1) {
                suppressPost(posts[c]);
            }
        }
    }

    // Setup routine to check for new posts with a dumb timed interval...
    // Thought of patching XHR.send to detect when new items are loaded, but
    // some requests were failing with CORS issue... and other way would be
    // to use Mutation Observers, but fuck 'em because I'm feeling lazy...
    setInterval(process, 3000);

    // May the odds be in your favour
    process();
})();