Block youtube users / channels

Prevent from seeing videos by certain users (from recommended, search, related channels... <!> no in playlists)

目前為 2015-07-27 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Block youtube users / channels
// @author       Schegge
// @namespace    https://greasyfork.org/en/users/12632-schegge
// @description  Prevent from seeing videos by certain users (from recommended, search, related channels... <!> no in playlists) 
// @version      2.1.5
// @match        *://www.youtube.com/*
// @exclude      *://www.youtube.com/embed/*
// @require      https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js
// @grant        GM_getValue
// @grant        GM_setValue
// ==/UserScript==

/****************************************

   →  the program is case-insensitive
   →  split the usernames with a comma
   →  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)
   →  on line 139 the channels' hovercards are disabled, comment it if you want them (they seem to cause a problem on the MutationObserver, but it does NOT broke this script anyway, so whatever)
   →  on line 142 you can change the pages in which the search is excluded
   →  VERSION 2:
       # blacklist editor changed
       # whitelist added
       # no need to refresh the page after changes in the blacklist/whitelist
       # added ☆ button to force a new search (in case of problems?)

   <!> please report any bugs

****************************************/

(function($) {

    var sBL, sWL, ytblacklist, ytwhitelist;    
    function getValues() {
        sBL = GM_getValue("savedblocks", "the program is case-insensitive, split the usernames with a comma, put a * in front of a word for wildcard, it will find the word no matter its position in the username, example, *vevo, delete all of this");
        sWL = GM_getValue("savedwhites", "put here whitelisted usernames, if for example you blacklist *vevo, but you want to see IndilaVEVO, write here:, indilavevo, delete all of this");
        ytblacklist = sBL.split(",");
        ytwhitelist = sWL.split(",");
    }
    getValues();

    // add blacklist button to the menu
    $buttonB = $("<span>", { id: "yt-blacklist", html: "B", css: {"cursor": "pointer", "margin-right": "2px", "font-size": "20px", "vertical-align": "middle"} } );
    $buttonS = $("<span>", { id: "yt-blacklist-research", html: "&#9734", css: {"cursor": "pointer", "margin-right": "5px", "font-size": "12px", "vertical-align": "top"} } );
    $("#upload-btn")
    .after($buttonS)
    .after($buttonB);

    var marginright = ( $(window).width() - $buttonB.offset().left - $buttonB.outerWidth() - $buttonS.outerWidth() - 20 );
    var margintop = $("#yt-masthead-container").height() + parseInt($("#yt-masthead-container").css("padding-top")) + parseInt($("#yt-masthead-container").css("padding-bottom"));

    // elements for user input
    $divInput = $("<div>", { id: "yt-blacklist-options", css: {"display": "none", "position": "fixed", "right": marginright+"px", "top": margintop+"px", "padding": "0 20px 20px 20px", "text-align": "center", "background-color": "#fff", "box-shadow": "0 2px 2px 1px rgba(0,0,0,.1)", "z-index": "99999999999"} });
    $textareaBL = $("<div style=\"display:inline-block;width:350px;vertical-align:top;margin:0 5px;box-sizing:border-box\"><div style=\"text-align:center;font-weight:500;margin-bottom:5px\">Blacklist</div><textarea id=\"blacklist-words\" style=\"resize:vertical;width:100%;height:80px;padding:4px;border:2px solid rgba(0,0,0,.13);box-sizing:border-box\">"+ sBL + "</textarea></div>");
    $textareaWL = $("<div style=\"display:inline-block;width:250px;vertical-align:top;margin:0 5px;box-sizing:border-box\"><div style=\"text-align:center;font-weight:500;margin-bottom:5px\">Whitelist</div><textarea id=\"whitelist-words\" style=\"resize:vertical;width:100%;height:80px;padding:4px;border:2px solid rgba(0,0,0,.13);box-sizing:border-box\">"+ sWL + "</textarea></div>");
    $saveDiv = $("<div>", { id: "saveblacklistDiv", css: {"clear": "both", "padding-bottom": "10px", "margin": "1px 0 0 0", "text-align": "right"} } );
    $save = $("<span>", { id: "saveblacklist", css: {"cursor": "pointer", "color": "#cc181e", "text-shadow": "1px 1px 1px rgba(0,0,0,0.25)", "border-radius": "2px"}, html: "save" });    
    $saved = $("<span>", { css: {"margin-right": "7px", "font-size": "80%"}, html: "saved and searched again" });
    $saveDiv.append($save);
    $divInput
    .append($saveDiv)
    .append($textareaWL)
    .append($textareaBL);
    $("body").append($divInput);

    $researched = $("<div>", { html: "done!", css: {"position": "fixed", "right": marginright+20+"px", "top": margintop-12+"px", "font-size": "80%", "z-index": "999999999999"} });

    // open and close textareas
    $("#yt-blacklist").click(function() {
        $("#yt-blacklist-options").slideToggle();
    });

    // 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 ( ifMatch(username) ) { // if the username is blacklisted
                if ( $(this).parents("#watch-header").length ) { // on the watch page
                    if ( !$(this).siblings(".span-is-black").length ) { // check if it wasn't already blacklisted
                        $(".yt-user-info").append("<span class=\"span-is-black\" style=\"color:rgb(204, 24, 30);font-weight:500\">BLACKLISTED!</span>");
                    }
                } else {
                    if ( !$(this).parents(".li-is-black").length ) { // check if it wasn't already blacklisted
                        $(this).closest("li").addClass("li-is-black").hide();
                    }
                }
            } else { // if a previous black/whitelist word is deleted/added
                if ( $(this).siblings(".span-is-black").length ) {
                    $(this).siblings(".span-is-black").remove();
                }
                if ( $(this).parents(".li-is-black").length ) {
                    $(this).parents(".li-is-black").removeClass("li-is-black").show();
                }
            }
        });
    }

    // where the usernames are
    var uClasses = [".g-hovercard", ".branded-page-related-channels-list"];

    // the final search function
    function search() {
        if ( yt.config_.UNIVERSAL_HOVERCARDS ) { yt.config_.UNIVERSAL_HOVERCARDS = false; }

        var url = window.location.pathname;
        if ( url != ( "/feed/history" || "/playlist?list=WL" || "/feed/subscriptions" ) ) {
            for (var i = 0; i < uClasses.length; i++) {
                findMatch(uClasses[i]);
            }
        }
    }

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

    // save blacklist changes and research
    $("#saveblacklist").click(function() {
        GM_setValue("savedblocks", $('#blacklist-words').val());
        GM_setValue("savedwhites", $('#whitelist-words').val());
        getValues();
        search();
        $save.before($saved);
        setTimeout(function() {
            $saved.remove();
        }, 3000);
    });  

    // research when the $buttonS is clicked
    $("#yt-blacklist-research").click(function() {        
        search();
        $("#yt-masthead-user").append($researched);
        setTimeout(function() {
            $researched.remove();
        }, 2000);
    });

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

})(window.jQuery);