Youtube channels open to "Videos" tab

By default, clicking on a channel while browsing Youtube loads the "Home" tab. This loads the "Videos" tab instead.

// ==UserScript==
// @name          Youtube channels open to "Videos" tab
// @description   By default, clicking on a channel while browsing Youtube loads the "Home" tab. This loads the "Videos" tab instead.
// @version       0.7
// @namespace     BawdyInkSlinger
// @author        BawdyInkSlinger
// @match         https://www.youtube.com/*
// @exclude       https://www.youtube.com/embed*
// @run-at        document-start
// @license       MIT License
// @grant         GM_registerMenuCommand
// @grant         GM_unregisterMenuCommand
// @grant         GM_setValue
// @grant         GM_getValue
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// ==/UserScript==

// original version: https://greasyfork.org/en/discussions/requests/56798-request-make-videoes-the-default-tab-on-youtube-channels#comment-455176
(async () => {
    let storage = undefined;
    initializeStorage();
    createDebugMenu();

    const channelHomeRegex = /^(https?:\/\/www\.youtube\.com)((\/(user|channel|c)\/[^/]+)|(\/@(?!.*\/)[^/]+))(\/?$|\/featured[^/])/;

    const startedOnChannel = channelHomeRegex.test(location.href);
    debug(`startedOnChannel=${startedOnChannel}`);
    if (startedOnChannel) {
        // this will get invoked when a youtube channel link is reached from a non-youtube origin page,
        // redirecting the link to /videos
        location.href = RegExp.$2 + "/videos";

        // BIS: I think there's a bug here but I've yet to prove it: If you navigate to another part of youtube, will
        // the script work due to this early return?
        return;
    }

    addEventListener('mousedown', event => {
        const anchorTag = event.target.closest('a');
        const anchorGoesToChannel = anchorTag && channelHomeRegex.test(anchorTag.href);

        debug(`anchorGoesToChannel`, anchorGoesToChannel, `anchorTag.href`, anchorTag?.href, );
        debug(`anchorTag before modification:`, anchorTag, `anchorTag.data before modification:`, anchorTag?.data);

        if (anchorGoesToChannel) {
            event.stopPropagation();
            const channelName = RegExp.$2;
            debug(`channelName`, channelName);
            location.href = channelName + "/videos";
        }
    });

    function initializeStorage() {
        storage = {
            get debugMode() {
                return GM_getValue("debugMode", false);
            },
            set debugMode(value) {
                GM_setValue("debugMode", value);
                log(`debugMode ${value ? "Enabled" : "Disabled"}`);
            }
        }
        log(`debugMode ${storage.debugMode ? "Enabled" : "Disabled"}`);
    }

    function createDebugMenu() {
        let debugMenuId = undefined;

        (function toggleMenu() {
            debugMenuId = GM_registerMenuCommand(`${storage.debugMode ? "Disable" : "Enable"} Debug Mode`, function(event) {
                storage.debugMode = !storage.debugMode;
                toggleMenu();
            }, {
                id: debugMenuId,
                title: `Debug Mode is ${storage.debugMode ? "Enabled" : "Disabled"}`,
            });
        })();
    }

    function debug(...args) {
        if (storage.debugMode) {
            console.log(`Youtube channels open to "Videos" tab:`, ...args);
        }
    }

    function log(...args) {
        console.log(`Youtube channels open to "Videos" tab:`, ...args);
    }

    function error(...args) {
        console.error(`Youtube channels open to "Videos" tab:`, ...args);
    }
})().catch((err) => {
    console.error(`Youtube channels open to "Videos" tab`, err);
});