Block youtube users / channels

Prevent from seeing videos by certain users (from recommendation, search... //no in playlists)

当前为 2015-07-23 提交的版本,查看 最新版本

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

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

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

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

您需要先安装一款用户脚本管理器扩展,例如 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 recommendation, search... //no in playlists) 
// @version      1.1
// @history      1.1 minor fixes
// @match        *://www.youtube.com/*
// @exclude      *://www.youtube.com/embed/*
// @exclude      *//www.youtube.com/playlist?list=WL
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @grant        GM_getValue
// @grant        GM_setValue
// ==/UserScript==

//* INSTRUCTIONS *//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//
//  → 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)  //
//  → after saving refresh the page                                                                                           //
//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//

(function($) {

    var gmSaved = 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, *vevos, after saving refresh the page, delete all of this");
    var ytblacklist = gmSaved.split(",");    

    // add blacklist button to the menu
    $liButton = $("<li>", {class: "guide-channel guide-notification-item overflowable-list-item", id: "yt-blacklist"} );
    $aButton = $("<a>", {class: "guide-item yt-uix-sessionlink yt-valign spf-link"} );
    $spanButton = $("<span class=\"yt-valign-container\" style=\"padding-left:32px\"><span class=\"display-name no-count\"><span>Blacklist</span></span></span></span>");
    $aButton.append($spanButton);
    $liButton.append($aButton);
    $("#guide-container li.guide-section:nth-child(1) ul.guide-user-links").append($liButton);

    // element for user input
    $divInput = $("<div>", {id: "yt-blacklist-options", /*class: "yt-card",*/ css: {"display": "none", "width": "100%", "padding": "20px", "margin": "10px 0", "text-align": "center", "box-sizing": "border-box"} });
    $textarea = $("<textarea id=\"blacklist-words\" cols=\"100\" rows=\"5\" style=\"padding:5px;border:2px solid rgba(0,0,0,.13)\">"+ gmSaved + "</textarea>");
    $saveDiv = $("<div>", {id: "saveblacklistDiv", css: {"padding-top": "10px", "width": "600px", "margin": "0 auto", "text-align": "right"} } );    
    $save = $("<span>", {id: "saveblacklist", css: {"cursor": "pointer", "padding": "5px", "color": "#fff", "background": "#cc181e", "font-weight": "bold", "text-shadow": "-1px -1px 0 rgba(0,0,0,0.25)"}, html: "save"} );    
    $saved = $("<span>", {id: "saved", css: {"padding": "5px", "margin-right": "10px", "font-size": "80%"}, html: "SAVED"} );    
    $saveDiv.append($save);
    $divInput
    .append($textarea)
    .append($saveDiv);
    $("#page-container #page").prepend($divInput);

    // open and close textarea
    $("#yt-blacklist").click(function() {
        $(document).scrollTop(0);
        $("#yt-blacklist-options").slideToggle("200");
        if($saved.length) { $saved.remove(); }
    });
    // save
    $("#saveblacklist").click(function() {        
        GM_setValue("savedblocks", $('#blacklist-words').val());
        $save.before($saved);
    });

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

    var Program = {

        blacklist: ytblacklist,
        
        sClass: [".g-hovercard", ".yt-uix-tile-link", ".branded-page-module-title-text"], // the classes where usernames are

        findMatch: function(s, b) {
            $(s).each(function() {
                if( $(this).length ) { // if the element exist
                    var username = $(this).text().trim().toLowerCase();
                    var match = false;
                    b = b.trim().toLowerCase();
                    if ( b.charAt(0) == "*" ) { // wildcards
                        var part = b.split("*"),
                            item = part[1];
                        if ( username.indexOf(item) !== -1 ) {
                            match = true;
                        }
                    } else { // exact match
                        if ( username == b ) {
                            match = true;
                        }
                    }

                    if( match ) {
                        if( $(this).parents("#watch-header").length ) {
                            $("#watch-header").before("<div style=\"padding:10px 20px;margin-bottom:10px;text-aling:center;background:#cc181e;color:#fff;text-shadow:-1px -1px 0 rgba(0,0,0,0.25)\">" + b + " is in your blacklist</div>");
                            $(".yt-user-info").append("<span style=\"color:rgb(204, 24, 30);font-weight:bold\">BLACKLISTED!</span>");
                        } else {
                            $(this).closest("li").remove();
                        }
                    }
                }
            });
        },

        search: function() {
            for (var i = 0; i < this.sClass.length; i++) {
                for (var j = 0; j < this.blacklist.length; j++) {
                    this.findMatch(this.sClass[i], this.blacklist[j]);
                }
            }
        }
    };

    Program.search();

    //*
    //  code below taken from:
    //  https://hacks.mozilla.org/2012/05/dom-mutationobserver-reacting-to-dom-changes-without-killing-browser-performance/
    //*

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

})(window.jQuery);