Youtube - Remove Home, Short and Live buttons from the channel page menu

Remove unwanted or unused buttons from the channel page menu

As of 2022-11-05. See the latest version.

// ==UserScript==
// @name         Youtube - Remove Home, Short and Live buttons from the channel page menu
// @namespace    http://tampermonkey.net/
// @version      0.6
// @description  Remove unwanted or unused buttons from the channel page menu
// @author       Jens Nordström
// @match        https://www.youtube.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @license      MIT
// @grant        none
// ==/UserScript==
(function() {
    'use strict';

    function init(target) {

        // Start interval to find elements on navigation
        var runInterval = setInterval(removeButtons, 0);

        function removeButtons() {

            // Find and remove buttons
            for (var i = 0; i < target.length; i++) {
                if (target[i].children[0] != null) {
                    var buttonText = target[i].children[0].innerText;
                    if (buttonText === "HOME" || buttonText === "SHORTS" || buttonText === "LIVE") {
                        target[i].remove();
                    }
                }
            }
        }

        // Stop the interval after init or left mouse click
        setTimeout(() => {
            clearInterval(runInterval);
        }, "5000")
    }

    // Wait for DOM to find elements
    function waitForDOM(selector) {
        return new Promise(resolve => {
            if (document.querySelector(selector)) {
                return resolve(document.querySelector(selector));
            }

            const observer = new MutationObserver(mutations => {
                if (document.querySelector(selector)) {
                    resolve(document.querySelector(selector));
                    observer.disconnect();
                }
            });

            observer.observe(document.body, {
                childList: true,
                subtree: true
            });
        });
    }

    // Target element mapping
    waitForDOM('.tp-yt-paper-tab').then((target) => {
        var element = document.querySelectorAll("tp-yt-paper-tab");
        var isMouseDown = false;

        init(element);

        // Detect left mouse click to run interval on navigation
        document.addEventListener('mousedown', function(event) {
            if (event.which) isMouseDown = true;
            init(element);
        }, true);
    });
})();