YouTube Hover Copy Button Correct

Hover to show copy button on YouTube subscription videos

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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);

})();