Byfandom Blacklist

Hide posts on Byfandom you don't want to see.

Tendrás que instalar una extensión para tu navegador como Tampermonkey, Greasemonkey o Violentmonkey si quieres utilizar este script.

You will need to install an extension such as Tampermonkey to install this script.

Tendrás que instalar una extensión como Tampermonkey o Violentmonkey para instalar este script.

Necesitarás instalar una extensión como Tampermonkey o Userscripts para instalar este script.

Tendrás que instalar una extensión como Tampermonkey antes de poder instalar este script.

Necesitarás instalar una extensión para administrar scripts de usuario si quieres instalar este script.

(Ya tengo un administrador de scripts de usuario, déjame instalarlo)

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

(Ya tengo un administrador de estilos de usuario, déjame instalarlo)

// ==UserScript==
// @name         Byfandom Blacklist
// @description  Hide posts on Byfandom you don't want to see.
// @namespace    byfandom
// @include      http*://*.byfandom.org*
// @grant        none
// @version      0.9
// ==/UserScript==

/* CONFIG */

window.blacklistConfig = {

    // NOTE: * can be used as a wildcard, but this may slow down the blacklist script

    postBlacklist: ['Jar-Jar Binks', 'friendly reminder that * is problematic', "howard stark's a+ parenting"],
    // hides posts that contain text matching a term from the list

    postWhitelist: ['@myusername'],
    // shows posts that contain any of these terms, even if they match the blacklist

    showReasons: true,
    // set to false if you don't want to see why posts were hidden

};

/* END CONFIG */

(function($) {
    var _posts = $('div.ow_ipc_info');
    _posts = _posts.add('div.ow_newsfeed_body');
    _posts = _posts.add('div.ow_photo_item'); // not currently working
    
    // wrapper classes for whole posts, for a future no-placeholder option:
    // div.ow_ipc, li.ow_newsfeed_item, div.ow_photo_item_wrap

    var _toggleClass = 'blacklist-toggle';
    var _cfg = getConfig();

    _posts.each(function() {
        var reason = shouldBlockPost($(this));
        if(reason) {
            blockPost($(this), reason);
        }
    });

    function getConfig() {
        var config = window.blacklistConfig || {};

        config.showReasons = (config.showReasons !== undefined) ? config.showReasons : true;
        config.postBlacklist = config.postBlacklist || [];
        config.postWhitelist = config.postWhitelist || [];

        config.postBlacklist = convertToRegexen(config.postBlacklist);
        config.postWhitelist = convertToRegexen(config.postWhitelist);

        return config;

        function convertToRegexen(termList) {
            var term, escapedTerm;
            for(var i = 0; i < termList.length; i++) {
                term = termList[i];
                if(term.indexOf('*') != -1) {
                    escapedTerm = term.replace(/[-\/\\^$+?.()|[\]{}]/g, '\\$&');
                    escapedTerm = escapedTerm.replace('*', '.*');
                    termList[i] = { asString: term, asRegex: new RegExp(escapedTerm, "i") };
                } else {
                    termList[i] = { asString: term, asRegex: null };
                }
            }
            return termList;
        }
    }

    function blockPost(post, reason) {
        var placeholder = makePlaceholder(reason);
        var originalContent = post.children().addClass(_toggleClass);

        post.addClass('blacklisted-post');
        post.prepend(placeholder);
        originalContent.hide();

        post.find('a.blacklist-toggle-control').click(function() {
            var thisPost = $(this).closest('.blacklisted-post');
            var thisContent = thisPost.children('.' + _toggleClass).toggle();
            $(this).text(thisContent.first().is(':visible') ? 'Hide' : 'Unhide');
        });

        function makePlaceholder(reason) {
            var blacklistText = 'Blacklisted post';
            if(_cfg.showReasons) {
                blacklistText += ': contains the term "' + reason + '"';
            }
            var toggleLink = '<a class="blacklist-toggle-control">Unhide</a>';

            var plStr = "<div class='blacklist-placeholder'>";
            plStr += "<p>" + blacklistText + " (" + toggleLink + ")</p>";
            plStr += "</div>";
            return plStr;
        }
    }

    function shouldBlockPost(post) {
        var postText = post.text();
        var wl = _cfg.postWhitelist;
        var bl = _cfg.postBlacklist;

        for (var i = 0; i < wl.length; i++) {
            if(hasTerm(postText, wl[i])) {
                return '';
            }
        }

        for (i = 0; i < bl.length; i++) {
            if(hasTerm(postText, bl[i])) {
                return bl[i].asString;
            }
        }

        return '';

        function hasTerm(src, term) {
            if(term.asRegex == null) {
                return src.toLowerCase().indexOf(term.asString.toLowerCase()) != -1;
            }
            return src.match(term.asRegex) != null;
        }
    }



})(window.jQuery);