Block youtube users / channels

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

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

您需要先安裝使用者腳本管理器擴展,如 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 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);