YouTube Jump to Channel Videos

Redirect links in the subscription list to videos tab instead of the home tab (only works for open in new tab/window)

目前為 2021-03-04 提交的版本,檢視 最新版本

// ==UserScript==
// @name         YouTube Jump to Channel Videos
// @namespace    http://tampermonkey.net/
// @version      0.5
// @description  Redirect links in the subscription list to videos tab instead of the home tab (only works for open in new tab/window)
// @author       Nathaniel Wu
// @include      *www.youtube.com/*
// @license      Apache-2.0
// @supportURL   https://gist.github.com/Nathaniel-Wu/b9cbdc29b2b33c7d49993ef70d7993d7
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    function redirect_channel_links(subscriptions) {
        subscriptions.querySelectorAll('ytd-guide-entry-renderer').forEach(channel => {
            let link = channel.querySelector('a');
            if (!Boolean(link.href.match(/\/videos($|\/$)/)))
                link.href += "/videos";
        });
    }
    function set_listeners() {
        let subscriptions = document.querySelector('div#content > tp-yt-app-drawer#guide div#sections > ytd-guide-section-renderer:nth-of-type(2) > div#items');//subject to change
        if (subscriptions == null)
            return false;
        redirect_channel_links(subscriptions);
        let active_DOMNodeInsertion = 0;
        subscriptions.addEventListener("DOMNodeInserted", () => {
            active_DOMNodeInsertion++;
            setTimeout(() => {
                active_DOMNodeInsertion--;
                if (active_DOMNodeInsertion == 0)
                    redirect_channel_links(subscriptions);
            }, 20);
        });
        return true;
    }
    function repeat_until_successful(function_ptr, interval) {
        if (!function_ptr())
            setTimeout(() => {
                repeat_until_successful(function_ptr, interval);
            }, interval);
    }
    function in_iframe() {
        try {
            return window.self !== window.top;
        } catch (e) {
            return true;
        }
    }
    if (!in_iframe())
        window.addEventListener("load", () => { repeat_until_successful(set_listeners, 100); });
})();