Block youtube users / channels

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

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

// ==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);