YouTube MP3 Download Button Ready

Adds a MP3 Download button with progress bar on YouTube using RapidAPI

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         YouTube MP3 Download Button Ready
// @namespace    http://ninjasunite.com
// @version      5.0
// @description  Adds a MP3 Download button with progress bar on YouTube using RapidAPI
// @match        https://www.youtube.com/*
// @require      https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js
// @grant        GM_xmlhttpRequest
// @connect      youtube-to-mp3-download.p.rapidapi.com
// ==/UserScript==

(function() {
    'use strict';

    // --- ВАЖНО --- 
    // Замените 'YOUR_RAPIDAPI_KEY' на ваш собственный ключ RapidAPI
    const API_KEY = 'YOUR_RAPIDAPI_KEY';
    const API_HOST = 'youtube-to-mp3-download.p.rapidapi.com';

    function addDownloadButton() {
        const subButton = $('ytd-subscribe-button-renderer');
        if (subButton.length && $('#youtube2mp3').length === 0) {
            const videoUrl = window.location.href;

            // Контейнер кнопки и прогресс-бар
            const btnContainer = $('<div id="youtube2mp3-container" style="display:inline-block; margin-left:8px; position:relative;"></div>');
            const btn = $('<a id="youtube2mp3" class="yt-simple-endpoint style-scope ytd-button-renderer" style="padding:5px 12px; background-color:#ff0000; color:#fff; font-size:12px; border-radius:2px; cursor:pointer; display:inline-block;">MP3 Download</a>');
            const progressBar = $('<div id="youtube2mp3-progress" style="position:absolute; bottom:0; left:0; height:3px; background-color:#00ff00; width:0%; border-radius:2px;"></div>');

            btnContainer.append(btn).append(progressBar);
            btnContainer.insertAfter(subButton);

            btn.click(function(e) {
                e.preventDefault();

                btn.text('Converting...');
                btn.css('background-color','#ffa500');
                progressBar.css('width','0%');

                GM_xmlhttpRequest({
                    method: "GET",
                    url: `https://${API_HOST}/convert?url=${encodeURIComponent(videoUrl)}&format=mp3`,
                    headers: {
                        "X-RapidAPI-Key": API_KEY,
                        "X-RapidAPI-Host": API_HOST
                    },
                    onprogress: function(e) {
                        if (e.lengthComputable) {
                            const percent = Math.round((e.loaded / e.total) * 100);
                            progressBar.css('width', percent + '%');
                        } else {
                            progressBar.css('width', '50%');
                        }
                    },
                    onload: function(response) {
                        try {
                            const data = JSON.parse(response.responseText);
                            if (data && data.link) {
                                window.open(data.link, '_blank');
                                btn.text('MP3 Download');
                                btn.css('background-color','#ff0000');
                                progressBar.css('width','100%');
                                setTimeout(()=>progressBar.css('width','0%'), 2000);
                            } else {
                                btn.text('Error');
                                btn.css('background-color','#ff0000');
                                alert("Ошибка: не удалось получить ссылку на MP3.");
                            }
                        } catch(err) {
                            btn.text('Error');
                            btn.css('background-color','#ff0000');
                            alert("Ошибка при обработке ответа API.");
                            console.error(err);
                        }
                    },
                    onerror: function() {
                        btn.text('Error');
                        btn.css('background-color','#ff0000');
                        alert("Ошибка запроса к API.");
                    }
                });
            });
        }
    }

    // Отслеживаем динамические изменения на YouTube (SPA)
    const observer = new MutationObserver(addDownloadButton);
    observer.observe(document.body, { childList: true, subtree: true });

})();