Youtube download button - yt1s.com (revised SuchtiOnTour Edit)

This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください。
// ==UserScript==
// @name            Youtube download button - yt1s.com (revised SuchtiOnTour Edit)
// @name:de         Youtube download button - yt1s.com (revised SuchtiOnTour Edit)
// @namespace       Violentmonkey Scripts
// @match           https://www.youtube.com/watch
// @match           https://*.youtube.com/*
// @grant           GM_addStyle
// @run-at          document-idle
// @version         1.2.5
// @author          SuchtiOnTour
// @license         MIT
// @description     This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:pt-BR This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:ar This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:bg This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:cs This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:da This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:de This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:el This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:eo This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:es This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:fi This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:fr This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:fr-CA This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:he This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:hu This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:id This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:it This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:ja This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:ko This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:nb This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:nl This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:pl This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:ro This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:ru This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:sk This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:sr This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:sv This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:th This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:tr This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:uk This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:ug This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:vi This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:zh-CN This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// @description:zh-TW This Script Adds a Download Button next to the subscribe button, you can easily download Audio/Video
// ==/UserScript==

(function() {
    const API = "https://yt1s.com/en/youtube-to-mp3?q=";
    const BUTTON_ID = "dwnldBtn";
    const TARGET_BUTTON = "#owner"; // Updated target element selector

    const buttonStyle = `
        #${BUTTON_ID} {
            background-color: #0F0F0F;
            color: #FFFFFF;
            border: 1px solid #3F3F3F;
            border-color: rgba(255,255,255,0.2);
            margin-left: 8px; /* Decreased margin-left to 8px for smaller gap */
            padding: 0 16px;
            border-radius: 18px; /* Updated border radius */
            font-size: 14px;
            font-family: Roboto, Noto, sans-serif;
            font-weight: 500;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            height: 36px;
            line-height: normal;
        }
        #${BUTTON_ID}:hover {
            background-color: #3F3F3F;
            color: #ffffff;
            border-color: #3F3F3F;
        }
    `;

    GM_addStyle(buttonStyle);

    function waitForElement(selector) {
        return new Promise(resolve => {
            if (document.querySelector(selector)) {
                console.log(`[Youtube Download Button]: Video Found, checking if button exists...`);
                return resolve(document.querySelector(selector));
            }
            const observer = new MutationObserver(mutations => {
                if (document.querySelector(selector)) {
                    console.log(`[Youtube Download Button]: Video Found by observer, checking if button exists...`);
                    resolve(document.querySelector(selector));
                    observer.disconnect();
                }
            });
            observer.observe(document.body, { childList: true, subtree: true });
        });
    }

    function addButton() {
        waitForElement(TARGET_BUTTON).then((btnContainer) => {
            if (!btnContainer) {
                return;
            }

            if (document.querySelector(`#${BUTTON_ID}`)) { // Check if the button already exists
                console.log(`[Youtube Download Button]: Download button exists`);
            } else {
                const downloadButton = document.createElement('a');
                downloadButton.href = `${API + encodeURIComponent(window.location.href)}`;
                downloadButton.target = '_blank';
                downloadButton.id = BUTTON_ID;
                downloadButton.innerText = 'Download';
                btnContainer.appendChild(downloadButton);
                console.log(`[Youtube Download Button]: Button added successfully`);
            }
        }).catch(error => {
            console.error(`[Youtube Download Button]: Error adding button: ${error}`);
        });
    }

    function updateButton() {
        waitForElement(`#${BUTTON_ID}`).then((btn) => {
            if (!btn) {
                return;
            }
            btn.href = API + encodeURIComponent(window.location.href);
        }).catch(error => {
            console.error(`[Youtube Download Button]: Error updating button: ${error}`);
        });
    }

    let buttonAdded = false;

    function checkAndAddButton() {
        if (window.location.pathname === '/watch' && !buttonAdded) {
            addButton();
            buttonAdded = true;
            setTimeout(updateButton, 2000); // Delay of 2 seconds for update
        }
    }

    window.addEventListener("yt-navigate-finish", () => {
        buttonAdded = false; // Reset flag when navigation happens
        checkAndAddButton();
    });

    checkAndAddButton(); // Initial check when script runs
})();