YouTube Download Button

Adds a download button to YouTube videos

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         YouTube Download Button
// @namespace    icycoldveins
// @version      0.4
// @description  Adds a download button to YouTube videos
// @author       icycoldveins
// @match        https://www.youtube.com/watch?v=*
// @license      MIT
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function addDownloadButton() {
        if (document.getElementById('downloadBtn')) return; // If button already exists, exit the function

        let downloadLink = document.createElement('a');
        downloadLink.setAttribute('id', 'downloadBtn');
        downloadLink.setAttribute('href', `https://www.y2mate.com/youtube/${window.location.search.split('v=')[1]}`);
        downloadLink.setAttribute('target', '_blank');
        downloadLink.innerText = 'Download Video';
        downloadLink.style.display = 'block';
        downloadLink.style.marginTop = '10px';
        downloadLink.style.background = '#ff0000';
        downloadLink.style.color = '#ffffff';
        downloadLink.style.padding = '5px 10px';
        downloadLink.style.textAlign = 'center';
        downloadLink.style.borderRadius = '2px';
        downloadLink.style.textDecoration = 'none';

        let parentDiv = document.querySelector('ytd-subscribe-button-renderer');
        if (parentDiv) {
            parentDiv.parentElement.insertBefore(downloadLink, parentDiv.nextSibling);
        }
    }

    // Since YouTube is a single-page application and it dynamically loads content,
    // we use a MutationObserver to listen for changes and add the download button when necessary.
    const targetNode = document.getElementById('content');
    const config = { attributes: true, childList: true, subtree: true };

    const callback = function(mutationsList, observer) {
        for (let mutation of mutationsList) {
            if (mutation.type === 'childList') {
                addDownloadButton();
            }
        }
    };

    const observer = new MutationObserver(callback);
    observer.observe(targetNode, config);

    // Add the download button initially
    addDownloadButton();

})();