跳到正文

剪贴板

剪贴板 eo8tocuf

原文
剪贴板信息

剪贴板信息

收录
更新
C++
// ==UserScript==
// @name         自定义白名单拦截器 (多功能悬浮球 + 内置说明书)
// @namespace    http://tampermonkey.net/
// @version      8.0
// @description  右下角悬浮展开菜单,支持一键添加当前网页、打开设置、查看说明。保留瞬间拦截、种子系统与批量导入。
// @author       YourName
// @match        *://*/*
// @run-at       document-start
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_registerMenuCommand
// @grant        GM_addStyle
// ==/UserScript==

(function() {
    'use strict';

    // 防误杀:iframe 子窗口直接放弃运行本脚本所有逻辑
    if (window.top !== window.self) return;

    // ==========================================
    // 1. 数据读取与基础函数
    // ==========================================
    let isEnabled = GM_getValue('isEnabled', true);
    let whitelist = GM_getValue('whitelist', []);

    // 清理 URL 的辅助函数 (提取纯净链接用于一键添加)
    function getCleanUrl(url) {
        return url.split('?')[0].split('#')[0].replace(/\/$/, "");
    }

    // ==========================================
    // 2. 核心拦截逻辑 (瞬间跳转)
    // ==========================================
    if (isEnabled && whitelist.length > 0) {
        const currentUrl = window.location.href;

        // 防误杀:Cloudflare 等安全验证页面直接放行
        if (!currentUrl.includes('/cdn-cgi/challenge-platform/') && !currentUrl.includes('__cf_chl_')) {
            let isAllowed = false;

            for (let allowedUrl of whitelist) {
                let baseUrl = allowedUrl.endsWith('/') ? allowedUrl.slice(0, -1) : allowedUrl;

                if (
                    currentUrl === baseUrl ||
                    currentUrl.startsWith(baseUrl + '/') ||
                    currentUrl.startsWith(baseUrl + '?') ||
                    currentUrl.startsWith(baseUrl + '#')
                ) {
                    isAllowed = true;
                    break;
                }
            }

            // 不允许访问,立即瞬间跳转!
            if (!isAllowed) {
                let targetUrl = whitelist[0];
                console.log(`[白名单拦截器] 拦截: ${currentUrl} -> 跳转至: ${targetUrl}`);
                window.location.replace(targetUrl);
                return; // 阻止后续代码执行
            }
        }
    }

    // ==========================================
    // 3. 注入网页右下角多功能悬浮菜单
    // ==========================================
    function injectFloatingMenu() {
        if (document.getElementById('cwm-float-container')) return;

        // 悬浮菜单样式
        const style = document.createElement('style');
        style.innerHTML = `
            #cwm-float-container {
                position: fixed; bottom: 30px; right: 30px;
                display: flex; flex-direction: column; gap: 12px;
                align-items: center; z-index: 2147483646; font-family: sans-serif;
            }
            .cwm-float-action {
                width: 40px; height: 40px; border-radius: 50%;
                background: #fff; color: #333;
                display: flex; justify-content: center; align-items: center;
                font-size: 18px; cursor: pointer;
                box-shadow: 0 4px 10px rgba(0,0,0,0.15);
                opacity: 0; transform: translateY(20px); pointer-events: none;
                transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
            }
            .cwm-float-action:hover { background: #e6f7ff; color: #1890ff; transform: scale(1.1) translateY(0); }

            #cwm-float-container:hover .cwm-float-action {
                opacity: 1; transform: translateY(0); pointer-events: auto;
            }

            .cwm-float-main {
                width: 50px; height: 50px; border-radius: 50%;
                background: rgba(24, 144, 255, 0.7); color: white;
                display: flex; justify-content: center; align-items: center;
                font-size: 24px; cursor: pointer;
                box-shadow: 0 4px 12px rgba(24, 144, 255, 0.3);
                backdrop-filter: blur(4px); transition: all 0.3s;
            }
            #cwm-float-container:hover .cwm-float-main {
                background: rgba(24, 144, 255, 1); box-shadow: 0 6px 16px rgba(24, 144, 255, 0.5);
            }

            /* 悬浮提示文字 (Tooltip) */
            .cwm-float-action[data-title]::after {
                content: attr(data-title);
                position: absolute; right: 55px;
                background: rgba(0,0,0,0.7); color: #fff;
                padding: 6px 10px; border-radius: 6px; font-size: 13px;
                white-space: nowrap; pointer-events: none;
                opacity: 0; transform: translateX(10px); transition: all 0.2s;
            }
            .cwm-float-action[data-title]:hover::after {
                opacity: 1; transform: translateX(0);
            }
        `;
        document.head.appendChild(style);

        // 悬浮菜单 DOM
        const container = document.createElement('div');
        container.id = 'cwm-float-container';
        container.innerHTML = `
            <div class="cwm-float-action" id="cwm-action-help" data-title="📖 使用说明">❓</div>
            <div class="cwm-float-action" id="cwm-action-add" data-title="➕ 一键允许当前网页">➕</div>
            <div class="cwm-float-action" id="cwm-action-set" data-title="⚙️ 打开设置面板">⚙️</div>
            <div class="cwm-float-main">🛡️</div>
        `;
        document.body.appendChild(container);

        // 绑定事件
        document.getElementById('cwm-action-set').addEventListener('click', createAndShowUI);
        document.getElementById('cwm-action-help').addEventListener('click', showHelpModal);

        document.getElementById('cwm-action-add').addEventListener('click', () => {
            const cleanUrl = getCleanUrl(window.location.href);
            if (!whitelist.includes(cleanUrl)) {
                whitelist.push(cleanUrl);
                GM_setValue('whitelist', whitelist);
                alert(`✅ 已成功将当前网页加入白名单:\n${cleanUrl}`);
                location.reload();
            } else {
                alert("✨ 当前网页已经在白名单中了!");
            }
        });
    }

    if (document.readyState === 'loading') {
        window.addEventListener('DOMContentLoaded', injectFloatingMenu);
    } else {
        injectFloatingMenu();
    }

    // 备用:篡改猴扩展菜单
    GM_registerMenuCommand("⚙️ 打开设置面板", createAndShowUI);

    // ==========================================
    // 4. 构建功能说明面板 (Help Modal)
    // ==========================================
    function showHelpModal() {
        if (document.getElementById('cwm-help-manager')) return;
        const helpOverlay = document.createElement('div');
        helpOverlay.id = 'cwm-help-manager';
        helpOverlay.innerHTML = `
            <div style="position:fixed; top:0; left:0; width:100vw; height:100vh; background:rgba(0,0,0,0.6); z-index:2147483647; display:flex; justify-content:center; align-items:center; backdrop-filter:blur(2px); font-family:sans-serif;">
                <div style="background:#fff; width:550px; max-width:90%; max-height:85vh; border-radius:12px; display:flex; flex-direction:column; overflow:hidden; box-shadow:0 10px 40px rgba(0,0,0,0.3);">
                    <div style="background:#f8f9fa; padding:16px 20px; border-bottom:1px solid #eee; display:flex; justify-content:space-between; align-items:center;">
                        <h2 style="margin:0; font-size:18px; color:#333;">📖 拦截器使用说明</h2>
                        <button id="cwm-help-close" style="background:none; border:none; font-size:24px; cursor:pointer; color:#999; line-height:1;">&times;</button>
                    </div>
                    <div style="padding:20px; overflow-y:auto; font-size:14px; color:#444; line-height:1.6;">
                        <h3 style="color:#1890ff; margin-top:0;">🛡️ 核心拦截规则</h3>
                        <p>1. 如果列表为空,则<b>允许访问所有网站</b>。</p>
                        <p>2. 加入列表的网址,其<b>本身及所有子网页</b>均可访问,但其<b>父级网页会被拦截</b>。</p>
                        <p style="background:#f6ffed; border:1px solid #b7eb8f; padding:10px; border-radius:6px;">
                            <b>举例:加入 <code>https://abc.com/test</code></b><br>
                            ✅ 允许: <code>https://abc.com/test</code><br>
                            ✅ 允许: <code>https://abc.com/test/123</code> (子网页)<br>
                            ❌ 拦截: <code>https://abc.com</code> (父网页) -> 瞬间跳回列表第一个网址。
                        </p>

                        <h3 style="color:#1890ff;">⚡ 快捷功能说明</h3>
                        <ul>
                            <li><b>➕ 一键允许:</b>点击悬浮球的 ➕,立刻将当前所处的网址加入白名单。如果需要临时探索新网站,可先在设置中关闭开关,进站后点击 ➕ 加入,再重新打开开关。</li>
                            <li><b>📥 种子系统:</b>为了方便在不同设备、浏览器之间同步你的白名单,点击【导出种子】会生成一段加密代码。在另一台电脑点击【导入种子】粘贴即可瞬间还原几十上百个网址!</li>
                            <li><b>📝 批量文本模式:</b>在设置面板右上角切换,可以直接像编辑记事本一样粘贴一大段网址列表。</li>
                        </ul>
                    </div>
                    <div style="padding:16px 20px; border-top:1px solid #eee; text-align:right; background:#f8f9fa;">
                        <button id="cwm-help-ok" style="padding:8px 24px; background:#1890ff; color:white; border:none; border-radius:6px; cursor:pointer; font-size:14px;">我明白了</button>
                    </div>
                </div>
            </div>
        `;
        document.body.appendChild(helpOverlay);
        const closeHelp = () => helpOverlay.remove();
        document.getElementById('cwm-help-close').addEventListener('click', closeHelp);
        document.getElementById('cwm-help-ok').addEventListener('click', closeHelp);
    }

    // ==========================================
    // 5. 构建可视化 UI 面板 (白名单设置中心)
    // ==========================================
    function createAndShowUI() {
        if (document.getElementById('custom-whitelist-manager')) return;

        let tempWhitelist = [...whitelist];
        let tempEnabled = isEnabled;
        let isTextMode = false;

        const style = document.createElement('style');
        style.innerHTML = `
            #custom-whitelist-manager { position: fixed; top: 0; left: 0; width: 100vw; height: 100vh; background: rgba(0,0,0,0.6); z-index: 2147483647; display: flex; justify-content: center; align-items: center; font-family: sans-serif; backdrop-filter: blur(2px); }
            .cwm-box { background: #fff; width: 550px; max-width: 95%; max-height: 85vh; border-radius: 12px; box-shadow: 0 10px 40px rgba(0,0,0,0.4); display: flex; flex-direction: column; overflow: hidden; }
            .cwm-header { background: #f8f9fa; padding: 16px 20px; border-bottom: 1px solid #eee; display: flex; justify-content: space-between; align-items: center; }
            .cwm-header h2 { margin: 0; font-size: 18px; color: #333; display: flex; gap: 10px; align-items:center; }
            .cwm-btn-textmode { font-size:12px; background:#e6f7ff; color:#1890ff; border:1px solid #91d5ff; padding:4px 8px; border-radius:4px; cursor:pointer; transition: 0.2s; }
            .cwm-btn-textmode:hover { background: #bae0ff; }
            .cwm-close { background: none; border: none; font-size: 24px; cursor: pointer; color: #999; line-height: 1; }
            .cwm-body { padding: 20px; overflow-y: auto; flex: 1; display: flex; flex-direction: column; }
            .cwm-switch-row { display: flex; justify-content: space-between; align-items: center; margin-bottom: 15px; background: #f0f7ff; padding: 10px 12px; border-radius: 8px; border: 1px solid #cce3fd; }
            .cwm-switch-row label { font-size: 15px; font-weight: bold; color: #0056b3; cursor: pointer; display: flex; align-items: center; gap: 8px; margin:0;}
            .cwm-seed-group { display: flex; gap: 8px; }
            .cwm-btn-seed { font-size:12px; background:#fff; border:1px solid #d9d9d9; padding:4px 8px; border-radius:4px; cursor:pointer; color:#555; transition:0.2s; }
            .cwm-btn-seed:hover { border-color:#1890ff; color:#1890ff; }
            .cwm-list { display: flex; flex-direction: column; gap: 10px; }
            .cwm-item { display: flex; gap: 10px; }
            .cwm-item input { flex: 1; padding: 8px 12px; border: 1px solid #ccc; border-radius: 6px; font-size: 14px; outline: none; }
            .cwm-btn-delete { background: #ff4d4f; color: white; border: none; padding: 0 12px; border-radius: 6px; cursor: pointer; font-size: 13px; }
            .cwm-btn-add { background: #fff; border: 1px dashed #1890ff; color: #1890ff; padding: 10px; border-radius: 6px; cursor: pointer; font-size: 14px; margin-top: 10px; text-align: center; }
            .cwm-textarea { width: 100%; box-sizing: border-box; flex: 1; min-height: 200px; padding: 12px; border: 1px solid #ccc; border-radius: 6px; font-size: 14px; outline: none; resize: none; font-family: monospace; white-space: pre; }
            .cwm-footer { padding: 16px 20px; border-top: 1px solid #eee; display: flex; justify-content: flex-end; gap: 12px; background: #f8f9fa; }
            .cwm-btn { padding: 8px 20px; border-radius: 6px; font-size: 14px; cursor: pointer; border: none; }
            .cwm-btn-cancel { background: #e0e0e0; color: #333; }
            .cwm-btn-save { background: #1890ff; color: white; }
        `;
        document.head.appendChild(style);

        const overlay = document.createElement('div');
        overlay.id = 'custom-whitelist-manager';
        overlay.innerHTML = `
            <div class="cwm-box">
                <div class="cwm-header">
                    <h2>🛡️ 白名单设置 <button class="cwm-btn-textmode" id="cwm-toggle-mode">切换批量文本模式</button></h2>
                    <button class="cwm-close" id="cwm-close-btn">&times;</button>
                </div>
                <div class="cwm-body">
                    <div class="cwm-switch-row">
                        <label><input type="checkbox" id="cwm-enable-checkbox" ${tempEnabled ? 'checked' : ''}> 启用拦截功能</label>
                        <div class="cwm-seed-group">
                            <button class="cwm-btn-seed" id="cwm-btn-export">🔗 导出种子</button>
                            <button class="cwm-btn-seed" id="cwm-btn-import">📥 导入种子</button>
                        </div>
                    </div>

                    <div id="cwm-view-list" style="display: flex; flex-direction: column;">
                        <div class="cwm-list" id="cwm-list-container"></div>
                        <div class="cwm-btn-add" id="cwm-add-btn">+ 添加新网址</div>
                    </div>

                    <div id="cwm-view-text" style="display: none; flex-direction: column; flex: 1; height: 100%;">
                        <div style="font-size:12px; color:#666; margin-bottom:8px;">每行一个网址,可以直接粘贴一大段:</div>
                        <textarea class="cwm-textarea" id="cwm-textarea-input"></textarea>
                    </div>
                </div>
                <div class="cwm-footer">
                    <button class="cwm-btn cwm-btn-cancel" id="cwm-cancel-btn">取消</button>
                    <button class="cwm-btn cwm-btn-save" id="cwm-save-btn">保存并生效</button>
                </div>
            </div>
        `;
        document.body.appendChild(overlay);

        const listContainer = document.getElementById('cwm-list-container');
        const viewList = document.getElementById('cwm-view-list');
        const viewText = document.getElementById('cwm-view-text');
        const textareaInput = document.getElementById('cwm-textarea-input');

        function renderList() {
            listContainer.innerHTML = '';
            if (tempWhitelist.length === 0) {
                listContainer.innerHTML = `<div style="text-align:center; color:#999; padding: 10px;">列表为空,允许访问所有网站。</div>`;
                return;
            }
            tempWhitelist.forEach((url, index) => {
                const itemDiv = document.createElement('div');
                itemDiv.className = 'cwm-item';
                itemDiv.innerHTML = `
                    <input type="text" value="${url}" data-index="${index}">
                    <button class="cwm-btn-delete" data-index="${index}">删除</button>
                `;
                listContainer.appendChild(itemDiv);
            });

            listContainer.querySelectorAll('input').forEach(input => {
                input.addEventListener('input', (e) => tempWhitelist[e.target.getAttribute('data-index')] = e.target.value);
            });
            listContainer.querySelectorAll('.cwm-btn-delete').forEach(btn => {
                btn.addEventListener('click', (e) => {
                    tempWhitelist.splice(parseInt(e.target.getAttribute('data-index')), 1);
                    renderList();
                });
            });
        }
        renderList();

        document.getElementById('cwm-add-btn').addEventListener('click', () => { tempWhitelist.push(''); renderList(); });
        document.getElementById('cwm-enable-checkbox').addEventListener('change', (e) => tempEnabled = e.target.checked);

        document.getElementById('cwm-toggle-mode').addEventListener('click', () => {
            isTextMode = !isTextMode;
            if (isTextMode) {
                viewList.style.display = 'none';
                viewText.style.display = 'flex';
                textareaInput.value = tempWhitelist.filter(u => u.trim() !== '').join('\n');
            } else {
                viewText.style.display = 'none';
                viewList.style.display = 'flex';
                tempWhitelist = textareaInput.value.split('\n').map(u => u.trim()).filter(u => u !== '');
                renderList();
            }
        });

        // 种子系统
        document.getElementById('cwm-btn-export').addEventListener('click', () => {
            if (isTextMode) tempWhitelist = textareaInput.value.split('\n').map(u => u.trim()).filter(u => u !== '');
            const cleanedList = tempWhitelist.map(url => url.trim()).filter(url => url !== '');
            if (cleanedList.length === 0) return alert("列表为空,无法生成种子。");
            const seed = "CWM-SEED-" + btoa(encodeURIComponent(JSON.stringify(cleanedList)));
            navigator.clipboard.writeText(seed).then(() => {
                alert("✅ 种子已成功复制到剪贴板!\n你可以将其发送给其他设备,或在另一台电脑上点击【导入种子】恢复列表。");
            }).catch(() => prompt("自动复制失败,请手动复制以下种子代码:", seed));
        });

        document.getElementById('cwm-btn-import').addEventListener('click', () => {
            const input = prompt("请输入或粘贴你的白名单种子代码:\n(以 CWM-SEED- 开头)");
            if (!input) return;
            if (!input.startsWith("CWM-SEED-")) return alert("❌ 无效的种子格式!请确保包含了 CWM-SEED- 前缀。");
            try {
                const decodedList = JSON.parse(decodeURIComponent(atob(input.substring(9))));
                if (Array.isArray(decodedList)) {
                    tempWhitelist = decodedList;
                    renderList();
                    if (isTextMode) textareaInput.value = tempWhitelist.join('\n');
                    alert("🎉 种子导入成功!目前共解析到 " + decodedList.length + " 个网址。\n\n⚠️ 请点击右下角的【保存并生效】以保存数据。");
                } else alert("❌ 种子数据异常,导入失败。");
            } catch (error) {
                alert("❌ 种子解析失败,可能是复制不完整或代码被篡改。");
            }
        });

        const closeUI = () => { overlay.remove(); style.remove(); };
        document.getElementById('cwm-close-btn').addEventListener('click', closeUI);
        document.getElementById('cwm-cancel-btn').addEventListener('click', closeUI);

        document.getElementById('cwm-save-btn').addEventListener('click', () => {
            if (isTextMode) tempWhitelist = textareaInput.value.split('\n').map(u => u.trim()).filter(u => u !== '');
            const cleanedList = tempWhitelist.map(url => url.trim()).filter(url => url !== '');
            GM_setValue('isEnabled', tempEnabled);
            GM_setValue('whitelist', cleanedList);
            alert('保存成功!');
            closeUI();
            location.reload();
        });
    }

})();