复制njav影片封面到剪贴板

复制njav影片封面到剪贴板,在中文和英文语言中。

// ==UserScript==
// @name         复制njav影片封面到剪贴板
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  复制njav影片封面到剪贴板,在中文和英文语言中。
// @author       Maktub
// @match        https://njav.tv/zh/v/*
// @match        https://njav.tv/en/v/*
// @grant        GM_addStyle
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // 创建按钮
    const button = document.createElement('button');
    button.innerHTML = '复制封面';
    button.style.position = 'fixed';
    button.style.top = '10px';
    button.style.right = '10px';
    button.style.zIndex = 1000;
    button.style.padding = '10px';
    button.style.backgroundColor = '#008CBA';
    button.style.color = 'white';
    button.style.border = 'none';
    button.style.borderRadius = '5px';
    button.style.cursor = 'pointer';
    button.style.boxShadow = '0 4px 8px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19)';

    // 添加按钮到页面
    document.body.appendChild(button);

    // 按钮点击事件
    button.addEventListener('click', async () => {
        // 查找具有data-poster属性的div标签
        const div = document.querySelector('div[data-poster]');
        if (div) {
            const posterUrl = div.getAttribute('data-poster');
            if (posterUrl) {
                try {
                    // 获取图片数据
                    const response = await fetch(posterUrl);
                    const blob = await response.blob();

                    // 使用canvas加载图片并转换为Blob
                    const img = new Image();
                    img.crossOrigin = 'Anonymous';
                    img.src = URL.createObjectURL(blob);

                    img.onload = () => {
                        const canvas = document.createElement('canvas');
                        canvas.width = img.width;
                        canvas.height = img.height;
                        const ctx = canvas.getContext('2d');
                        ctx.drawImage(img, 0, 0);

                        canvas.toBlob(async (canvasBlob) => {
                            const clipboardItem = new ClipboardItem({'image/png': canvasBlob});
                            try {
                                await navigator.clipboard.write([clipboardItem]);
                                alert('已复制');
                            } catch (error) {
                                console.error('Failed to copy image to clipboard:', error);
                                alert('未复制');
                            }
                        }, 'image/png');
                    };

                    img.onerror = () => {
                        alert('图像加载失败');
                    };
                } catch (error) {
                    console.error('Failed to copy image to clipboard:', error);
                    alert('复制失败.');
                }
            } else {
                alert('属性已更改,等待更新.');
            }
        } else {
            alert('属性已更改,等待更新.');
        }
    });
})();