YouTube Channel Whitelister

Helps whitelist YouTube channels in uBlock Origin

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        YouTube Channel Whitelister
// @namespace   https://tekno.pw
// @author      teknogeek
// @description Helps whitelist YouTube channels in uBlock Origin
// @include     http://*.youtube.com/*
// @include     https://*.youtube.com/*
// @version     2.2
// @grant       none
// @license     http://creativecommons.org/licenses/by-sa/4.0/
// ==/UserScript==


function setChannelName(elem) {
    // use regex to get the channel name or user ID from the element data (thanks @Google for the __data__ :D)
    var channelID = elem.__data__.data.owner.videoOwnerRenderer.navigationEndpoint.browseEndpoint.canonicalBaseUrl;
    channelID = channelID.match(/\/(?:user|channel)\/(.*)/);

    if(channelID !== null) {
        // check that the channel ID hasn't been added to the URL already
        if(location.href.search('&user=') == -1) {
            // if not, add it now
            history.pushState({}, null, `${location.href}&user=${channelID[1]}`);
            window.location.reload();
        }
    }
}

// add an observer to the page that will wait for dynamic page updates in order to properly work when a video is being loaded by JS
var observer = new MutationObserver(function(mutations) {
    mutations.forEach(function(mutation) {
        if (mutation.addedNodes !== null) {
            for (var i = 0; i < mutation.addedNodes.length; i++) {
                var elem = mutation.addedNodes[i];
                if(elem !== undefined && elem.tagName !== undefined) {
                    var elemTag = elem.tagName.toLowerCase();
                    if(elemTag == 'ytd-video-secondary-info-renderer') {
                        setChannelName(elem);
                    }
                }
            }
        }
    });
});

// add the observer to the document body
observer.observe(document.body, {childList: true, subtree: true});