YouTube Hover Copy Button Correct

Hover to show copy button on YouTube subscription videos

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(У мене вже є менеджер скриптів, дайте мені встановити його!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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);

})();