Greasy Fork

Greasy Fork is available in English.

link工具

点击复制,提取链接

当前为 2024-06-15 提交的版本,查看 最新版本

// ==UserScript==
// @name         link工具
// @namespace    http://tampermonkey.net/
// @version      0.4.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 copySwitchState = localStorage.getItem('copySwitchState') === 'true';
    var toggleSwitchState = localStorage.getItem('toggleSwitchState') === 'true';
    var onlyItemIdSwitchState = localStorage.getItem('onlyItemIdSwitchState') === '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; border: 1px solid #ccc; background: #fff; cursor: pointer;border-radius: 10px;';
    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;';

    // 创建快捷粘贴搜索开关元素
    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 onlyItemIdSwitchContainer = document.createElement('div');
    onlyItemIdSwitchContainer.classList.add('flex', 'items-center', 'dropdown-item');
    onlyItemIdSwitchContainer.style.cssText = 'padding: 12px 16px; cursor: pointer; display: flex; justify-content: space-between;';

    var onlyItemIdSwitchText = document.createElement('span');
    onlyItemIdSwitchText.textContent = '提取商品链接';
    onlyItemIdSwitchText.classList.add('lh-22');
    onlyItemIdSwitchContainer.appendChild(onlyItemIdSwitchText);

    var onlyItemIdSwitch = document.createElement('button');
    onlyItemIdSwitch.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>';
    onlyItemIdSwitch.setAttribute('type', 'button');
    onlyItemIdSwitch.setAttribute('role', 'switch');
    onlyItemIdSwitch.setAttribute('aria-checked', onlyItemIdSwitchState); // 设置开关状态
    onlyItemIdSwitch.classList.add('ant-switch', 'css-9fw9up');
    if (onlyItemIdSwitchState) {
        onlyItemIdSwitch.classList.add('ant-switch-checked');
    }
    onlyItemIdSwitchContainer.appendChild(onlyItemIdSwitch);

    dropdownContent.appendChild(toggleSwitchContainer);
    dropdownContent.appendChild(onlyItemIdSwitchContainer);
    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 onlyItemIdSwitchHandle = onlyItemIdSwitch.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);
    });

    // 提取商品链接开关点击事件
    onlyItemIdSwitch.addEventListener('click', function() {
        var newState = onlyItemIdSwitch.getAttribute('aria-checked') === 'true' ? false : true;
        onlyItemIdSwitch.setAttribute('aria-checked', newState);
        if (newState) {
            onlyItemIdSwitch.classList.add('ant-switch-checked');
        } else {
            onlyItemIdSwitch.classList.remove('ant-switch-checked');
        }
        updateSwitchState('onlyItemIdSwitchState', newState);
    });

    // 单击和双击事件的延迟和计数器
    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) {
        NotificationContainer.textContent = message;
        NotificationContainer.style.display = 'block';

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

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

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

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

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

            // 判断粘贴内容是否为空
            if (pastedData.trim().length > 0) {
                // 查找按钮并点击
                var button = document.querySelector('.ant-btn.css-9fw9up.ant-btn-primary');
                if (button) {
                    setTimeout(function() {
                        button.click();
                    }, 100);
                }
            }
        }
    });

    //检查文本中是否是天猫链接函数
    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;
    }



    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 = feizhu + itemId;
                }
                else{
                    // 转换为淘宝链接
                    newUrl = taobao + itemId;
                }
                GM_setClipboard(newUrl);
                showNotification("剪贴板内容已更新为:"+newUrl);
                //console.log('剪贴板内容已更新为:'+newUrl);
            } else {
                if(true) GM_setClipboard("12位数字ID不全");// 防止错误粘贴功能
                showNotification("剪贴板中没有找到12位数字ID");
                //console.log('剪贴板中没有找到12位数字ID');
            }
        }).catch((err) => {
            console.error('读取剪贴板失败:', err);
        });
    }

    // 监听鼠标左键点击事件
    document.addEventListener('click', function(event) {
        if (onlyItemIdSwitch.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();
                }
            }
        }
    });

})();