MediaWiki

Gadget-CopyTextTool.js:修订间差异

来自卡厄思梦境WIKI

律Rhyme留言 | 贡献
创建页面,内容为“// 文本复制工具 (function() { 'use strict'; // ===== 自定义配置区域 ===== const BUTTON_CONFIG = { '蓝色': '{{文本|蓝|', '绿色': '{{文本|绿|', '蓝色下划线': '{{文本|蓝|下划线|', '绿色描边': '{{描边|绿|', '词典': '{{词典|' }; // ======================== let toolElement = null; let isPinned = true; let isDragging = false; let currentX;…”
 
律Rhyme留言 | 贡献
无编辑摘要
第1行: 第1行:
// 文本复制工具
/**
(function() {
* 文本复制工具 - 简化测试版
*/
(function($, mw) {
     'use strict';
     'use strict';
      
      
     // ===== 自定义配置区域 =====
     console.log('文本复制工具已加载'); // 调试信息
     const BUTTON_CONFIG = {
      
    // 配置
    var config = {
         '蓝色': '{{文本|蓝|',
         '蓝色': '{{文本|蓝|',
         '绿色': '{{文本|绿|',
         '绿色': '{{文本|绿|',
第11行: 第15行:
         '词典': '{{词典|'
         '词典': '{{词典|'
     };
     };
    // ========================
      
      
     let toolElement = null;
     // 等待页面加载完成
    let isPinned = true;
    $(function() {
    let isDragging = false;
        console.log('DOM已加载,准备添加工具'); // 调试信息
    let currentX;
       
    let currentY;
        // 添加菜单链接
    let initialX;
        var link = mw.util.addPortletLink(
    let initialY;
            'p-tb',
    let xOffset = 0;
            '#',
     let yOffset = 0;
            '📋 文本复制工具',
            '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 createToolPanel() {
     function createTool() {
         const tool = document.createElement('div');
         console.log('创建工具面板'); // 调试信息
         tool.id = 'copy-text-tool';
          
         tool.innerHTML = `
        var $tool = $('<div>')
            <div class="tool-header" id="tool-header">
            .attr('id', 'copy-text-tool')
                 <span class="tool-title">📋 文本复制工具</span>
            .css({
                 <div class="tool-controls">
                position: 'fixed',
                    <button class="tool-btn" id="minimize-btn" title="最小化">−</button>
                top: '100px',
                    <button class="tool-btn" id="close-btn" title="关闭">×</button>
                right: '20px',
                 </div>
                width: '300px',
             </div>
                background: 'white',
             <div class="tool-body">
                border: '2px solid #2196F3',
                 <div class="pin-control">
                borderRadius: '8px',
                     <input type="checkbox" id="pin-checkbox" ${isPinned ? 'checked' : ''}>
                boxShadow: '0 4px 6px rgba(0,0,0,0.1)',
                     <label for="pin-checkbox">窗口置顶</label>
                zIndex: 9999,
                </div>
                padding: '15px'
                <div id="button-container"></div>
            });
                <div class="tool-status" id="tool-status">准备就绪</div>
          
             </div>
        // 标题
        `;
        $('<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);
          
          
         // 添加按钮
         // 添加按钮
         const buttonContainer = tool.querySelector('#button-container');
         $.each(config, function(name, text) {
        for (const [name, text] of Object.entries(BUTTON_CONFIG)) {
            $('<button>')
            const button = document.createElement('button');
                .text(name)
             button.className = 'copy-button';
                .css({
             button.textContent = name;
                    width: '100%',
             button.onclick = () => copyText(text, name);
                    padding: '12px',
            buttonContainer.appendChild(button);
                    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');
          
          
         document.body.appendChild(tool);
         // 拖动功能
        return tool;
        makeDraggable($tool[0]);
     }
     }
      
      
     // 复制文本到剪贴板
     // 复制文本
     function copyText(text, buttonName) {
     function copyText(text, name) {
         // 使用现代剪贴板API
         console.log('复制文本:', name, text); // 调试信息
         if (navigator.clipboard && navigator.clipboard.writeText) {
       
             navigator.clipboard.writeText(text).then(() => {
         var $temp = $('<textarea>')
                 showStatus(`✓ 已复制: ${buttonName}`, true);
             .val(text)
            }).catch(() => {
            .css({
                 fallbackCopy(text, buttonName);
                 position: 'fixed',
             });
                 opacity: 0
        } else {
             })
             fallbackCopy(text, buttonName);
             .appendTo('body');
        }
          
    }
         $temp[0].select();
   
          
    // 备用复制方法
    function fallbackCopy(text, buttonName) {
        const textarea = document.createElement('textarea');
         textarea.value = text;
        textarea.style.position = 'fixed';
         textarea.style.opacity = '0';
        document.body.appendChild(textarea);
         textarea.select();
         try {
         try {
             document.execCommand('copy');
             document.execCommand('copy');
             showStatus(`✓ 已复制: ${buttonName}`, true);
             $('#copy-status')
         } catch (err) {
                .text('✓ 已复制: ' + name)
             showStatus('❌ 复制失败', false);
                .css('color', '#2196F3');
            console.log('复制成功'); // 调试信息
         } catch(err) {
             $('#copy-status')
                .text('❌ 复制失败')
                .css('color', '#f44336');
            console.error('复制失败:', err); // 调试信息
         }
         }
         document.body.removeChild(textarea);
          
        $temp.remove();
     }
     }
      
      
     // 显示状态
     // 简单拖动实现
     function showStatus(message, success) {
     function makeDraggable(element) {
         const status = document.getElementById('tool-status');
         var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
         status.textContent = message;
         element.onmousedown = dragMouseDown;
         status.className = 'tool-status' + (success ? ' success' : '');
          
    }
        function dragMouseDown(e) {
   
            e = e || window.event;
    // 拖拽功能
            e.preventDefault();
    function dragStart(e) {
             pos3 = e.clientX;
        if (e.type === "touchstart") {
             pos4 = e.clientY;
             initialX = e.touches[0].clientX - xOffset;
             document.onmouseup = closeDragElement;
             initialY = e.touches[0].clientY - yOffset;
             document.onmousemove = elementDrag;
        } else {
             initialX = e.clientX - xOffset;
             initialY = e.clientY - yOffset;
         }
         }
          
          
         if (e.target.id === "tool-header" || e.target.className === "tool-title") {
         function elementDrag(e) {
             isDragging = true;
             e = e || window.event;
        }
    }
   
    function drag(e) {
        if (isDragging) {
             e.preventDefault();
             e.preventDefault();
              
             pos1 = pos3 - e.clientX;
            if (e.type === "touchmove") {
            pos2 = pos4 - e.clientY;
                currentX = e.touches[0].clientX - initialX;
             pos3 = e.clientX;
                currentY = e.touches[0].clientY - initialY;
            pos4 = e.clientY;
             } else {
             element.style.top = (element.offsetTop - pos2) + "px";
                currentX = e.clientX - initialX;
             element.style.left = (element.offsetLeft - pos1) + "px";
                currentY = e.clientY - initialY;
             element.style.right = "auto";
             }
           
            xOffset = currentX;
             yOffset = currentY;
              
            setTranslate(currentX, currentY, toolElement);
         }
         }
    }
   
    function dragEnd(e) {
        initialX = currentX;
        initialY = currentY;
        isDragging = false;
    }
   
    function setTranslate(xPos, yPos, el) {
        el.style.transform = `translate3d(${xPos}px, ${yPos}px, 0)`;
    }
   
    // 初始化
    function init() {
        // 添加菜单项
        mw.util.addPortletLink(
            'p-tb',
            '#',
            '📋 文本复制工具',
            't-copy-text-tool',
            '打开文本复制工具',
            null,
            '#t-permalink'
        );
          
          
         // 点击菜单项显示工具
         function closeDragElement() {
        document.getElementById('t-copy-text-tool').addEventListener('click', function(e) {
             document.onmouseup = null;
             e.preventDefault();
            document.onmousemove = null;
            if (!toolElement) {
         }
                toolElement = createToolPanel();
               
                // 绑定事件
                const header = document.getElementById('tool-header');
                header.addEventListener('mousedown', dragStart);
                document.addEventListener('mousemove', drag);
                document.addEventListener('mouseup', dragEnd);
               
                // 最小化按钮
                document.getElementById('minimize-btn').onclick = () => {
                    toolElement.classList.toggle('minimized');
                };
               
                // 关闭按钮
                document.getElementById('close-btn').onclick = () => {
                    toolElement.remove();
                    toolElement = null;
                };
               
                // 置顶开关
                document.getElementById('pin-checkbox').onchange = (e) => {
                    isPinned = e.target.checked;
                    toolElement.style.position = isPinned ? 'fixed' : 'absolute';
                };
            } else {
                toolElement.style.display = toolElement.style.display === 'none' ? 'block' : 'none';
            }
         });
     }
     }
      
      
    // 页面加载完成后初始化
})(jQuery, mediaWiki);
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }
})();

2025年10月3日 (五) 20:39的版本

/**
 * 文本复制工具 - 简化测试版
 */
(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);