Greasy Fork

Greasy Fork is available in English.

解除复制限制

解除网页复制限制并提供可视化控制

当前为 2025-02-01 提交的版本,查看 最新版本

// ==UserScript==
// @name            解除复制限制
// @name:zh         解除复制限制
// @name:en         Unlock Copy Restrictions
// @name:ja         コピー制限解除
// @name:ko         복사 제한 해제
// @name:es         Desbloquear restricciones de copia
// @namespace       gura8390/copy/2
// @version         1.6
// @license         MIT
// @icon            https://img.icons8.com/nolan/64/password1.png
// @description     解除网页复制限制并提供可视化控制
// @description:zh  解除网页复制限制并提供可视化控制
// @description:en  Unlock web copy restrictions with visual control
// @description:ja  ウェブのコピー制限を解除し、視覚的な制御を提供。
// @description:ko  웹 페이지의 복사 제한을 해제하고 시각적 제어를 제공하며
// @description:es Desbloquea restricciones de copia en la web y proporciona control visual
// @author          lbihhe
// @match           *://*/*
// @grant           GM_getValue
// @grant           GM_setValue
// @grant           GM_registerMenuCommand
// @grant           GM_xmlhttpRequest
// @run-at          document-end
// ==/UserScript==

(function() {
    'use strict';

    // =====================
    // 1. 多语言本地化配置
    // =====================
    const locales = {
        en: {
            menu_toggle_script: "🔄 Toggle Script State",
            menu_toggle_button: "👁️ Toggle Button Display",
            btn_unlock: "🔓 Unlock Restrictions",
            btn_lock: "🔒 Restore Defaults",
            toast_unlocked: "✔️ Copy restrictions unlocked!",
            toast_locked: "✔️ Restrictions restored!"
        },
        zh: {
            menu_toggle_script: "🔄 切换脚本状态",
            menu_toggle_button: "👁️ 切换按钮显示",
            btn_unlock: "🔓 解除限制",
            btn_lock: "🔒 恢复原状",
            toast_unlocked: "✔️ 复制限制已解除!",
            toast_locked: "✔️ 限制已恢复!"
        },
        ja: {
            menu_toggle_script: "🔄 スクリプト状態を切り替え",
            menu_toggle_button: "👁️ ボタン表示を切り替え",
            btn_unlock: "🔓 制限解除",
            btn_lock: "🔒 デフォルトに戻す",
            toast_unlocked: "✔️ コピー制限が解除されました!",
            toast_locked: "✔️ 制限が復元されました!"
        },
        ko: {
            menu_toggle_script: "🔄 스크립트 상태 전환",
            menu_toggle_button: "👁️ 버튼 표시 전환",
            btn_unlock: "🔓 제한 해제",
            btn_lock: "🔒 기본값 복원",
            toast_unlocked: "✔️ 복사 제한이 해제되었습니다!",
            toast_locked: "✔️ 제한이 복원되었습니다!"
        },
        es: {
            menu_toggle_script: "🔄 Cambiar estado del script",
            menu_toggle_button: "👁️ Cambiar visualización del botón",
            btn_unlock: "🔓 Desbloquear restricciones",
            btn_lock: "🔒 Restaurar valores predeterminados",
            toast_unlocked: "✔️ ¡Restricciones de copia desbloqueadas!",
            toast_locked: "✔️ ¡Restricciones restauradas!"
        }
    };

    // 根据浏览器语言决定使用哪种语言环境
    const lang = navigator.language.toLowerCase();
    let userLang = 'en'; // 默认使用英文
    if (lang.startsWith('zh')) {
        userLang = 'zh';
    } else if (lang.startsWith('ja')) {
        userLang = 'ja';
    } else if (lang.startsWith('ko')) {
        userLang = 'ko';
    } else if (lang.startsWith('es')) {
        userLang = 'es';
    }
    const t = locales[userLang];

    // =============================
    // 2. 解除复制限制的核心逻辑
    // =============================
    const CONFIG = {
        ENABLED: 'copy_enabled',   // true:解除限制,false:恢复原状
        SHOW_BUTTON: 'show_button'
    };

    let unlockStyle = null;
    // 用于阻止其它脚本干扰复制行为的事件处理函数
    const stopPropagation = function(e) {
        e.stopPropagation();
    };
    const eventsList = ['contextmenu', 'copy', 'selectstart'];

    const initConfig = () => {
        if (GM_getValue(CONFIG.ENABLED, null) === null) {
            GM_setValue(CONFIG.ENABLED, true);
        }
        if (GM_getValue(CONFIG.SHOW_BUTTON, null) === null) {
            GM_setValue(CONFIG.SHOW_BUTTON, true);
        }
    };

    const registerMenu = () => {
        GM_registerMenuCommand(t.menu_toggle_script, () => {
            const current = GM_getValue(CONFIG.ENABLED);
            GM_setValue(CONFIG.ENABLED, !current);
            location.reload();
        });

        GM_registerMenuCommand(t.menu_toggle_button, () => {
            GM_setValue(CONFIG.SHOW_BUTTON, !GM_getValue(CONFIG.SHOW_BUTTON));
            location.reload();
        });
    };

    // 解除复制限制:注入覆盖 CSS 与事件拦截
    const unlockCopy = () => {
        if (!unlockStyle) {
            unlockStyle = document.createElement('style');
            unlockStyle.id = 'copy-unlocker-style';
            unlockStyle.innerHTML = `
                * {
                    user-select: auto !important;
                    -webkit-user-select: auto !important;
                    -moz-user-select: auto !important;
                    -ms-user-select: auto !important;
                }
            `;
            document.head.appendChild(unlockStyle);
        }
        eventsList.forEach(event => {
            document.body.addEventListener(event, stopPropagation, true);
        });
    };

    // 恢复原状:移除覆盖样式与事件监听
    const restoreCopy = () => {
        if (unlockStyle) {
            unlockStyle.remove();
            unlockStyle = null;
        }
        eventsList.forEach(event => {
            document.body.removeEventListener(event, stopPropagation, true);
        });
    };

    // 提示动画(toast)
    const showSuccessToast = (msg, bgColor = '#4CAF50') => {
        const toast = document.createElement('div');
        toast.innerHTML = msg;
        toast.style.cssText = `
            position: fixed;
            bottom: 80px;
            right: 20px;
            background: ${bgColor};
            color: white;
            padding: 12px 24px;
            border-radius: 8px;
            z-index: 9999;
            opacity: 0;
            animation: fadeSlideIn 0.6s forwards, fadeOut 0.6s 2.5s forwards;
            box-shadow: 0 4px 12px rgba(0,0,0,0.15);
        `;
        if (!document.getElementById('toast-animations')) {
            const style = document.createElement('style');
            style.id = 'toast-animations';
            style.innerHTML = `
                @keyframes fadeSlideIn {
                    0% { transform: translateY(100%); opacity: 0; }
                    60% { transform: translateY(-10px); opacity: 1; }
                    100% { transform: translateY(0); opacity: 1; }
                }
                @keyframes fadeOut {
                    to { opacity: 0; }
                }
            `;
            document.head.appendChild(style);
        }
        document.body.appendChild(toast);
        setTimeout(() => {
            toast.remove();
        }, 3200);
    };

    // 创建浮动按钮(现代化样式)
    const createFloatButton = () => {
        const btn = document.createElement('button');
        btn.id = 'copy-unlocker-btn';
        updateButtonLabel();

        // 采用更现代化的样式:扁平化设计、渐变背景、柔和阴影
        btn.style.cssText = `
            position: fixed;
            bottom: 20px;
            right: 20px;
            z-index: 9999;
            padding: 14px 28px;
            background: linear-gradient(45deg, #00c6ff, #0072ff);
            color: #fff;
            border: none;
            border-radius: 10px;
            cursor: pointer;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
            transition: transform 0.2s ease, box-shadow 0.2s ease;
            font-size: 16px;
        `;

        // 悬停时的效果
        btn.addEventListener('mouseenter', () => {
            btn.style.transform = 'scale(1.05)';
            btn.style.boxShadow = '0 4px 12px rgba(0, 0, 0, 0.25)';
        });
        btn.addEventListener('mouseleave', () => {
            btn.style.transform = 'scale(1)';
            btn.style.boxShadow = '0 2px 8px rgba(0, 0, 0, 0.15)';
        });
        btn.addEventListener('click', () => {
            const enabled = GM_getValue(CONFIG.ENABLED);
            if (enabled) {
                restoreCopy();
                GM_setValue(CONFIG.ENABLED, false);
                showSuccessToast(t.toast_locked, '#F44336');
            } else {
                unlockCopy();
                GM_setValue(CONFIG.ENABLED, true);
                showSuccessToast(t.toast_unlocked, '#4CAF50');
            }
            updateButtonLabel();
        });
        function updateButtonLabel(){
            if (GM_getValue(CONFIG.ENABLED)) {
                btn.innerHTML = t.btn_lock;
            } else {
                btn.innerHTML = t.btn_unlock;
            }
        }
        return btn;
    };

    // ===============================
    // 3. 针对 doc88.com 的特殊优化处理
    // ===============================
    // 声明全局变量 path(用于后续获取文本的属性链)
    let path = "";

    const website_rule_doc88 = {
        regexp: /.*doc88\.com\/.+/,
        init: () => {
            // 隐藏左侧菜单
            const style = document.createElement('style');
            style.id = "copy-element-hide";
            style.innerHTML = "#left-menu { display: none !important; }";
            document.head.appendChild(style);

            // 远程请求 doc88 的主要 JS 模块,提取 cp_textarea 的相关数据
            GM_xmlhttpRequest({
                method: "GET",
                url: "https://res3.doc88.com/resources/js/modules/main-v2.min.js?v=2.56",
                onload: function(response) {
                    const result = /\("#cp_textarea"\)\.val\(([\S]*?)\);/.exec(response.responseText);
                    if (result) {
                        path = result[1];
                    }
                }
            });

            // 页面加载完成后,尝试从 copyText 函数中提取属性链
            window.addEventListener("load", () => {
                if (typeof unsafeWindow.copyText === "function") {
                    const cpFn = unsafeWindow.copyText.toString();
                    const fnResult = /<textarea[\s\S]*?>'\+([\S]*?)\+"<\/textarea>/.exec(cpFn);
                    if (fnResult) {
                        path = fnResult[1];
                    }
                }
            });
        },
        getSelectedText: () => {
            let select = unsafeWindow;
            // 依据提取到的 path 逐级获取目标文本
            path.split(".").forEach((v) => {
                select = select[v];
            });
            if (!select) {
                // 如果获取失败,则强制设置 vip 和 logined 为 1,并移除隐藏样式
                if (unsafeWindow.Config) {
                    unsafeWindow.Config.vip = 1;
                    unsafeWindow.Config.logined = 1;
                }
                const el = document.getElementById("copy-element-hide");
                if (el) {
                    el.remove();
                }
            }
            return select;
        }
    };

    // 如果当前页面属于 doc88.com,则执行其特殊初始化
    if (website_rule_doc88.regexp.test(window.location.href)) {
        website_rule_doc88.init();
    }

    // ===============================
    // 4. 主执行函数
    // ===============================
    const main = () => {
        initConfig();
        registerMenu();

        // 根据配置决定解除或恢复复制限制
        if (GM_getValue(CONFIG.ENABLED)) {
            unlockCopy();
        } else {
            restoreCopy();
        }

        // 如果允许,则添加浮动控制按钮
        if (GM_getValue(CONFIG.SHOW_BUTTON)) {
            document.body.appendChild(createFloatButton());
        }
    };

    main();

})();