YouTube Hover Copy Button Correct

Hover to show copy button on YouTube subscription videos

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

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

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

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

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

})();