YouTube Autoplay Buttons. Adding “Play all videos” buttons back

Adds 3 buttons on each YouTube profile under Videos (All videos + shorts, All videos, Shorts only)

As of 2024-10-04. See the latest version.

// ==UserScript==
// @name         YouTube Autoplay Buttons. Adding “Play all videos” buttons back
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Adds 3 buttons on each YouTube profile under Videos (All videos + shorts, All videos, Shorts only)
// @author       Nieme
// @match        https://www.youtube.com/*
// @grant        none
// @license      GPLv2
// ==/UserScript==

(function() {
    'use strict';

    function getChannelId() {
        let cId = null;

        if (typeof ytInitialData !== 'undefined') {
            cId = ytInitialData?.metadata?.channelMetadataRenderer?.externalId;
        }

        if (!cId) {
            cId = document.querySelector('.ytp-ce-link[href]')?.href?.split('/')?.pop() ||
                  document.querySelector('[itemprop="identifier"]')?.content;
        }

        return cId;
    }

    function addCustomButtons() {
        const chipsContainer = document.querySelector('ytd-feed-filter-chip-bar-renderer #chips');
        if (chipsContainer && !document.querySelector('.custom-play-all-button')) {
            
            function createButton(text, playlistLinkGenerator) {
                const newButton = document.createElement('a');
                newButton.style.marginLeft = '10px';
                newButton.href = '#';
                newButton.classList.add('custom-play-all-button');
                
                const newText = document.createTextNode(text);  // Text direkt als Text-Node einfügen
                newButton.appendChild(newText);

                newButton.addEventListener('click', (e) => {
                    e.preventDefault();

                    const cId = getChannelId();

                    if (cId) {
                        const playlistLink = playlistLinkGenerator(cId);
                        window.location.href = playlistLink;
                    } else {
                        console.error('Channel-ID konnte nicht abgerufen werden.');
                    }
                });

                chipsContainer.appendChild(newButton);
            }

            createButton('Play Standard and Shorts together', (cId) => `https://www.youtube.com/playlist?list=UU${cId.slice(2)}`);
            createButton('Play Standard only', (cId) => `https://www.youtube.com/playlist?list=UULF${cId.slice(2)}`);
            createButton('Play Shorts only', (cId) => `https://www.youtube.com/playlist?list=UUSH${cId.slice(2)}`);
        }
    }

    function checkAndAddButtons() {
        if (window.location.pathname.includes('/videos')) {
            addCustomButtons();
        }
    }

    // CSS-Regel, um sicherzustellen, dass der Text sichtbar ist
    const style = document.createElement('style');
    style.innerHTML = `
        .custom-play-all-button {
            font-size: 14px;
            color: #000;  /* Textfarbe sicherstellen */
            text-decoration: none;
        }
    `;
    document.head.appendChild(style);

    const observer = new MutationObserver(() => {
        checkAndAddButtons();
    });

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

    let oldHref = document.location.href;
    setInterval(() => {
        const currentHref = document.location.href;
        if (oldHref !== currentHref) {
            oldHref = currentHref;
            checkAndAddButtons();
        }
    }, 5000);

})();