Block youtube users / channels

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

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴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.0
// @match        http*://*.youtube.com/*
// @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", css: {"display": "none", "width": "80%", "padding": "10px", "margin": "0 auto 10px auto", "text-align": "center"} });
    $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"} );    
    $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");
    });
    // save
    $("#saveblacklist").click(function() {        
        GM_setValue("savedblocks", $('#blacklist-words').val());
        if ( !$("#saveblacklistDiv #saved").length ) { $("#saveblacklistDiv").prepend("<span id=\"saved\" style=\"padding-right:10px;font-size:.8em\"><em>please refresh the page</em> . SAVED .</span>"); }
    });

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

    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 ) {
                            $(".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);