YouTube Download Button

Adds a download button to YouTube videos

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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();

})();