没有朋友

移除页面aff参数,增加一键复制优惠券按钮

// ==UserScript==
// @name         没有朋友
// @namespace    https://linux.do/u/amna/
// @version      1.1
// @description  移除页面aff参数,增加一键复制优惠券按钮
// @license      MIT
// @locale       zh-CN
// @author       https://linux.do/u/amna/
// @match        *://ygpy.net/*
// @run-at       document-end
// @grant        GM_setClipboard
// @grant        GM_addStyle
// ==/UserScript==

(function() {
    'use strict';

    // --- 功能 1: 移除链接参数 ---
    function cleanLinks() {
        const allLinks = document.querySelectorAll('a');
        allLinks.forEach(link => {
            if (link.href.includes('?')) {
                link.href = link.href.split('?')[0];
            }
        });
    }

    // --- 功能 2: 添加代码复制按钮 (优化版) ---
    function addCopyButtons() {
        // 定义一个需要忽略的文本内容的集合 (Set),查找效率更高
        const ignoreSet = new Set([
            "流媒体解锁",
            "多设备在线",
            "支持 BT 下载",
            "国内中转线路",
            "IEPL 专线路",
            "设备不限制",
            "晚高峰稳定",
            "国际专线",
            "IPLC 线路",
            "50+ 节点",
            "不限设备",
            "超大带宽",
            "赠送 Emby",
            "IEPL 专线",
            "家宽 IP",
            "厦门专线",
            "开始阅读"
        ]);

        const codeBlocks = document.querySelectorAll('code');

        // 使用 GM_addStyle 添加按钮样式 (只需添加一次)
        if (!document.getElementById('gm-copy-button-style')) {
             GM_addStyle(`
                #gm-copy-button-style { display: none; } /* 占位符 */
                .copy-button-for-code {
                    margin-left: 8px;
                    padding: 3px 6px;
                    border: 1px solid #ccc;
                    border-radius: 4px;
                    background-color: #f0f0f0;
                    color: #333;
                    cursor: pointer;
                    font-size: 12px;
                    font-family: sans-serif;
                    user-select: none; /* 防止选中文本 */
                    display: inline-block; /* 确保按钮正确显示 */
                    vertical-align: middle; /* 垂直居中对齐 */
                }
                .copy-button-for-code:hover {
                    background-color: #e0e0e0;
                    border-color: #bbb;
                }
                .copy-button-for-code:active {
                    background-color: #d0d0d0;
                }
            `);
             const styleTag = document.createElement('style');
             styleTag.id = 'gm-copy-button-style';
             document.head.appendChild(styleTag);
        }


        codeBlocks.forEach(code => {
            const textContent = code.textContent.trim(); // 获取并清理文本前后空格

            // 【优化点】如果文本内容在忽略列表中,或者文本为空,则跳过
            if (!textContent || ignoreSet.has(textContent)) {
                return;
            }

            // 防止重复添加按钮
            if (code.nextElementSibling && code.nextElementSibling.classList.contains('copy-button-for-code')) {
                return;
            }

            const copyButton = document.createElement('button');
            copyButton.textContent = '复制';
            copyButton.className = 'copy-button-for-code';

            copyButton.addEventListener('click', (e) => {
                e.stopPropagation(); // 防止点击事件冒泡
                GM_setClipboard(code.textContent, 'text'); // 使用油猴的API复制原始文本

                // 提供视觉反馈
                const originalText = copyButton.textContent;
                copyButton.textContent = '已复制!';
                setTimeout(() => {
                    copyButton.textContent = originalText;
                }, 2000); // 2秒后恢复
            });

            // 将按钮插入到 code 标签之后
            code.parentNode.insertBefore(copyButton, code.nextSibling);
        });
    }

    // --- 执行脚本 ---
    // 页面加载完成后执行一次
    window.addEventListener('load', () => {
        cleanLinks();
        addCopyButtons();
    });

    // 对于动态加载内容的页面 (如SPA),我们使用 MutationObserver 监视DOM变化
    const observer = new MutationObserver(mutations => {
        // 使用一个标志来避免在短时间内重复执行
        let processed = false;
        mutations.forEach(mutation => {
            if (mutation.addedNodes.length && !processed) {
                // 每次有新内容加载时,都重新执行功能
                cleanLinks();
                addCopyButtons();
                processed = true;
            }
        });
    });

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