YouTube Hover Copy Button Correct

Hover to show copy button on YouTube subscription videos

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

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

})();