YouTube Hover Copy Button Correct

Hover to show copy button on YouTube subscription videos

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         YouTube Hover Copy Button Correct
// @namespace    yt-copy
// @version      8.0
// @match        https://www.youtube.com/feed/subscriptions*
// @grant        none
// @description Hover to show copy button on YouTube subscription videos
// ==/UserScript==

(function() {
    'use strict';

    function injectButtons() {
        const items = document.querySelectorAll("ytd-rich-item-renderer");

        items.forEach(item => {
            if (item.querySelector(".yt-copy-btn")) return;

            const link = item.querySelector('a[href^="/watch"]');
            const thumbContainer = item.querySelector('a.yt-lockup-view-model__content-image');

            if (!link || !thumbContainer) return;

            const url = "https://www.youtube.com" + link.getAttribute("href");

            // 给缩略图容器 relative
            thumbContainer.style.position = "relative";

            // 创建按钮
            const btn = document.createElement("div");
            btn.className = "yt-copy-btn";
            btn.textContent = "复制";
            Object.assign(btn.style, {
                position: "absolute",
                top: "8px",
                right: "8px",
                background: "rgba(0,0,0,0.75)",
                color: "#fff",
                padding: "4px 8px",
                fontSize: "12px",
                borderRadius: "6px",
                cursor: "pointer",
                opacity: "0",
                transition: "opacity 0.15s",
                zIndex: "9999",
                pointerEvents: "auto"
            });

            btn.addEventListener("click", e => {
                e.preventDefault();
                e.stopPropagation();
                navigator.clipboard.writeText(url);
                btn.textContent = "已复制";
                setTimeout(() => btn.textContent = "复制", 1200);
            });

            thumbContainer.appendChild(btn);

            // ✅ 核心修复:hover 绑定在整个视频卡片
            item.addEventListener("mouseenter", () => btn.style.opacity = "1");
            item.addEventListener("mouseleave", () => btn.style.opacity = "0");
        });
    }

    // 无限滚动处理
    setInterval(injectButtons, 1500);

})();