Block Youtube Users

Prevent from seeing videos by certain users (from recommended, search, related channels...)

当前为 2016-12-02 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Block Youtube Users
// @author       Schegge
// @namespace    https://github.com/Schegge
// @description  Prevent from seeing videos by certain users (from recommended, search, related channels...)
// @version      2.2
// @match        *://www.youtube.com/*
// @exclude      *://www.youtube.com/embed/*
// @require      https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js
// @grant        GM_getValue
// @grant        GM_setValue
// @icon         data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAC4AAAAuCAYAAABXuSs3AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyBpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEg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PzbIED6XmOLB/PedKw3VAgiozxishGhX4XaL9OS8JKAc7X+vV0vJEOTIy9Y4M8CQB3yWbTSVtmhpz4PCqT2nWf0L4K8AAGQEtCug+rd8AAAAASUVORK5CYII=
// ==/UserScript==

/*** DESCRIPTION
  →  the program is case-insensitive
  →  you can choose the symbol to split the usernames (default is a comma) ('*' not allowed) (max 1 character)
  →  put a * in front of a word for wildcard (only in the blacklist!), it will find the word no matter its position in the username (example: *vevo)
  →  you can enable/disable to blacklist channels by clicking on '[x]' before the usernames
  →  you can suspend temporarily the block (to reactivate it just click on save or ☆ or refresh the page)
  →  it also hides videos from the playlists/mixes, but it doesn't prevent them from playing if the playlist is in autoplay

 <!> please report any bugs
 (tested on chrome 55 and firefox 50)
***/

