Greasy Fork

Greasy Fork is available in English.

自动复制选中文本和解除复制限制按钮 by lbihhe

在任意网站选中任意文本时自动复制,并添加一个按钮以启用/禁用解除网站的复制限制和自动复制功能

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        自动复制选中文本和解除复制限制按钮 by lbihhe
// @namespace   http://tampermonkey.net/
// @version     1.6
// @description 在任意网站选中任意文本时自动复制,并添加一个按钮以启用/禁用解除网站的复制限制和自动复制功能
// @author      lbihhe
// @license     MIT
// @match       *://*/*
// @grant       none
// ==/UserScript==

(function() {
    'use strict';

    // 定义一个对象来存储状态和按钮
    var copyState = {
        enabled: false,
        button: createButton()
    };

    // 创建按钮
    function createButton() {
        var button = document.createElement('button');
        button.innerHTML = '解除复制限制并启用自动复制';
        button.style.position = 'fixed';
        button.style.top = '40px';
        button.style.right = '10px';
        button.style.zIndex = '9999';
        button.style.padding = '10px';
        button.style.backgroundColor = 'rgba(255, 255, 224, 0.6)';
        button.style.color = '#000';
        button.style.border = 'none';
        button.style.borderRadius = '5px';
        button.style.cursor = 'pointer';
        button.style.fontFamily = '微软雅黑, Arial, sans-serif';
        button.style.fontSize = '14px';
        document.body.appendChild(button);
        return button;
    }

    // 停止事件传播的处理函数
    function stopPropagation(e) {
        e.stopPropagation();
    }

    // 自动复制选中文本的处理函数
    function autoCopyHandler() {
        if (copyState.enabled) {
            var selectedText = window.getSelection().toString().trim();
            if (selectedText) {
                copyTextToClipboard(selectedText);
            }
        }
    }

    // 将文本复制到剪贴板
    function copyTextToClipboard(text) {
        var tempTextarea = document.createElement('textarea');
        tempTextarea.style.position = 'fixed';
        tempTextarea.style.top = '0';
        tempTextarea.style.left = '0';
        tempTextarea.style.width = '2em';
        tempTextarea.style.height = '2em';
        tempTextarea.style.padding = '0';
        tempTextarea.style.border = 'none';
        tempTextarea.style.outline = 'none';
        tempTextarea.style.boxShadow = 'none';
        tempTextarea.style.background = 'transparent';
        tempTextarea.value = text;
        document.body.appendChild(tempTextarea);
        tempTextarea.select();
        try {
            var successful = document.execCommand('copy');
            if (successful) {
                console.log('选中文本已复制: ' + text);
            } else {
                console.error('复制失败');
            }
        } catch (err) {
            console.error('复制过程中出现异常', err);
        }
        document.body.removeChild(tempTextarea);
    }

    // 解除复制限制的函数
    function enableCopy() {
        // 移除常见的禁止复制的事件监听器
        document.addEventListener('copy', stopPropagation, true);
        document.addEventListener('cut', stopPropagation, true);
        document.addEventListener('contextmenu', stopPropagation, true);
        document.addEventListener('selectstart', stopPropagation, true);
        document.addEventListener('mousedown', stopPropagation, true);
        document.addEventListener('mouseup', autoCopyHandler, true);
        document.addEventListener('keydown', stopPropagation, true);
        document.addEventListener('keyup', stopPropagation, true);
        document.addEventListener('keypress', stopPropagation, true);

        // 解除 CSS 样式限制
        var css = '* { -webkit-user-select: auto !important; -moz-user-select: auto !important; -ms-user-select: auto !important; user-select: auto !important; }';
        var style = document.createElement('style');
        style.type = 'text/css';
        style.appendChild(document.createTextNode(css));
        document.head.appendChild(style);

        // 处理 body 标签的 oncontextmenu 属性
        if (document.body) {
            document.body.oncontextmenu = null;
        }

        // 处理常见的框架
        var frames = document.getElementsByTagName('iframe');
        for (var i = 0; i < frames.length; i++) {
            try {
                var frameDoc = frames[i].contentWindow.document;
                frameDoc.addEventListener('copy', stopPropagation, true);
                frameDoc.addEventListener('cut', stopPropagation, true);
                frameDoc.addEventListener('contextmenu', stopPropagation, true);
                frameDoc.addEventListener('selectstart', stopPropagation, true);
                frameDoc.addEventListener('mousedown', stopPropagation, true);
                frameDoc.addEventListener('mouseup', autoCopyHandler, true);
                frameDoc.addEventListener('keydown', stopPropagation, true);
                frameDoc.addEventListener('keyup', stopPropagation, true);
                frameDoc.addEventListener('keypress', stopPropagation, true);
            } catch (e) {
                console.error('无法访问iframe内容:', e);
            }
        }
    }

    // 禁用复制功能的函数
    function disableCopy() {
        // 恢复默认事件
        document.removeEventListener('copy', stopPropagation, true);
        document.removeEventListener('cut', stopPropagation, true);
        document.removeEventListener('contextmenu', stopPropagation, true);
        document.removeEventListener('selectstart', stopPropagation, true);
        document.removeEventListener('mousedown', stopPropagation, true);
        document.removeEventListener('mouseup', autoCopyHandler, true);
        document.removeEventListener('keydown', stopPropagation, true);
        document.removeEventListener('keyup', stopPropagation, true);
        document.removeEventListener('keypress', stopPropagation, true);

        // 移除 CSS 样式限制
        var style = document.querySelector('style');
        if (style) {
            document.head.removeChild(style);
        }

        // 恢复默认 body 标签的 oncontextmenu 属性
        if (document.body) {
            document.body.oncontextmenu = null;
        }
    }

    // 按钮点击事件 - 启用/禁用解除复制限制和自动复制
    copyState.button.addEventListener('click', function() {
        if (copyState.enabled) {
            disableCopy();
            copyState.button.innerHTML = '解除复制限制并启用自动复制';
        } else {
            enableCopy();
            copyState.button.innerHTML = '禁用复制功能';
        }
        copyState.enabled = !copyState.enabled;
    });
})();