Raw Source
rayanbfvr / uBlock Origin - Whitelist Youtube Channels

// ==UserScript==
// @name         uBlock Origin - Whitelist Youtube Channels
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  To whitelist Youtube channels in uBlock Origin.
// @author       Rayanbfvr
// @match        https://www.youtube.com*
// @match        https://www.youtube.com/*
// @require     https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
// @require     https://gist.github.com/raw/2625891/waitForKeyElements.js
// @run-at      content-document-global-created
// @grant       none
// @downloadURL https://openuserjs.org/install/rayanbfvr/uBlock_Origin_-_Whitelist_Youtube_Channels.js
// @updateURL https://openuserjs.org/meta/rayanbfvr/uBlock_Origin_-_Whitelist_Youtube_Channels.meta.js
// @license MIT
// ==/UserScript==

// NOTE: This currently breaks AJAX loading of video links.
// Since version 2.0, you need to provide a Youtube API key (https://developers.google.com/youtube/v3/getting-started) inside the userscript's source code.

(function() {
    'use strict';

    // Provide your key here and only here
    var key = "[YOUR KEY]";

    if(key != "[YOUR KEY]") {

        var videoElements = 'a[href*="/watch"]';
        var videoLinksElements = document.querySelectorAll('a[href*="/watch"]');

        waitForKeyElements(videoElements, addClickEvents);

    } else {
        alert("Whitelist Youtube Channels:\n Please add your Youtube API key in the userscript");
    }

    function addClickEvents(videoLinksElements) {
        for(var i = 0; i < videoLinksElements.length; i++) {
            var videoElement = videoLinksElements[i];

            $(videoElement).click(function(e) {
                var videoID = videoElement.getAttribute("href");
                videoID = videoID.replace(/^(.*?)\/watch\?v=/g,"");

                $.get( "https://www.googleapis.com/youtube/v3/videos?part=snippet&id="+videoID+"&key="+key, function( data ) {
                    videoElement.href = "https://www.youtube.com/watch?v=" + videoID + "&user=" + data.items[0].snippet.channelId;
                    window.location.href = videoElement.href;
                });

                return false;
            });
        }

    }

})();