(function($) {

    // get black/whitelist saved
    var sBL, sWL, sep, add, ytblacklist, ytwhitelist;
    function getValues() {
        sBL = GM_getValue('savedblocks', 'it is case-insensitive, split the usernames with a comma (default), put a * in front of a word for wildcard, it will find the word no matter its position in the username');
        sWL = GM_getValue('savedwhites', 'write here whitelisted usernames, if for example you blacklist *vevo, but you want to see IndilaVEVO');
        sep = GM_getValue('sep', ',');
        add = GM_getValue('enableadd', '');
        ytblacklist = sBL.split(sep);
        ytwhitelist = sWL.split(sep);
    }
    getValues();

    // where the usernames are
    //                                         related channels                                         playlist/mixes
    var uClasses = ['#content .g-hovercard', '.branded-page-related-channels-list .yt-uix-tile-link', '.video-uploader-byline'];

    // elements for user input
    var margintop = $('#yt-masthead-container').height() + parseInt($('#yt-masthead-container').css('padding-top')) + parseInt($('#yt-masthead-container').css('padding-bottom'));

    $('head').append('<style> ' +
                     'li.li-is-black, tr.tr-is-black { display: none!important; } ' +
                     '#yt-blacklist { cursor: pointer; margin-right: 2px; font-size: 22px; vertical-align: middle; } ' +
                     '#yt-blacklist-research { cursor: pointer; font-size: 12px; vertical-align: top; margin-right: 5px; } ' +
                     '#yt-blacklist-options { width: 500px; display: flex; flex-flow: row wrap; align-items: baseline; position: fixed; right: 70px; top:' + margintop + 'px; padding: 0 20px 15px; background-color: #fff; box-shadow: 0 1px 1px 0 rgba(0,0,0,.1); border: 1px solid #e8e8e8; border-top: 0; z-index: 9999999999; } ' +
                     '#yt-blacklist-options div { box-sizing: border-box; padding: 5px; } ' +
                     '#yt-blacklist-options .textarea div { width: 100%; text-align: center; font-weight: 500; } ' +
                     '#yt-blacklist-options .textarea textarea { resize: vertical; width: 100%; padding: 4px; border: 2px solid rgba(0,0,0,.13); box-sizing: border-box; } ' +
                     '#yt-blacklist-options .textarea.wl { width: 45%; } ' +
                     '#yt-blacklist-options .textarea.bl { width: 55%; } ' +
                     '#saveblacklist { cursor: pointer; color: #cc181e; text-shadow: 1px 1px 1px rgba(0, 0, 0, .25); border-radius: 2px; } ' +
                     '.yt-blacklist-sep { width: 33%; font-size: 9px; color: rgba(0,0,0,.5); } ' +
                     '#sep-symbol { width: 10px; background: #fff; border: 1px dotted rgba(0,0,0,.13); padding: 0 2px; color: #000; } ' +
                     '#yt-blacklist-suspend { width: 33%; cursor: pointer; font-size: 70%; color: rgba(0,0,0,.5); text-align: right; } ' +
                     '#yt-blacklist-enableadd { width: 33%; cursor: pointer; font-size: 70%; color: rgba(0,0,0,.5); text-align: center; } ' +
                     '.yt-blacklist-add { margin-right: .5em; cursor: pointer; color: #cc181e; font-size: 70%; font-family: consolas, monospace; vertical-align: top; }' +
                     '</style>');

    $('<div style="display: inline-block; position: relative; height: 28px"><span id="yt-blacklist">B</span><span id="yt-blacklist-research">&#9734;</span></div>').insertAfter('#upload-btn');

    $('body').append('<div id="yt-blacklist-options" style="display: none">' +
                     '<div style="width: 100%; text-align: right"><span id="saveblacklist">save</span></div>' +
                     '<div class="textarea wl"><div>Whitelist</div><textarea rows="4" id="whitelist-words">' + sWL + '</textarea></div>' +
                     '<div class="textarea bl"><div>Blacklist</div><textarea rows="4" id="blacklist-words">' + sBL + '</textarea></div>' +
                     '<div class="yt-blacklist-sep">separator: <input id="sep-symbol" type="text" value="' + sep + '" maxlength="1" /></div>' +
                     '<div id="yt-blacklist-enableadd">enable click add</div>' +
                     '<div id="yt-blacklist-suspend">suspend block</div>' +
                     '</div>');
    if (add) $('#yt-blacklist-enableadd').text('disable click add');

    // check if a username is whitelisted
    function ifWhite(u) {
        var whitelisted = false;
        for(var z = 0; z < ytwhitelist.length; z++) {
            var w = ytwhitelist[z].trim().toLowerCase();
            if (w.length && u == w) {
                whitelisted = true;
            }
        }
        return whitelisted;
    }

    // check if a username is blacklisted
    function ifMatch(u) {
        var match = false;
        if (!ifWhite(u)) { // if the username isn't whitelisted
            for (var j = 0; j < ytblacklist.length; j++) {
                var b = ytblacklist[j].trim().toLowerCase();
                if (b.charAt(0) == '*') { // wildcards
                    var part = b.split('*'),
                        item = part[1];
                    if (item.length && u.indexOf(item) !== -1) {
                        match = true;
                    }
                } else { // exact match
                    if (b.length && u == b) {
                        match = true;
                    }
                }
            }
        }
        return match;
    }

    // do the thing
    function findMatch(s) {
        $(s).each(function() {
            var username = $(this).text().trim().toLowerCase();
            if (!username) return 'continue';

            // if the username is blacklisted
            if (ifMatch(username)) {
                if ($(this).closest("tr").hasClass("tr.pl-video")) { // PLAYLIST PAGE
                    if (!$(this).closest("tr").hasClass("tr-is-black")) {
                        $(this).closest("tr").addClass("tr-is-black");
                    }
                } else { // SEARCH, RECOMMENDED, etc...
                    if (!$(this).closest("li").hasClass("li-is-black")) {
                        $(this).closest("li").addClass("li-is-black");
                    }
                }
            } else {
                // button to add names to blacklist
                if (add && !$(this).siblings('.yt-blacklist-add').length && !$(this).hasClass('comment-author-text')) {
                    $('<span class="yt-blacklist-add" data="' + username + '">[x]</span>').insertBefore($(this));
                }
            }
        });
    }

    // the final search function
    function search() {
        var url = window.location.href;
        // playlist?list=WL = Watch Later | != feed/t... = History, Subscriptions
        if (!/.*youtube\.com\/(playlist\?list=WL|feed\/[^t]\w+)/.test(url)) {
            for (var i = 0; i < uClasses.length; i++) {
                findMatch(uClasses[i]);
            }
        }
    }

    // search when youtube is first opened
    search();

    // open and close options
    $('#yt-blacklist').on('click', function() {
        $('#yt-blacklist-options').slideToggle();
    });

    // save blacklist changes and research
    $saved = $('<span style="margin-right: 7px; font-size: 80%">saved</span>');
    $error = $('<span style="margin-right: 7px; font-size: 80%; color: red">ERROR! * NOT ALLOWED AS SEPARATOR</span>');
    $('#saveblacklist').on('click', function() {
        if ($('#sep-symbol').val() == '*') {
            $(this).before($error);
            setTimeout(function() { $error.remove(); }, 4000);
        } else {
            // save new values
            GM_setValue('savedblocks', $('#blacklist-words').val());
            GM_setValue('savedwhites', $('#whitelist-words').val());
            GM_setValue('sep', $('#sep-symbol').val());
            // add notification
            $(this).before($saved);
            setTimeout(function() { $saved.remove(); }, 2000);
            // clear everything
            $(".li-is-black").removeClass("li-is-black");
            $(".tr-is-black").removeClass("tr-is-black");
            // research
            getValues();
            search();
        }
    });

    // research and suspend
    $('#yt-blacklist-research').on('click', search);
    $('#yt-blacklist-suspend').on('click', function() {
        $(".li-is-black").removeClass("li-is-black");
        $(".tr-is-black").removeClass("tr-is-black");
    });

    // enable/disable click add
    $('#yt-blacklist-enableadd').on('click', function() {
        if (add) {
            add = '';
            $('.yt-blacklist-add').remove();
            $(this).text('enable click add');
        } else {
            add = 'yes';
            $(this).text('disable click add');
        }
        GM_setValue('enableadd', add);
        search();
    });

    // add usernames to blacklist
    $('body').on('click', '.yt-blacklist-add', function(e) {
        e.preventDefault();
        e.stopPropagation();
        var q = sBL ? sep + ' ' : '';
        $('#blacklist-words').val($('#blacklist-words').val() + q + $(this).attr('data'));
        GM_setValue('savedblocks', $('#blacklist-words').val());
        getValues();
        search();
    });

    // research after every change in #content
    var target = document.querySelector('#content');
    var config = { attributes: false, childList: true, characterData: false, subtree: true };
    var observer = new MutationObserver(function(mutations) { search(); });

    try { observer.observe(target, config); } catch (e) {}

})(jQuery);