YouTube 儲存快捷按鈕

在主按鈕列新增一個「儲存」快捷鍵,點擊自動展開選單並點擊儲存

当前为 2025-08-20 提交的版本,查看 最新版本

// ==UserScript==
// @name         YouTube 儲存快捷按鈕
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  在主按鈕列新增一個「儲存」快捷鍵,點擊自動展開選單並點擊儲存
// @author       shanlan(ChatGPT o3-mini)
// @match        https://www.youtube.com/*
// @grant        none
// @run-at       document-end
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // 監聽頁面變動
    const observer = new MutationObserver(() => {
        addSaveShortcut();
    });
    observer.observe(document.body, { childList: true, subtree: true });

    function addSaveShortcut() {
        // 主按鈕列
        const topBtns = document.querySelector('#top-level-buttons-computed');
        if (!topBtns) return;

        // 避免重複插入
        if (topBtns.querySelector('.yt-save-shortcut-btn')) return;

        // 建立自製按鈕
        const btn = document.createElement('button');
        btn.className = 'yt-spec-button-shape-next yt-spec-button-shape-next--tonal yt-spec-button-shape-next--mono yt-spec-button-shape-next--size-m yt-spec-button-shape-next--icon-leading yt-save-shortcut-btn';
        btn.style.marginLeft = '8px';
        btn.innerHTML = `
            <div aria-hidden="true" class="yt-spec-button-shape-next__icon">
                <svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24" style="vertical-align:middle"><path d="M18 4v15.06l-5.42-3.87-.58-.42-.58.42L6 19.06V4h12m1-1H5v18l7-5 7 5V3z"></path></svg>
            </div>
            <div class="yt-spec-button-shape-next__button-text-content">儲存</div>
        `;

        btn.onclick = function(e) {
            e.stopPropagation();
            // 1. 觸發「更多」選單
            let moreBtn = document.querySelector('ytd-menu-renderer yt-icon-button#button, ytd-menu-renderer yt-button-shape#button-shape');
            if (!moreBtn) {
                alert('找不到「更多」選單按鈕,請回報腳本作者');
                return;
            }
            moreBtn.click();

            // 2. 等待選單出現後自動點擊「儲存」
            let tryCount = 0;
            const tryClickSave = setInterval(() => {
                tryCount++;
                // 找到「儲存」選單項目
                let saveItem = Array.from(document.querySelectorAll('ytd-menu-popup-renderer tp-yt-paper-listbox ytd-menu-service-item-renderer')).find(item => {
                    return item.innerText.trim().includes('儲存');
                });
                if (saveItem) {
                    saveItem.click();
                    clearInterval(tryClickSave);
                }
                if (tryCount > 20) { // 最多嘗試2秒
                    clearInterval(tryClickSave);
                }
            }, 100);
        };

        topBtns.appendChild(btn);
    }
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址