MediaWiki

MediaWiki:Gadget-CopyTextTool.js

来自卡厄思梦境WIKI

律Rhyme留言 | 贡献2025年10月3日 (五) 20:39的版本

注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
/**
 * 文本复制工具 - 简化测试版
 */
(function($, mw) {
    'use strict';
    
    console.log('文本复制工具已加载'); // 调试信息
    
    // 配置
    var config = {
        '蓝色': '{{文本|蓝|',
        '绿色': '{{文本|绿|',
        '蓝色下划线': '{{文本|蓝|下划线|',
        '绿色描边': '{{描边|绿|',
        '词典': '{{词典|'
    };
    
    // 等待页面加载完成
    $(function() {
        console.log('DOM已加载,准备添加工具'); // 调试信息
        
        // 添加菜单链接
        var link = mw.util.addPortletLink(
            'p-tb',
            '#',
            '📋 文本复制工具',
            't-copy-text-tool',
            '打开文本复制工具'
        );
        
        if (!link) {
            console.error('无法添加菜单链接');
            return;
        }
        
        console.log('菜单链接已添加'); // 调试信息
        
        // 点击事件
        $(link).on('click', function(e) {
            e.preventDefault();
            console.log('工具被点击'); // 调试信息
            
            if ($('#copy-text-tool').length) {
                $('#copy-text-tool').toggle();
                return;
            }
            
            createTool();
        });
    });
    
    // 创建工具面板
    function createTool() {
        console.log('创建工具面板'); // 调试信息
        
        var $tool = $('<div>')
            .attr('id', 'copy-text-tool')
            .css({
                position: 'fixed',
                top: '100px',
                right: '20px',
                width: '300px',
                background: 'white',
                border: '2px solid #2196F3',
                borderRadius: '8px',
                boxShadow: '0 4px 6px rgba(0,0,0,0.1)',
                zIndex: 9999,
                padding: '15px'
            });
        
        // 标题
        $('<div>')
            .css({
                background: '#2196F3',
                color: 'white',
                padding: '10px',
                marginBottom: '15px',
                borderRadius: '5px',
                fontWeight: 'bold'
            })
            .text('📋 文本复制工具')
            .append(
                $('<button>')
                    .text('×')
                    .css({
                        float: 'right',
                        background: 'none',
                        border: 'none',
                        color: 'white',
                        fontSize: '20px',
                        cursor: 'pointer'
                    })
                    .on('click', function() {
                        $tool.hide();
                    })
            )
            .appendTo($tool);
        
        // 添加按钮
        $.each(config, function(name, text) {
            $('<button>')
                .text(name)
                .css({
                    width: '100%',
                    padding: '12px',
                    marginBottom: '8px',
                    background: '#2196F3',
                    color: 'white',
                    border: 'none',
                    borderRadius: '5px',
                    cursor: 'pointer',
                    fontSize: '14px'
                })
                .on('click', function() {
                    copyText(text, name);
                })
                .hover(
                    function() { $(this).css('background', '#1976D2'); },
                    function() { $(this).css('background', '#2196F3'); }
                )
                .appendTo($tool);
        });
        
        // 状态栏
        var $status = $('<div>')
            .attr('id', 'copy-status')
            .css({
                textAlign: 'center',
                color: '#666',
                padding: '8px',
                fontSize: '12px',
                marginTop: '10px'
            })
            .text('准备就绪')
            .appendTo($tool);
        
        $tool.appendTo('body');
        
        // 拖动功能
        makeDraggable($tool[0]);
    }
    
    // 复制文本
    function copyText(text, name) {
        console.log('复制文本:', name, text); // 调试信息
        
        var $temp = $('<textarea>')
            .val(text)
            .css({
                position: 'fixed',
                opacity: 0
            })
            .appendTo('body');
        
        $temp[0].select();
        
        try {
            document.execCommand('copy');
            $('#copy-status')
                .text('✓ 已复制: ' + name)
                .css('color', '#2196F3');
            console.log('复制成功'); // 调试信息
        } catch(err) {
            $('#copy-status')
                .text('❌ 复制失败')
                .css('color', '#f44336');
            console.error('复制失败:', err); // 调试信息
        }
        
        $temp.remove();
    }
    
    // 简单拖动实现
    function makeDraggable(element) {
        var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
        element.onmousedown = dragMouseDown;
        
        function dragMouseDown(e) {
            e = e || window.event;
            e.preventDefault();
            pos3 = e.clientX;
            pos4 = e.clientY;
            document.onmouseup = closeDragElement;
            document.onmousemove = elementDrag;
        }
        
        function elementDrag(e) {
            e = e || window.event;
            e.preventDefault();
            pos1 = pos3 - e.clientX;
            pos2 = pos4 - e.clientY;
            pos3 = e.clientX;
            pos4 = e.clientY;
            element.style.top = (element.offsetTop - pos2) + "px";
            element.style.left = (element.offsetLeft - pos1) + "px";
            element.style.right = "auto";
        }
        
        function closeDragElement() {
            document.onmouseup = null;
            document.onmousemove = null;
        }
    }
    
})(jQuery, mediaWiki);