Block youtube users / channels

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

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

// ==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... <!> no in playlists) 
// @version      2.0.1
// @match        *://www.youtube.com/*
// @exclude      *://www.youtube.com/embed/*
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/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 128 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
   
   <!> 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
    $button = $("<span>", { id: "yt-blacklist", html: "B", css: {"cursor": "pointer", "margin-right": "12px", "font-size": "20px", "vertical-align": "middle"} } );
    $("#upload-btn").after($button);
    var marginright = ( $(window).width() - ( $button.offset().left + $button.outerWidth() ) - 25 );

    // elements for user input
    $divInput = $("<div>", { id: "yt-blacklist-options", css: {"display": "none", "position": "fixed", "right": marginright+"px", "top": "51px", "z-index": "9999999999", "padding": " 0 20px 20px 20px", "text-align": "center", "background-color": "#fff", "box-shadow": "0 2px 2px 1px rgba(0,0,0,.1)"} });
    $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);

    // 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;
        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 ( !ifWhite(username) && ifMatch(username) ) { // if the username isn't whitelisted and 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() {
        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 after every change in #content
    var target = document.querySelector('#content');
    var observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            search();
        });    
    });
    var config = { childList: true };
    observer.observe(target, config);

})(window.jQuery);