Greasy Fork

Greasy Fork is available in English.

link工具

点击复制,提取链接

目前为 2024-07-05 提交的版本,查看 最新版本

// ==UserScript==
// @name         link工具
// @namespace    http://tampermonkey.net/
// @version      0.5.2
// @description  点击复制,提取链接
// @author       lwj
// @match        https://*
// @license      MIT
// @grant        GM_setClipboard
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function () {
    // 检查当前页面 URL 是否匹配指定的网址
    var isHomeURL = function () {
        return window.location.href.indexOf("https://m.linkmcn.com/#/live/plan?select=") !== -1;
    };

    var isTableCardURL = function () {
        return window.location.href.indexOf("https://m.linkmcn.com/#/live/plan/tableCard/") !== -1;
    };

    // 初始化开关状态
    var copySwitchState = localStorage.getItem('copySwitchState') === 'true';
    var toggleSwitchState = localStorage.getItem('toggleSwitchState') === 'true';
    var mainOnlyItemIdSwitchState = localStorage.getItem('mainOnlyItemIdSwitchState') === 'true';
    var tableCardPngSwitchState = localStorage.getItem('tableCardPngSwitchState') === 'true';

    var notificationTimer; // 通知计时器

    // 创建开关容器元素
    var switchesContainer = document.createElement('div');
    switchesContainer.classList.add('flex', 'items-center', 'justify-between', 'pb-12');
    switchesContainer.style.cssText = 'position: fixed; top: 20px; left: 50%; transform: translateX(-50%); z-index: 9999;';
    if (isHomeURL()) {
        document.body.appendChild(switchesContainer);
    }

    // 创建点击复制开关元素
    var copySwitchContainer = document.createElement('div');
    copySwitchContainer.classList.add('flex', 'items-center');

    var copySwitchText = document.createElement('span');
    copySwitchText.textContent = '点击店名复制';
    copySwitchText.classList.add('mr-8', 'ml-4', 'lh-22');
    copySwitchContainer.appendChild(copySwitchText);

    var copySwitch = document.createElement('button');
    copySwitch.innerHTML = '<div class="ant-switch-handle"></div><span class="ant-switch-inner"><span class="ant-switch-inner-checked"></span><span class="ant-switch-inner-unchecked"></span></span>';
    copySwitch.setAttribute('type', 'button');
    copySwitch.setAttribute('role', 'switch');
    copySwitch.setAttribute('aria-checked', copySwitchState); // 设置开关状态
    copySwitch.classList.add('ant-switch', 'css-9fw9up');
    if (copySwitchState) {
        copySwitch.classList.add('ant-switch-checked'); // 开启
    }
    copySwitchContainer.appendChild(copySwitch);

    switchesContainer.appendChild(copySwitchContainer);

    // 创建下拉菜单容器
    var dropdownContainer = document.createElement('div');
    dropdownContainer.style.cssText = 'position: relative; display: inline-block;';

    var dropdownButton = document.createElement('button');
    dropdownButton.textContent = '更多功能';
    dropdownButton.style.cssText = 'margin-left: 20px; padding: 5px 10px; cursor: pointer; border-radius: 999px;';
    dropdownButton.classList.add('ant-btn', 'css-9fw9up', 'ant-btn-default', 'primaryButton___N3z1x');
    dropdownContainer.appendChild(dropdownButton);

    // 创建下拉菜单内容
    var dropdownContent = document.createElement('div');
    dropdownContent.style.cssText = `
        display: none;
        position: absolute;
        background-color: #f9f9f9;
        min-width: 200px;
        box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
        z-index: 1;
        border-radius: 10px;
        max-height: 355px;
        overflow-y: auto; /* 使用 auto 可以根据内容是否溢出显示滚动条 */
        scrollbar-width: none; /* 隐藏滚动条,适用于 Firefox */
        -ms-overflow-style: none; /* 隐藏滚动条,适用于 IE 和 Edge */
    `;
    // 创建快捷粘贴搜索开关元素
    var toggleSwitchContainer = document.createElement('div');
    toggleSwitchContainer.classList.add('flex', 'items-center', 'dropdown-item');
    toggleSwitchContainer.style.cssText = 'padding: 12px 16px; cursor: pointer; display: flex; justify-content: space-between;';

    var toggleSwitchText = document.createElement('span');
    toggleSwitchText.textContent = '快捷粘贴搜索';
    toggleSwitchText.classList.add('lh-22');
    toggleSwitchContainer.appendChild(toggleSwitchText);

    var toggleSwitch = document.createElement('button');
    toggleSwitch.innerHTML = '<div class="ant-switch-handle"></div><span class="ant-switch-inner"><span class="ant-switch-inner-checked"></span><span class="ant-switch-inner-unchecked"></span></span>';
    toggleSwitch.setAttribute('type', 'button');
    toggleSwitch.setAttribute('role', 'switch');
    toggleSwitch.setAttribute('aria-checked', toggleSwitchState); // 设置开关状态
    toggleSwitch.classList.add('ant-switch', 'css-9fw9up');
    if (toggleSwitchState) {
        toggleSwitch.classList.add('ant-switch-checked');
    }
    toggleSwitchContainer.appendChild(toggleSwitch);

    // 创建手卡快捷截图开关元素
    var tableCardPngSwitchContainer = document.createElement('div');
    tableCardPngSwitchContainer.classList.add('flex', 'items-center', 'dropdown-item');
    tableCardPngSwitchContainer.style.cssText = 'padding: 12px 16px; cursor: pointer; display: flex; justify-content: space-between;';

    var tableCardPngSwitchText = document.createElement('span');
    tableCardPngSwitchText.textContent = '手卡快捷截图';
    tableCardPngSwitchText.classList.add('lh-22');
    tableCardPngSwitchContainer.appendChild(tableCardPngSwitchText);

    var tableCardPngSwitch = document.createElement('button');
    tableCardPngSwitch.innerHTML = '<div class="ant-switch-handle"></div><span class="ant-switch-inner"><span class="ant-switch-inner-checked"></span><span class="ant-switch-inner-unchecked"></span></span>';
    tableCardPngSwitch.setAttribute('type', 'button');
    tableCardPngSwitch.setAttribute('role', 'switch');
    tableCardPngSwitch.setAttribute('aria-checked', tableCardPngSwitchState); // 设置开关状态
    tableCardPngSwitch.classList.add('ant-switch', 'css-9fw9up');
    if (tableCardPngSwitchState) {
        tableCardPngSwitch.classList.add('ant-switch-checked');
    }
    tableCardPngSwitchContainer.appendChild(tableCardPngSwitch);

    // 创建提取商品链接按钮容器
    var onlyItemIdButtonContainer = document.createElement('div');
    var mainOnlyItemIdSwitch;// 声明开关元素
    // 声明自动点击开关元素与开关状态生成
    var sonSwitch_onlyItemId_autoClick;
    var sonSwitch_onlyItemId_autoClick_State = localStorage.getItem('sonSwitch_onlyItemId_autoClick_State') === 'true';
    // 声明检查开关元素与开关状态生成
    var sonSwitch_onlyItemId_checkId;
    var sonSwitch_onlyItemId_checkId_State = localStorage.getItem('sonSwitch_onlyItemId_checkId_State') === 'true';

    onlyItemIdButtonContainer.classList.add('flex', 'items-center', 'dropdown-item');
    onlyItemIdButtonContainer.style.cssText = 'padding: 12px 16px; cursor: pointer; display: flex; justify-content: space-between; align-items: center;';

    // 创建提取商品链接按钮文本
    var onlyItemIdButtonText = document.createElement('span');
    onlyItemIdButtonText.textContent = '链接自动提取';
    onlyItemIdButtonText.classList.add('lh-22');
    onlyItemIdButtonContainer.appendChild(onlyItemIdButtonText);

    // 创建提取商品链接按钮
    var onlyItemIdButton = document.createElement('button');
    onlyItemIdButton.style.cssText = 'background: none; border: none; font-size: 16px; cursor: pointer; transition: transform 0.3s;margin: 0px 6px';
    onlyItemIdButtonContainer.appendChild(onlyItemIdButton);

    // 创建 SVG 图标
    var svgIcon = document.createElementNS("http://www.w3.org/2000/svg", "svg");
    svgIcon.setAttribute('class', 'icon');
    svgIcon.setAttribute('viewBox', '0 0 1024 1024');
    svgIcon.setAttribute('width', '20');
    svgIcon.setAttribute('height', '20');
    svgIcon.setAttribute('fill', '#bbbbbb');
    svgIcon.innerHTML = '<path d="M288.791335 65.582671l446.41733 446.417329-446.41733 446.417329z"></path>';
    svgIcon.style.cssText = 'vertical-align: middle;'; // 垂直居中样式
    onlyItemIdButton.appendChild(svgIcon);

    dropdownContent.appendChild(onlyItemIdButtonContainer);

    var versionTitle = document.createElement('p');
    versionTitle.textContent = 'Version 0.5';
    versionTitle.style.cssText = `
    font-size: 12px;
    line-height: 1.8; /* 调整行高 */
    margin: 0 20px;
    justify-content: center; /* 使用 flex 居中对齐 */
    align-items: center;
    border-top: 1px solid #D2D2D2;
    text-align: center; /* 居中文本 */
    color: #9B9B9B;
    display: flex; /* 添加 display: flex 以使用 flexbox 布局 */
`;
    // 将开关和按钮加入页面
    dropdownContent.appendChild(toggleSwitchContainer);
    dropdownContent.appendChild(onlyItemIdButtonContainer);
    dropdownContent.appendChild(tableCardPngSwitchContainer);
    dropdownContent.appendChild(versionTitle);
    dropdownContainer.appendChild(dropdownContent);
    switchesContainer.appendChild(dropdownContainer);

    // 下拉按钮点击事件
    dropdownButton.addEventListener('click', function () {
        dropdownContent.style.display = dropdownContent.style.display === 'none' ? 'block' : 'none';
    });

    // 点击外部区域隐藏下拉菜单
    window.addEventListener('click', function (event) {
        if (!dropdownContainer.contains(event.target)) {
            dropdownContent.style.display = 'none';
        }
    });

    // 创建通知弹窗
    var NotificationContainer = document.createElement('div');
    NotificationContainer.style.cssText = 'position: fixed; bottom: 60px; left: 50%; transform: translateX(-50%); background-color: rgba(0, 0, 0, 0.5); color: #fff; padding: 10px; border-radius: 10px; display: none; z-index: 9999;';
    document.body.appendChild(NotificationContainer);

    // 获取开关元素
    var copySwitchHandle = copySwitch.querySelector('.ant-switch-handle');
    var toggleSwitchHandle = toggleSwitch.querySelector('.ant-switch-handle');
    var tableCardPngSwitchHandle = tableCardPngSwitch.querySelector('.ant-switch-handle');

    // 更新本地存储的开关状态
    var updateSwitchState = function (switchName, newState) {
        localStorage.setItem(switchName, newState.toString());
    };

    // 监听开关点击事件
    copySwitch.addEventListener('click', function () {
        var newState = copySwitch.getAttribute('aria-checked') === 'true' ? false : true;
        copySwitch.setAttribute('aria-checked', newState);
        if (newState) {
            copySwitch.classList.add('ant-switch-checked');
        } else {
            copySwitch.classList.remove('ant-switch-checked');
        }
        updateSwitchState('copySwitchState', newState);
    });

    toggleSwitch.addEventListener('click', function () {
        var newState = toggleSwitch.getAttribute('aria-checked') === 'true' ? false : true;
        toggleSwitch.setAttribute('aria-checked', newState);
        if (newState) {
            toggleSwitch.classList.add('ant-switch-checked');
        } else {
            toggleSwitch.classList.remove('ant-switch-checked');
        }
        updateSwitchState('toggleSwitchState', newState);
    });

    // 手卡快捷截图开关点击事件
    tableCardPngSwitch.addEventListener('click', function () {
        var newState = tableCardPngSwitch.getAttribute('aria-checked') === 'true' ? false : true;
        tableCardPngSwitch.setAttribute('aria-checked', newState);
        if (newState) {
            tableCardPngSwitch.classList.add('ant-switch-checked');
        } else {
            tableCardPngSwitch.classList.remove('ant-switch-checked');
        }
        updateSwitchState('tableCardPngSwitchState', newState);
    });

    // 提取商品链接按钮点击事件
    // 提取商品链接设置页面逻辑区

    // 创建提取商品链接二级页面内容
    secondaryContent = document.createElement('div');
    secondaryContent.id = 'secondary-content';
    secondaryContent.style.cssText = 'margin: 0px 10px; display:none; padding: 10px 12px; background: #E9E9E9; border: 1px solid #D2D2D2; border-radius: 10px;';

    // 创建设置页面
    var contentContainer = document.createElement('div');
    contentContainer.style.cssText = 'display: flex; justify-content: space-between; align-items: center;';

    // 创建标题
    var title = document.createElement('p');
    title.textContent = '提取商品链接';
    title.style.cssText = 'font-size: 14px; margin: 0;';
    contentContainer.appendChild(title);

    // 创建开关
    mainOnlyItemIdSwitch = document.createElement('button');
    mainOnlyItemIdSwitch.innerHTML = '<div class="ant-switch-handle"></div><span class="ant-switch-inner"><span class="ant-switch-inner-checked"></span><span class="ant-switch-inner-unchecked"></span></span>';
    mainOnlyItemIdSwitch.setAttribute('type', 'button');
    mainOnlyItemIdSwitch.setAttribute('role', 'switch');
    mainOnlyItemIdSwitch.setAttribute('aria-checked', mainOnlyItemIdSwitchState); // 设置开关状态
    mainOnlyItemIdSwitch.classList.add('ant-switch', 'css-9fw9up', 'ant-switch-small');
    if (mainOnlyItemIdSwitchState) {
        mainOnlyItemIdSwitch.classList.add('ant-switch-checked');
    }

    // 主功能说明容器
    var mainIllustrate = document.createElement('div');
    mainIllustrate.style.cssText = 'font-size: 12px; color: #9B9B9B; margin: 5px 0px; padding-bottom: 5px; display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid #D2D2D2;';
    mainIllustrate.textContent = '当点击商品链接窗口时,会主动提取当前剪切板中的商品链接,并更新你的剪切板';

    contentContainer.appendChild(mainOnlyItemIdSwitch);
    secondaryContent.appendChild(contentContainer);
    secondaryContent.appendChild(mainIllustrate);

    // 子选项设置页面
    var sonContentContainer = document.createElement('div');
    sonContentContainer.style.cssText = 'display: block; justify-content: space-between; align-items: center;';
    secondaryContent.appendChild(sonContentContainer);

    // 自动保存开关页面
    var autoClickSwitch = document.createElement('div');
    autoClickSwitch.style.cssText = 'display: flex; justify-content: space-between; align-items: center;';

    // 自动保存开关标题
    var autoClickTitle = document.createElement('p');
    autoClickTitle.textContent = '自动保存';
    autoClickTitle.style.cssText = 'font-size: 14px; margin: 0;';
    autoClickSwitch.appendChild(autoClickTitle);

    // 创建自动保存开关元素
    sonSwitch_onlyItemId_autoClick = document.createElement('button');
    sonSwitch_onlyItemId_autoClick.innerHTML = '<div class="ant-switch-handle"></div><span class="ant-switch-inner"><span class="ant-switch-inner-checked"></span><span class="ant-switch-inner-unchecked"></span></span>';
    sonSwitch_onlyItemId_autoClick.setAttribute('type', 'button');
    sonSwitch_onlyItemId_autoClick.setAttribute('role', 'switch');
    sonSwitch_onlyItemId_autoClick.setAttribute('aria-checked', sonSwitch_onlyItemId_autoClick_State); // 设置开关状态
    sonSwitch_onlyItemId_autoClick.classList.add('ant-switch', 'css-9fw9up', 'ant-switch-small');
    if (sonSwitch_onlyItemId_autoClick_State) {
        sonSwitch_onlyItemId_autoClick.classList.add('ant-switch-checked');
    }

    // 自动保存功能说明容器
    var autoClickIllustrate = document.createElement('div');
    autoClickIllustrate.style.cssText = 'font-size: 12px; color: #9B9B9B; margin: 5px 0px; padding-bottom: 5px; display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid #D2D2D2;';
    autoClickIllustrate.textContent = '替换链接后自动点击“保存”按钮';

    // 链接检查开关页面
    var checkIdSwitch = document.createElement('div');
    checkIdSwitch.style.cssText = 'display: flex; justify-content: space-between; align-items: center;';

    // 链接检查开关标题
    var checkIdTitle = document.createElement('p');
    checkIdTitle.textContent = '链接检查';
    checkIdTitle.style.cssText = 'font-size: 14px; margin: 0;';
    checkIdSwitch.appendChild(checkIdTitle);

    // 创建链接检查开关元素
    sonSwitch_onlyItemId_checkId = document.createElement('button');
    sonSwitch_onlyItemId_checkId.innerHTML = '<div class="ant-switch-handle"></div><span class="ant-switch-inner"><span class="ant-switch-inner-checked"></span><span class="ant-switch-inner-unchecked"></span></span>';
    sonSwitch_onlyItemId_checkId.setAttribute('type', 'button');
    sonSwitch_onlyItemId_checkId.setAttribute('role', 'switch');
    sonSwitch_onlyItemId_checkId.setAttribute('aria-checked', sonSwitch_onlyItemId_checkId_State); // 设置开关状态
    sonSwitch_onlyItemId_checkId.classList.add('ant-switch', 'css-9fw9up', 'ant-switch-small');
    if (sonSwitch_onlyItemId_checkId_State) {
        sonSwitch_onlyItemId_checkId.classList.add('ant-switch-checked');
    }
    else {
        sonContentContainer.style.opacity = '0.4';  // 设置透明度使内容变浅
        sonContentContainer.style.pointerEvents = 'none';  // 禁止点击操作
    }

    // 链接检查功能说明容器
    var checkIdIllustrate = document.createElement('div');
    checkIdIllustrate.style.cssText = 'font-size: 12px; color: #9B9B9B; margin: 5px 0px; padding-bottom: 5px; display: flex; justify-content: space-between; align-items: center; border-bottom: 0px solid #D2D2D2; white-space: pre-line;';
    checkIdIllustrate.textContent = '链接中不存在12位ID时,使用“警告”替换当前剪切板\n开启此功能会引起剪切板冲突';

    // 自动保存加入autoClickSwitch页面
    autoClickSwitch.appendChild(sonSwitch_onlyItemId_autoClick);
    // 链接检查加入checkIdSwitch页面
    checkIdSwitch.appendChild(sonSwitch_onlyItemId_checkId);

    sonContentContainer.appendChild(autoClickSwitch);
    sonContentContainer.appendChild(autoClickIllustrate);
    sonContentContainer.appendChild(checkIdSwitch);
    sonContentContainer.appendChild(checkIdIllustrate);

    // 将二级页面内容插入到 onlyItemIdButtonContainer 的下方
    onlyItemIdButtonContainer.parentNode.insertBefore(secondaryContent, onlyItemIdButtonContainer.nextSibling);

    // 监听onlyItemId主功能开关状态
    mainOnlyItemIdSwitch.addEventListener('click', function () {
        var newState = mainOnlyItemIdSwitch.getAttribute('aria-checked') === 'true' ? false : true;
        mainOnlyItemIdSwitch.setAttribute('aria-checked', newState);
        if (newState) {
            mainOnlyItemIdSwitch.classList.add('ant-switch-checked');
        } else {
            mainOnlyItemIdSwitch.classList.remove('ant-switch-checked');
        }
        updateSwitchState('mainOnlyItemIdSwitchState', newState);

        // 获取sonContentContainer容器内的所有输入元素
        var inputs = sonContentContainer.querySelectorAll('input, select, textarea, button');
        inputs.forEach(function (input) {
            input.disabled = !newState;
        });

        //sonContentContainer容器变灰色
        if (!newState) {
            sonContentContainer.style.opacity = '0.4';  // 设置透明度使内容变浅
            sonContentContainer.style.pointerEvents = 'none';  // 禁止点击操作
        } else {
            sonContentContainer.style.opacity = '';  // 恢复透明度
            sonContentContainer.style.pointerEvents = '';  // 恢复点击操作
        }
    });

    // 监听onlyItemId自动保存开关状态
    sonSwitch_onlyItemId_autoClick.addEventListener('click', function () {
        var newState = sonSwitch_onlyItemId_autoClick.getAttribute('aria-checked') === 'true' ? false : true;
        sonSwitch_onlyItemId_autoClick.setAttribute('aria-checked', newState);
        if (newState) {
            sonSwitch_onlyItemId_autoClick.classList.add('ant-switch-checked');
        } else {
            sonSwitch_onlyItemId_autoClick.classList.remove('ant-switch-checked');
        }
        updateSwitchState('sonSwitch_onlyItemId_autoClick_State', newState);
    });

    // 监听onlyItemId链接检查开关状态
    sonSwitch_onlyItemId_checkId.addEventListener('click', function () {
        var newState = sonSwitch_onlyItemId_checkId.getAttribute('aria-checked') === 'true' ? false : true;
        sonSwitch_onlyItemId_checkId.setAttribute('aria-checked', newState);
        if (newState) {
            sonSwitch_onlyItemId_checkId.classList.add('ant-switch-checked');
        } else {
            sonSwitch_onlyItemId_checkId.classList.remove('ant-switch-checked');
        }
        updateSwitchState('sonSwitch_onlyItemId_checkId_State', newState);
    });


    onlyItemIdButton.addEventListener('click', function () {
        // 切换二级页面显示状态
        var secondaryContent = document.getElementById('secondary-content');
        if (secondaryContent) {
            secondaryContent.style.display = secondaryContent.style.display === 'block' ? 'none' : 'block';
        } else {
            console.log("onlyItemId二级页面异常")
        }

        // 旋转按钮图标
        var icon = onlyItemIdButton.querySelector('svg');
        var rotation = icon.style.transform === 'rotate(90deg)' ? 'rotate(0deg)' : 'rotate(90deg)';
        icon.style.transform = rotation;
    });

    // 鼠标悬浮事件
    onlyItemIdButton.addEventListener('mouseenter', function () {
        onlyItemIdButton.querySelector('svg').setAttribute('fill', '#ff6200');
    });

    // 鼠标移出事件
    onlyItemIdButton.addEventListener('mouseleave', function () {
        onlyItemIdButton.querySelector('svg').setAttribute('fill', '#bbbbbb');
    });

    // 单击和双击事件的延迟和计数器
    var delay = 200; // 延迟时间,单位毫秒
    var clicks = 0;
    var timer = null;

    // 监听鼠标左键点击事件
    document.addEventListener('click', function (event) {
        // 检查是否开启了点击复制功能且在匹配的网址上
        if (copySwitch.getAttribute('aria-checked') === 'true' && isHomeURL()) {
            clicks++;
            if (clicks % 2 === 1) {
                timer = setTimeout(function () {
                    // 单击操作
                    clicks = 0;
                    handleSingleClick(event);
                }, delay);
            } else {
                // 取消之前的单击操作
                clearTimeout(timer);
                clicks = 0;
                handleDoubleClick(event);
            }
        }
    });

    // 单击处理函数
    function handleSingleClick(event) {
        if (event.target && event.target.classList.contains('link-overflow-tip')) {
            var text = event.target.textContent;
            var copiedText = text.substring();
            GM_setClipboard(copiedText);
            showNotification("复制成功:" + copiedText);
        }
    }

    // 双击处理函数
    function handleDoubleClick(event) {
        if (event.target && event.target.classList.contains('link-overflow-tip')) {
            var text = event.target.textContent;
            var copiedText = text.substring(0, 3);
            GM_setClipboard(copiedText);
            showNotification("复制成功:" + copiedText);
        }
    }

    // 显示通知
    function showNotification(message, showImage = false) {
        NotificationContainer.textContent = message;
        NotificationContainer.style.display = 'inline-block'; // 设置为 inline-block
        NotificationContainer.style.textAlign = 'center'; // 让文本居中显示
        NotificationContainer.style.width = 'auto'; // 设置宽度自动调整
        NotificationContainer.style.padding = '10px'; // 设置内边距

        // 清除之前的计时器
        if (notificationTimer) {
            clearTimeout(notificationTimer);
        }

        // 1.5 秒后隐藏通知
        notificationTimer = setTimeout(function () {
            NotificationContainer.style.display = 'none';
        }, 1500);

        // 在显示图片之前将容器宽度设置为合适的值
        NotificationContainer.style.width = showImage ? '300px' : 'auto';

        // 如果指定了显示图片,则读取剪贴板中的图片并显示
        if (showImage) {
            NotificationContainer.style.padding = '5px';
            navigator.clipboard.read().then(async function (data) {
                for (const item of data) {
                    for (const type of item.types) {
                        if (type.startsWith('image/')) {
                            const blob = await item.getType(type);
                            const imageURL = URL.createObjectURL(blob);

                            const imageElement = document.createElement('img');
                            imageElement.src = imageURL;
                            imageElement.style.width = '300px'; // 控制图片宽度为其实际大小的40%
                            imageElement.style.borderRadius = '6px'; // 添加圆角样式

                            const imageContainer = document.createElement('div');
                            imageContainer.style.paddingTop = '10px';
                            imageElement.style.maxWidth = '290px';
                            imageContainer.style.borderRadius = '6px'; // 添加圆角样式
                            imageContainer.style.margin = 'auto'; // 居中
                            imageContainer.style.display = 'block'; // 设置为 block,使其单独占据一行
                            // imageContainer.style.overflow = 'hidden'; // 裁剪多余部分
                            imageContainer.appendChild(imageElement);

                            NotificationContainer.appendChild(imageContainer);

                            break;
                        }
                    }
                }
            }).catch(function (error) {
                console.error('Error reading clipboard:', error);
            });
        }
    }


    // 添加鼠标移入事件监听器
    NotificationContainer.addEventListener('mouseenter', function () {
        // 清除计时器
        if (notificationTimer) {
            clearTimeout(notificationTimer);
        }
    });

    // 添加鼠标移出事件监听器
    NotificationContainer.addEventListener('mouseleave', function () {
        // 刷新计时器
        notificationTimer = setTimeout(function () {
            NotificationContainer.style.display = 'none';
        }, 1500);
    });

    async function clickButton(pastedData = true, delayTime, containerSelector = document, buttonSelector) {
        // 判断粘贴内容是否为空或为 true
        if (pastedData === true || (typeof pastedData === 'string' && pastedData.trim().length > 0)) {
            // 查找指定容器内的按钮
            var container = containerSelector === document ? document : document.querySelector(containerSelector);
            if (container) {
                var button = container.querySelector(buttonSelector);
                if (button) {
                    setTimeout(function () {
                        button.click();
                    }, delayTime);
                }
            }
        }
    }

    // 监听粘贴事件
    document.addEventListener('paste', function (event) {
        // 判断是否开启了自动点击功能且在匹配的网址上
        if (toggleSwitch.getAttribute('aria-checked') === 'true' && isHomeURL()) {
            // 获取粘贴板中的内容
            var pastedData = (event.clipboardData || window.clipboardData).getData('text');
            var className = '.ant-btn.css-9fw9up.ant-btn-primary';
            clickButton(pastedData, 100, undefined, className);
        }
    });

    // 检查文本中是否是天猫链接函数
    function checkForTmallInClipboard(text) {
        const regex = /https:\/\/[^ ]*tmall[^ ]*id=\d{12}/;
        return regex.test(text);
    }

    function checkForChaoshiInClipboard(text) {
        const regex = /https:\/\/[^ ]*chaoshi[^ ]*id=\d{12}/;
        return regex.test(text);
    }

    function checkForFeizhuInClipboard(text) {
        const regex = /https:\/\/[^ ]*fliggy[^ ]*id=\d{12}/;
        return regex.test(text);
    }

    // 提取链接id函数
    function extractIdFromClipboard(text) {
        const idMatch = text.match(/id=(\d{12})/);
        return idMatch ? idMatch[1] : null;
    }

    // 粘贴功能
    async function simulatePaste(targetElement, clearBeforePaste = true) {
        try {
            // 从剪贴板读取文本
            let clipboardText = await navigator.clipboard.readText();

            // 检查目标元素是否为可编辑元素
            if (targetElement.isContentEditable || targetElement.tagName === 'INPUT' || targetElement.tagName === 'TEXTAREA') {
                // 如果clearBeforePaste为true,清空目标元素的内容
                if (clearBeforePaste) {
                    if (targetElement.isContentEditable) {
                        targetElement.innerHTML = '';
                    } else {
                        targetElement.value = '';
                    }
                }

                // 插入剪贴板内容到目标元素
                if (document.execCommand('insertText', false, clipboardText)) {
                    // console.log('粘贴成功:' + clipboardText);
                } else {
                    targetElement.value += clipboardText;
                }
            } else {
                alert('目标元素不可编辑');
            }
        } catch (err) {
            console.error('读取剪贴板内容失败:', err);
            showNotification("读取剪贴板内容失败");
        }
    }

    function updateClipboardContent() {
        var tmail = "https://detail.tmall.com/item.htm?id=";
        var chaoshi = "https://chaoshi.detail.tmall.com/item.htm?id=";
        var taobao = "https://item.taobao.com/item.htm?id=";
        var feizhu = "https://traveldetail.fliggy.com/item.htm?id=";
        navigator.clipboard.readText().then((clipText) => {
            const isTmall = checkForTmallInClipboard(clipText);
            const isChaoshi = checkForChaoshiInClipboard(clipText);
            const isFeizhu = checkForFeizhuInClipboard(clipText);
            const itemId = extractIdFromClipboard(clipText);

            if (itemId) {
                var newUrl;
                if (isTmall && !isChaoshi) {
                    // 转换为天猫链接
                    newUrl = tmail + itemId;
                }
                else if (isChaoshi) {
                    // 转换为猫超链接
                    newUrl = chaoshi + itemId;
                }
                else if (isFeizhu) {
                    // 转换为飞猪链接
                    newUrl = tmail + itemId;
                }
                else {
                    // 转换为淘宝链接
                    newUrl = taobao + itemId;
                }
                GM_setClipboard(newUrl);
                showNotification("剪贴板内容已更新为:" + newUrl);
                //console.log('剪贴板内容已更新为:' + newUrl);
            } else {
                if (sonSwitch_onlyItemId_checkId.getAttribute('aria-checked') === 'true') {
                    // 防止错误粘贴功能
                    GM_setClipboard("12位数字ID不全");
                }
                showNotification("剪贴板中没有找到12位数字ID");
                //console.log('剪贴板中没有找到12位数字ID');
            }
        }).catch((err) => {
            console.error('读取剪贴板失败:', err);
        });
    }

    // 监听鼠标左键点击事件
    document.addEventListener('click', function (event) {
        // 提取商品链接粘贴功能区域
        if (mainOnlyItemIdSwitch.getAttribute('aria-checked') === 'true' && isHomeURL()) {
            if (event.target.closest('.ant-form-item.liveLinkFormItem___RPAQZ.css-9fw9up.ant-form-item-has-success')) {
                if (event.target.classList.contains('ant-input-affix-wrapper') ||
                    event.target.classList.contains('ant-input-affix-wrapper-status-error') ||
                    event.target.classList.contains('css-9fw9up')) {
                    // console.log('目标元素被点击');
                    updateClipboardContent();
                    simulatePaste(document.activeElement);

                    // 点击保存按钮
                    if (sonSwitch_onlyItemId_autoClick.getAttribute('aria-checked') === 'true') {
                        var fbutton = '.ant-drawer-footer';
                        var buttonName = '.ant-btn.css-9fw9up.ant-btn-primary';
                        showNotification("粘贴成功&保存成功");
                        clickButton(undefined, 300, fbutton, buttonName);
                    }
                }
            }
        }
        // 自动序号标1功能
        if (false) {
            if (event.target.closest('.ant-input-number.ant-input-number-sm.css-1ayq15k')) {
                if (event.target.classList.contains('ant-input-number-input')) {
                    console.log('目标元素被点击');
                    GM_setClipboard("1");
                    simulatePaste(document.activeElement);
                }
            }
        }
    });

    /* 快捷截图功能区 */
    if (tableCardPngSwitch.getAttribute('aria-checked') === 'true' && isTableCardURL()) {
        // Load html2canvas library
        function loadHtml2Canvas(callback) {
            var script = document.createElement('script');
            script.src = 'https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js';
            script.onload = callback;
            document.head.appendChild(script);
        }

        function createCaptureScreenshotButton() {
            const targetClass = '[class*="ant-space"][class*="css-9fw9up"][class*="ant-space-horizontal"][class*="ant-space-align-center"]';

            const observer = new MutationObserver((mutationsList, observer) => {
                for (let mutation of mutationsList) {
                    if (mutation.type === 'childList') {
                        const targetElement = document.querySelector(targetClass);
                        if (targetElement) {
                            if (document.querySelector('.captureScreenshot')) {
                                observer.disconnect();
                                return;
                            }

                            var captureScreenshot = document.createElement('div');
                            captureScreenshot.classList.add('ant-space-item');

                            var captureScreenshotButton = document.createElement('button');
                            captureScreenshotButton.textContent = '快捷截图';
                            captureScreenshotButton.classList.add('ant-btn', 'css-9fw9up', 'ant-btn-default', 'captureScreenshot');
                            captureScreenshot.appendChild(captureScreenshotButton);

                            targetElement.insertBefore(captureScreenshot, targetElement.firstChild);

                            captureScreenshotButton.addEventListener('click', captureScreenshotFunction);

                            observer.disconnect();
                            break;
                        }
                    }
                }
            });

            observer.observe(document.body, {
                childList: true,
                subtree: true
            });
        }

        function loadImageAsDataURL(url) {
            return new Promise((resolve, reject) => {
                GM_xmlhttpRequest({
                    method: 'GET',
                    url: url,
                    responseType: 'blob',
                    onload: function (response) {
                        const blob = response.response;
                        const reader = new FileReader();
                        reader.onloadend = function () {
                            resolve(reader.result);
                        };
                        reader.onerror = function () {
                            reject(new Error('Failed to load image'));
                        };
                        reader.readAsDataURL(blob);
                    },
                    onerror: function () {
                        reject(new Error('Network error'));
                    }
                });
            });
        }

        async function captureScreenshotFunction() {
            const tableElement = document.querySelector('table');
            var displayScale = 2.5;
            showNotification("截图中···");
            if (tableElement) {
                const rows = tableElement.querySelectorAll('tr');

                if (rows.length >= 3) {
                    rows[2].cells[0].textContent = '';

                    // 隐藏除第二行和第三行外的所有行
                    rows.forEach((row, index) => {
                        if (index !== 1 && index !== 2) {
                            row.style.display = 'none';
                        }
                    });

                    const imgElement = rows[2].cells[2].querySelector('img');
                    if (imgElement) {
                        try {
                            const dataUrl = await loadImageAsDataURL(imgElement.src);
                            imgElement.src = dataUrl;

                            setTimeout(() => {
                                // 使用 html2canvas 捕获截图
                                html2canvas(tableElement, { scale: displayScale }).then(canvas => {
                                    // 恢复所有行的显示状态
                                    rows.forEach(row => {
                                        row.style.display = '';
                                    });

                                    canvas.toBlob(async function (blob) {
                                        try {
                                            await navigator.clipboard.write([new ClipboardItem({ "image/png": blob })]);
                                            console.log("%cTable Screenshot:", "color: #9147ff", "Screenshot copied to clipboard.");
                                            showNotification("截图已成功复制到剪贴板", true);
                                        } catch (error) {
                                            console.log("%cTable Screenshot: Screenshot failed to copy to clipboard!", "color: #ff8080");
                                            showNotification("截图失败!");
                                        }
                                    });
                                });
                            }, 200); // 延迟 200 毫秒等待图片加载完毕
                        } catch (error) {
                            console.log('Image load error:', error);
                        }
                    } else {
                        console.log('Image element not found');
                    }
                } else {
                    console.log("Table does not have enough rows");
                }
            } else {
                console.log("Table element not found");
            }
        }

        loadHtml2Canvas(createCaptureScreenshotButton);
    }
})();