MediaWiki

Gadget-TierListMaker.js:修订间差异

来自卡厄思梦境WIKI

律Rhyme留言 | 贡献
无编辑摘要
律Rhyme留言 | 贡献
无编辑摘要
 
第1行: 第1行:
/**
// MediaWiki:Gadget-TierListMaker.js
* 战斗员图鉴分级表制作工具
 
* 支持拖放卡片到表格并导出为PNG
*/
(function() {
(function() {
     'use strict';
     'use strict';
 
   
     // 仅在特定页面启用
     // 仅在 TierListMaker 页面运行
     if (mw.config.get('wgPageName') !== '节奏榜') {
     if (mw.config.get('wgPageName') !== '节奏榜') {
         return;
         return;
     }
     }
 
   
     // 等待页面加载完成
     // 等待页面加载完成
     mw.loader.using(['mediawiki.util', 'mediawiki.api']).then(function() {
     $(function() {
         init();
         initTierListMaker();
     });
     });
 
   
     function init() {
     function initTierListMaker() {
         // 添加控制按钮
         // 添加控制按钮
         addControlPanel();
         addControlButtons();
       
        // 使卡片可拖动
        makeCardsDraggable();
          
          
         // 使表格单元格可接收
         // 初始化拖拽功能
         makeTableDroppable();
         initDragAndDrop();
          
          
         // 添加样式
         // 添加样式
         addStyles();
         addCustomStyles();
       
        // 恢复面板状态
        restorePanelState();
     }
     }
 
   
     function addControlPanel() {
     function addControlButtons() {
         var $panel = $('<div>')
         var $buttonContainer = $('<div>')
             .attr('id', 'tierlist-control-panel')
             .attr('id', 'tier-list-controls')
             .css({
             .css({
                 'position': 'fixed',
                 'margin': '10px 0',
                'top': '100px',
                 'padding': '10px',
                'right': '20px',
                 'background': '#f0f0f0',
                 'z-index': '9999',
                 'border-radius': '5px'
                 'background': '#fff',
                'border': '2px solid #0645ad',
                 'border-radius': '8px',
                'box-shadow': '0 4px 6px rgba(0,0,0,0.1)',
                'min-width': '200px',
                'transition': 'transform 0.3s ease'
             });
             });
 
          
         // 标题栏(可点击折叠)
         var $saveButton = $('<button>')
         var $header = $('<div>')
             .text('保存为PNG')
             .attr('id', 'tierlist-panel-header')
             .addClass('tier-list-save-btn')
             .css({
             .click(saveTierListAsPNG);
                'background': '#0645ad',
       
                'color': '#fff',
        var $clearButton = $('<button>')
                'padding': '10px 15px',
            .text('清空所有')
                'border-radius': '6px 6px 0 0',
            .addClass('tier-list-clear-btn')
                'cursor': 'pointer',
             .click(clearAllTiers);
                'user-select': 'none',
       
                'display': 'flex',
         var $resetButton = $('<button>')
                'justify-content': 'space-between',
             .text('重置')
                'align-items': 'center'
             .addClass('tier-list-reset-btn')
            })
            .click(function() {
             .hover(
                 location.reload();
                function() { $(this).css('background', '#0b5394'); },
                function() { $(this).css('background', '#0645ad'); }
             );
 
         var $title = $('<h3>')
             .text('分级表工具')
             .css({
                'margin': '0',
                'font-size': '16px',
                 'flex': '1'
             });
             });
 
       
         var $toggleIcon = $('<span>')
         $buttonContainer.append($saveButton, ' ', $clearButton, ' ', $resetButton);
            .attr('id', 'tierlist-toggle-icon')
        $('.wikitable').before($buttonContainer);
            .text('')
    }
            .css({
   
                'font-size': '12px',
    function addCustomStyles() {
                'transition': 'transform 0.3s ease'
         var styles = `
            });
            <style>
 
                .tier-list-save-btn, .tier-list-clear-btn, .tier-list-reset-btn {
        $header.append($title, $toggleIcon);
                    padding: 8px 16px;
        $header.click(togglePanel);
                    margin: 0 5px;
 
                    font-size: 14px;
        // 内容区域
                    font-weight: bold;
         var $content = $('<div>')
                    border: none;
            .attr('id', 'tierlist-panel-content')
                    border-radius: 4px;
            .css({
                    cursor: pointer;
                'padding': '15px'
                    transition: all 0.3s;
            });
                }
 
               
        var $exportBtn = $('<button>')
                 .tier-list-save-btn {
            .text('📸 导出为PNG')
                    background: #4CAF50;
            .css({
                    color: white;
                'width': '100%',
                }
                'padding': '10px',
               
                'margin': '5px 0',
                .tier-list-save-btn:hover {
                'background': '#0645ad',
                    background: #45a049;
                'color': '#fff',
                 }
                'border': 'none',
                  
                'border-radius': '4px',
                 .tier-list-clear-btn {
                'cursor': 'pointer',
                    background: #f44336;
                'font-size': '14px',
                    color: white;
                'font-weight': 'bold',
                }
                'transition': 'background 0.2s'
                  
            })
                 .tier-list-clear-btn:hover {
            .hover(
                    background: #da190b;
                 function() { $(this).css('background', '#0b5394'); },
                 }
                function() { $(this).css('background', '#0645ad'); }
                  
            )
                 .tier-list-reset-btn {
            .click(exportToPNG);
                    background: #2196F3;
 
                    color: white;
        var $clearBtn = $('<button>')
                }
            .text('🗑️ 清空表格')
               
            .css({
                .tier-list-reset-btn:hover {
                'width': '100%',
                    background: #0b7dda;
                 'padding': '10px',
                 }
                 'margin': '5px 0',
               
                 'background': '#d33',
                .wikitable td {
                'color': '#fff',
                    min-height: 120px;
                 'border': 'none',
                    min-width: 300px;
                 'border-radius': '4px',
                    padding: 10px;
                 'cursor': 'pointer',
                    vertical-align: top;
                 'font-size': '14px',
                    position: relative;
                 'font-weight': 'bold',
                 }
                'transition': 'background 0.2s'
                  
            })
                 .tier-row-drop-zone {
            .hover(
                    border: 2px dashed transparent;
                function() { $(this).css('background', '#a00'); },
                    transition: all 0.3s;
                 function() { $(this).css('background', '#d33'); }
                }
            )
               
            .click(clearTable);
                 .tier-row-drop-zone.drag-over {
 
                    border-color: #2196F3;
        var $toggleBtn = $('<button>')
                    background-color: rgba(33, 150, 243, 0.1);
            .text('👁️ 切换预览模式')
                }
            .attr('id', 'toggle-edit-mode')
               
            .css({
                .avatar-frame {
                'width': '100%',
                    cursor: move;
                'padding': '10px',
                    transition: transform 0.2s, opacity 0.2s;
                'margin': '5px 0',
                    user-select: none;
                'background': '#36c',
                 }
                 'color': '#fff',
                  
                 'border': 'none',
                 .avatar-frame:hover {
                 'border-radius': '4px',
                    transform: scale(1.05);
                'cursor': 'pointer',
                }
                'font-size': '14px',
               
                'font-weight': 'bold',
                .avatar-frame.dragging {
                'transition': 'background 0.2s'
                    opacity: 0.5;
            })
                 }
            .hover(
                  
                 function() { $(this).css('background', '#258'); },
                 .avatar-frame.in-tier {
                function() { $(this).css('background', '#36c'); }
                    margin: 5px;
            )
                 }
            .click(toggleEditMode);
                  
 
                 #avatar-pool {
        var $info = $('<div>')
                    border: 2px solid #ddd;
            .css({
                    padding: 10px;
                'margin-top': '10px',
                    margin: 10px 0;
                'padding': '10px',
                    border-radius: 5px;
                'background': '#f8f9fa',
                    background: #fafafa;
                'border-radius': '4px',
                    min-height: 150px;
                 'font-size': '12px',
                 }
                 'color': '#666',
               
                 'line-height': '1.6'
                 #avatar-pool-title {
            })
                     font-size: 16px;
            .html('<strong>使用说明:</strong><br>1. 拖动卡片到表格<br>2. 右键点击可删除<br>3. 点击标题栏折叠<br>4. 导出前切换预览');
                    font-weight: bold;
 
                     margin-bottom: 10px;
        $content.append($exportBtn, $clearBtn, $toggleBtn, $info);
                     color: #333;
        $panel.append($header, $content);
        $('body').append($panel);
 
        // 添加最小化按钮(在面板外)
        var $minimizeBtn = $('<button>')
            .attr('id', 'tierlist-minimize-btn')
            .text('📋')
            .css({
                'position': 'fixed',
                 'top': '100px',
                 'right': '20px',
                 'z-index': '9998',
                'width': '50px',
                 'height': '50px',
                 'background': '#0645ad',
                 'color': '#fff',
                'border': '2px solid #0645ad',
                'border-radius': '50%',
                'cursor': 'pointer',
                'font-size': '24px',
                'display': 'none',
                'box-shadow': '0 4px 6px rgba(0,0,0,0.1)',
                 'transition': 'all 0.2s'
            })
            .hover(
                 function() {  
                     $(this).css({
                        'background': '#0b5394',
                        'transform': 'scale(1.1)'
                     });  
                },
                function() {
                     $(this).css({
                        'background': '#0645ad',
                        'transform': 'scale(1)'
                    });  
                 }
                 }
             )
             </style>
            .click(function() {
        `;
                showPanel();
         $('head').append(styles);
            });
 
         $('body').append($minimizeBtn);
     }
     }
 
   
     function togglePanel() {
     function initDragAndDrop() {
         var $content = $('#tierlist-panel-content');
        // 创建头像池容器
         var $icon = $('#tierlist-toggle-icon');
         var $avatarPool = $('<div>')
         var $panel = $('#tierlist-control-panel');
            .attr('id', 'avatar-pool')
            .addClass('tier-row-drop-zone');
       
         var $poolTitle = $('<div>')
            .attr('id', 'avatar-pool-title')
            .text('角色池(拖动到上方表格)');
          
        $avatarPool.append($poolTitle);
          
          
         if ($content.is(':visible')) {
         // 将所有头像移动到头像池
            // 折叠
        var $avatars = $('.avatar-frame');
            $content.slideUp(300);
         $avatars.each(function() {
            $icon.css('transform', 'rotate(-90deg)');
             $(this).addClass('avatar-item').appendTo($avatarPool);
            localStorage.setItem('tierlist-panel-collapsed', 'true');
         });
         } else {
            // 展开
            $content.slideDown(300);
             $icon.css('transform', 'rotate(0deg)');
            localStorage.setItem('tierlist-panel-collapsed', 'false');
        }
    }
 
    function hidePanel() {
        var $panel = $('#tierlist-control-panel');
         var $minimizeBtn = $('#tierlist-minimize-btn');
          
          
         $panel.fadeOut(300);
         $('.wikitable').after($avatarPool);
        $minimizeBtn.fadeIn(300);
        localStorage.setItem('tierlist-panel-hidden', 'true');
    }
 
    function showPanel() {
        var $panel = $('#tierlist-control-panel');
        var $minimizeBtn = $('#tierlist-minimize-btn');
          
          
         $minimizeBtn.fadeOut(300);
         // 为表格单元格添加drop-zone类
         $panel.fadeIn(300);
         $('.wikitable td').addClass('tier-row-drop-zone');
        localStorage.setItem('tierlist-panel-hidden', 'false');
    }
 
    function restorePanelState() {
        var isCollapsed = localStorage.getItem('tierlist-panel-collapsed') === 'true';
        var isHidden = localStorage.getItem('tierlist-panel-hidden') === 'true';
          
          
         if (isCollapsed) {
         // 初始化所有头像的拖拽
            $('#tierlist-panel-content').hide();
        initAvatarDrag();
            $('#tierlist-toggle-icon').css('transform', 'rotate(-90deg)');
        }
          
          
         if (isHidden) {
         // 初始化所有drop zone
            $('#tierlist-control-panel').hide();
        initDropZones();
            $('#tierlist-minimize-btn').show();
        }
     }
     }
 
   
     function makeCardsDraggable() {
     function initAvatarDrag() {
         $('.战斗员卡片').each(function() {
         $('.avatar-frame').each(function() {
             var $card = $(this);
             var $avatar = $(this);
              
              
             // 如果已经是可拖动的,跳过
             $avatar.attr('draggable', 'true');
            if ($card.attr('data-draggable-init')) {
                return;
            }
              
              
             $card.attr('draggable', 'true')
             $avatar.on('dragstart', function(e) {
                .attr('data-draggable-init', 'true')
                $(this).addClass('dragging');
                .css('cursor', 'move')
                e.originalEvent.dataTransfer.effectAllowed = 'move';
                .on('dragstart', function(e) {
                e.originalEvent.dataTransfer.setData('text/html', this.outerHTML);
                    // 克隆卡片HTML,确保包含所有结构
            });
                    var $clone = $card.clone();
           
                    $clone.find('*').each(function() {
            $avatar.on('dragend', function(e) {
                        // 移除可能导致样式问题的内联样式
                $(this).removeClass('dragging');
                        var $el = $(this);
            });
                        var style = $el.attr('style');
                        if (style) {
                            // 保留关键样式,移除 opacity 等
                            style = style.replace(/opacity\s*:\s*[^;]+;?/gi, '');
                            $el.attr('style', style);
                        }
                    });
                   
                    var cardHTML = $clone[0].outerHTML;
                    e.originalEvent.dataTransfer.setData('text/html', cardHTML);
                    e.originalEvent.dataTransfer.effectAllowed = 'copy';
                   
                    // 只让原始卡片变透明,不影响拖动的副本
                    $card.css('opacity', '0.5');
                })
                .on('dragend', function() {
                    $card.css('opacity', '1');
                });
         });
         });
     }
     }
 
   
     function makeTableDroppable() {
     function initDropZones() {
         $('.wikitable td').each(function() {
         $('.tier-row-drop-zone').each(function() {
             var $cell = $(this);
             var $zone = $(this);
           
            $zone.on('dragover', function(e) {
                e.preventDefault();
                e.originalEvent.dataTransfer.dropEffect = 'move';
                $(this).addClass('drag-over');
            });
           
            $zone.on('dragleave', function(e) {
                $(this).removeClass('drag-over');
            });
              
              
             $cell.addClass('tierlist-dropzone')
             $zone.on('drop', function(e) {
                .css({
                e.preventDefault();
                    'min-height': '280px',
                $(this).removeClass('drag-over');
                    'vertical-align': 'top',
                  
                    'padding': '10px',
                 var $dragging = $('.avatar-frame.dragging');
                    'position': 'relative'
                  
                })
                 if ($dragging.length) {
                .on('dragover', function(e) {
                     // 移动现有元素
                    e.preventDefault();
                     $dragging.removeClass('dragging');
                    e.originalEvent.dataTransfer.dropEffect = 'copy';
                    $cell.addClass('drag-over');
                 })
                 .on('dragleave', function() {
                    $cell.removeClass('drag-over');
                 })
                 .on('drop', function(e) {
                     e.preventDefault();
                     $cell.removeClass('drag-over');
                      
                      
                     var cardHTML = e.originalEvent.dataTransfer.getData('text/html');
                     // 如果是在tier中,添加样式
                     if (cardHTML) {
                     if ($(this).hasClass('wikitable')) {
                        var $newCard = $(cardHTML);
                        $dragging.addClass('in-tier');
                       
                    } else if ($(this).attr('id') === 'avatar-pool') {
                        // 确保新卡片的样式正确
                        $dragging.removeClass('in-tier');
                        $newCard.addClass('placed-card')
                    } else {
                            .css({
                         $dragging.addClass('in-tier');
                                'margin': '5px',
                                'opacity': '1',  // 明确设置为不透明
                                'filter': 'none' // 移除任何滤镜
                            })
                            .attr('draggable', 'true')
                            .removeAttr('data-draggable-init') // 允许重新初始化
                            .on('contextmenu', function(e) {
                                e.preventDefault();
                                if (confirm('确定要删除这张卡片吗?')) {
                                    $newCard.remove();
                                }
                            })
                            .on('dblclick', function() {
                                if (confirm('确定要删除这张卡片吗?')) {
                                    $newCard.remove();
                                }
                            });
                       
                        // 确保内部元素也没有 opacity 问题
                         $newCard.find('*').css('opacity', '');
                       
                        $cell.append($newCard);
                       
                        // 重新使新卡片可拖动
                        makeCardsDraggable();
                     }
                     }
                 });
                   
                    $(this).append($dragging);
                 }
            });
         });
         });
     }
     }
 
   
     function toggleEditMode() {
     function clearAllTiers() {
         $('body').toggleClass('tierlist-preview-mode');
         if (!confirm('确定要清空所有分级吗?')) {
        var isPreview = $('body').hasClass('tierlist-preview-mode');
            return;
         $('#toggle-edit-mode').text(isPreview ? '✏️ 切换编辑模式' : '👁️ 切换预览模式');
         }
          
          
         if (isPreview) {
         // 将所有tier中的头像移回头像池
             $('.placed-card').css('pointer-events', 'none');
        $('.wikitable td .avatar-frame').each(function() {
             $(this).removeClass('in-tier');
            $('#avatar-pool').append($(this));
        });
    }
   
    function saveTierListAsPNG() {
        // 检查是否已加载html2canvas库
        if (typeof html2canvas === 'undefined') {
            loadHtml2Canvas(function() {
                captureTierList();
            });
         } else {
         } else {
             $('.placed-card').css('pointer-events', 'auto');
             captureTierList();
         }
         }
     }
     }
 
   
     function clearTable() {
     function loadHtml2Canvas(callback) {
         if (confirm('确定要清空表格中的所有卡片吗?此操作不可撤销!')) {
         mw.loader.using('jquery', function() {
            $('.wikitable .placed-card').remove();
            mw.notify('表格已清空', { type: 'success' });
        }
    }
 
    function exportToPNG() {
        var $btn = $(event.target);
        $btn.prop('disabled', true).text('⏳ 正在生成...');
 
        // 加载 html2canvas 库
        if (typeof html2canvas === 'undefined') {
             $.getScript('https://cdn.jsdelivr.net/npm/html2canvas@1.4.1/dist/html2canvas.min.js')
             $.getScript('https://cdn.jsdelivr.net/npm/html2canvas@1.4.1/dist/html2canvas.min.js')
                 .done(function() {
                 .done(function() {
                     performExport($btn);
                     callback();
                 })
                 })
                 .fail(function() {
                 .fail(function() {
                     mw.notify('加载导出库失败,请检查网络连接', { type: 'error' });
                     alert('加载截图库失败,请检查网络连接');
                    $btn.prop('disabled', false).text('📸 导出为PNG');
                 });
                 });
         } else {
         });
            performExport($btn);
        }
     }
     }
 
   
     function performExport($btn) {
     function captureTierList() {
         var $table = $('.wikitable').first();
         var $table = $('.wikitable');
          
          
         // 临时隐藏控制面板和最小化按钮
         if ($table.length === 0) {
        var $panel = $('#tierlist-control-panel');
            alert('未找到表格');
        var $minimizeBtn = $('#tierlist-minimize-btn');
            return;
        var panelVisible = $panel.is(':visible');
         }
         var btnVisible = $minimizeBtn.is(':visible');
          
          
         $panel.hide();
         // 显示加载提示
        $minimizeBtn.hide();
        var $loading = $('<div>')
            .css({
                'position': 'fixed',
                'top': '50%',
                'left': '50%',
                'transform': 'translate(-50%, -50%)',
                'background': 'rgba(0,0,0,0.8)',
                'color': 'white',
                'padding': '20px 40px',
                'border-radius': '10px',
                'z-index': 9999,
                'font-size': '18px'
            })
            .text('正在生成图片...')
            .appendTo('body');
          
          
        // 进入预览模式
        var wasPreview = $('body').hasClass('tierlist-preview-mode');
        if (!wasPreview) {
            $('body').addClass('tierlist-preview-mode');
        }
        // 确保所有卡片都是完全不透明的
        var $cards = $('.placed-card');
        $cards.css({
            'opacity': '1',
            'filter': 'none'
        });
         html2canvas($table[0], {
         html2canvas($table[0], {
             backgroundColor: '#ffffff',
             backgroundColor: '#ffffff',
第434行: 第305行:
             logging: false,
             logging: false,
             useCORS: true,
             useCORS: true,
             allowTaint: true,
             allowTaint: true
            imageTimeout: 0,
            removeContainer: true
         }).then(function(canvas) {
         }).then(function(canvas) {
             // 恢复界面
             $loading.remove();
            if (panelVisible) $panel.show();
            if (btnVisible) $minimizeBtn.show();
            if (!wasPreview) {
                $('body').removeClass('tierlist-preview-mode');
            }
              
              
             // 下载图片
             // 转换为blob并下载
             var link = document.createElement('a');
             canvas.toBlob(function(blob) {
            var timestamp = new Date().toISOString().slice(0, 19).replace(/:/g, '-');
                var url = URL.createObjectURL(blob);
            link.download = '战斗员分级表_' + timestamp + '.png';
                var link = document.createElement('a');
            link.href = canvas.toDataURL('image/png');
                var timestamp = new Date().toISOString().slice(0,19).replace(/:/g,'-');
            link.click();
                link.download = 'tier-list-' + timestamp + '.png';
           
                link.href = url;
            $btn.prop('disabled', false).text('📸 导出为PNG');
                link.click();
              
                URL.revokeObjectURL(url);
            mw.notify('分级表已成功导出!', { type: 'success' });
             });
         }).catch(function(error) {
         }).catch(function(error) {
             console.error('导出失败:', error);
             $loading.remove();
            if (panelVisible) $panel.show();
             console.error('截图失败:', error);
             if (btnVisible) $minimizeBtn.show();
             alert('生成图片失败,请重试');
            $btn.prop('disabled', false).text('📸 导出为PNG');
             mw.notify('导出失败: ' + error.message, { type: 'error' });
         });
         });
     }
     }
 
      
     function addStyles() {
        var styles = `
            .tierlist-dropzone {
                transition: background-color 0.3s, border 0.3s;
            }
           
            .tierlist-dropzone.drag-over {
                background-color: #e6f3ff !important;
                border: 2px dashed #0645ad !important;
            }
           
            .placed-card {
                transition: transform 0.2s, box-shadow 0.2s;
                opacity: 1 !important;
                filter: none !important;
            }
           
            .placed-card * {
                opacity: 1 !important;
                filter: none !important;
            }
           
            body:not(.tierlist-preview-mode) .placed-card:hover {
                transform: scale(1.05);
                box-shadow: 0 4px 8px rgba(0,0,0,0.2);
                z-index: 10;
            }
           
            .tierlist-preview-mode .placed-card {
                pointer-events: none;
            }
           
            .tierlist-preview-mode .placed-card:hover {
                transform: none;
                box-shadow: none;
            }
           
            #tierlist-control-panel button:active {
                transform: scale(0.95);
            }
           
            #tierlist-panel-header {
                position: relative;
            }
           
            /* 添加一个关闭按钮到标题栏 */
            #tierlist-panel-header::after {
                content: '✕';
                position: absolute;
                right: 40px;
                top: 50%;
                transform: translateY(-50%);
                cursor: pointer;
                font-size: 18px;
                opacity: 0.8;
                transition: opacity 0.2s;
            }
           
            #tierlist-panel-header:hover::after {
                opacity: 1;
            }
           
            @media print {
                #tierlist-control-panel,
                #tierlist-minimize-btn {
                    display: none !important;
                }
            }
        `;
       
        $('<style>').text(styles).appendTo('head');
    }
 
    // 为标题栏添加关闭功能
    $(document).on('click', '#tierlist-panel-header', function(e) {
        var $header = $(this);
        var clickX = e.pageX - $header.offset().left;
        var headerWidth = $header.outerWidth();
       
        // 如果点击的是右侧的关闭区域(最右边40px)
        if (clickX > headerWidth - 40 && clickX < headerWidth - 10) {
            e.stopPropagation();
            hidePanel();
        }
    });
 
})();
})();

2025年10月6日 (一) 19:39的最新版本

// MediaWiki:Gadget-TierListMaker.js

(function() {
    'use strict';
    
    // 仅在 TierListMaker 页面运行
    if (mw.config.get('wgPageName') !== '节奏榜') {
        return;
    }
    
    // 等待页面加载完成
    $(function() {
        initTierListMaker();
    });
    
    function initTierListMaker() {
        // 添加控制按钮
        addControlButtons();
        
        // 初始化拖拽功能
        initDragAndDrop();
        
        // 添加样式
        addCustomStyles();
    }
    
    function addControlButtons() {
        var $buttonContainer = $('<div>')
            .attr('id', 'tier-list-controls')
            .css({
                'margin': '10px 0',
                'padding': '10px',
                'background': '#f0f0f0',
                'border-radius': '5px'
            });
        
        var $saveButton = $('<button>')
            .text('保存为PNG')
            .addClass('tier-list-save-btn')
            .click(saveTierListAsPNG);
        
        var $clearButton = $('<button>')
            .text('清空所有')
            .addClass('tier-list-clear-btn')
            .click(clearAllTiers);
        
        var $resetButton = $('<button>')
            .text('重置')
            .addClass('tier-list-reset-btn')
            .click(function() {
                location.reload();
            });
        
        $buttonContainer.append($saveButton, ' ', $clearButton, ' ', $resetButton);
        $('.wikitable').before($buttonContainer);
    }
    
    function addCustomStyles() {
        var styles = `
            <style>
                .tier-list-save-btn, .tier-list-clear-btn, .tier-list-reset-btn {
                    padding: 8px 16px;
                    margin: 0 5px;
                    font-size: 14px;
                    font-weight: bold;
                    border: none;
                    border-radius: 4px;
                    cursor: pointer;
                    transition: all 0.3s;
                }
                
                .tier-list-save-btn {
                    background: #4CAF50;
                    color: white;
                }
                
                .tier-list-save-btn:hover {
                    background: #45a049;
                }
                
                .tier-list-clear-btn {
                    background: #f44336;
                    color: white;
                }
                
                .tier-list-clear-btn:hover {
                    background: #da190b;
                }
                
                .tier-list-reset-btn {
                    background: #2196F3;
                    color: white;
                }
                
                .tier-list-reset-btn:hover {
                    background: #0b7dda;
                }
                
                .wikitable td {
                    min-height: 120px;
                    min-width: 300px;
                    padding: 10px;
                    vertical-align: top;
                    position: relative;
                }
                
                .tier-row-drop-zone {
                    border: 2px dashed transparent;
                    transition: all 0.3s;
                }
                
                .tier-row-drop-zone.drag-over {
                    border-color: #2196F3;
                    background-color: rgba(33, 150, 243, 0.1);
                }
                
                .avatar-frame {
                    cursor: move;
                    transition: transform 0.2s, opacity 0.2s;
                    user-select: none;
                }
                
                .avatar-frame:hover {
                    transform: scale(1.05);
                }
                
                .avatar-frame.dragging {
                    opacity: 0.5;
                }
                
                .avatar-frame.in-tier {
                    margin: 5px;
                }
                
                #avatar-pool {
                    border: 2px solid #ddd;
                    padding: 10px;
                    margin: 10px 0;
                    border-radius: 5px;
                    background: #fafafa;
                    min-height: 150px;
                }
                
                #avatar-pool-title {
                    font-size: 16px;
                    font-weight: bold;
                    margin-bottom: 10px;
                    color: #333;
                }
            </style>
        `;
        $('head').append(styles);
    }
    
    function initDragAndDrop() {
        // 创建头像池容器
        var $avatarPool = $('<div>')
            .attr('id', 'avatar-pool')
            .addClass('tier-row-drop-zone');
        
        var $poolTitle = $('<div>')
            .attr('id', 'avatar-pool-title')
            .text('角色池(拖动到上方表格)');
        
        $avatarPool.append($poolTitle);
        
        // 将所有头像移动到头像池
        var $avatars = $('.avatar-frame');
        $avatars.each(function() {
            $(this).addClass('avatar-item').appendTo($avatarPool);
        });
        
        $('.wikitable').after($avatarPool);
        
        // 为表格单元格添加drop-zone类
        $('.wikitable td').addClass('tier-row-drop-zone');
        
        // 初始化所有头像的拖拽
        initAvatarDrag();
        
        // 初始化所有drop zone
        initDropZones();
    }
    
    function initAvatarDrag() {
        $('.avatar-frame').each(function() {
            var $avatar = $(this);
            
            $avatar.attr('draggable', 'true');
            
            $avatar.on('dragstart', function(e) {
                $(this).addClass('dragging');
                e.originalEvent.dataTransfer.effectAllowed = 'move';
                e.originalEvent.dataTransfer.setData('text/html', this.outerHTML);
            });
            
            $avatar.on('dragend', function(e) {
                $(this).removeClass('dragging');
            });
        });
    }
    
    function initDropZones() {
        $('.tier-row-drop-zone').each(function() {
            var $zone = $(this);
            
            $zone.on('dragover', function(e) {
                e.preventDefault();
                e.originalEvent.dataTransfer.dropEffect = 'move';
                $(this).addClass('drag-over');
            });
            
            $zone.on('dragleave', function(e) {
                $(this).removeClass('drag-over');
            });
            
            $zone.on('drop', function(e) {
                e.preventDefault();
                $(this).removeClass('drag-over');
                
                var $dragging = $('.avatar-frame.dragging');
                
                if ($dragging.length) {
                    // 移动现有元素
                    $dragging.removeClass('dragging');
                    
                    // 如果是在tier中,添加样式
                    if ($(this).hasClass('wikitable')) {
                        $dragging.addClass('in-tier');
                    } else if ($(this).attr('id') === 'avatar-pool') {
                        $dragging.removeClass('in-tier');
                    } else {
                        $dragging.addClass('in-tier');
                    }
                    
                    $(this).append($dragging);
                }
            });
        });
    }
    
    function clearAllTiers() {
        if (!confirm('确定要清空所有分级吗?')) {
            return;
        }
        
        // 将所有tier中的头像移回头像池
        $('.wikitable td .avatar-frame').each(function() {
            $(this).removeClass('in-tier');
            $('#avatar-pool').append($(this));
        });
    }
    
    function saveTierListAsPNG() {
        // 检查是否已加载html2canvas库
        if (typeof html2canvas === 'undefined') {
            loadHtml2Canvas(function() {
                captureTierList();
            });
        } else {
            captureTierList();
        }
    }
    
    function loadHtml2Canvas(callback) {
        mw.loader.using('jquery', function() {
            $.getScript('https://cdn.jsdelivr.net/npm/html2canvas@1.4.1/dist/html2canvas.min.js')
                .done(function() {
                    callback();
                })
                .fail(function() {
                    alert('加载截图库失败,请检查网络连接');
                });
        });
    }
    
    function captureTierList() {
        var $table = $('.wikitable');
        
        if ($table.length === 0) {
            alert('未找到表格');
            return;
        }
        
        // 显示加载提示
        var $loading = $('<div>')
            .css({
                'position': 'fixed',
                'top': '50%',
                'left': '50%',
                'transform': 'translate(-50%, -50%)',
                'background': 'rgba(0,0,0,0.8)',
                'color': 'white',
                'padding': '20px 40px',
                'border-radius': '10px',
                'z-index': 9999,
                'font-size': '18px'
            })
            .text('正在生成图片...')
            .appendTo('body');
        
        html2canvas($table[0], {
            backgroundColor: '#ffffff',
            scale: 2,
            logging: false,
            useCORS: true,
            allowTaint: true
        }).then(function(canvas) {
            $loading.remove();
            
            // 转换为blob并下载
            canvas.toBlob(function(blob) {
                var url = URL.createObjectURL(blob);
                var link = document.createElement('a');
                var timestamp = new Date().toISOString().slice(0,19).replace(/:/g,'-');
                link.download = 'tier-list-' + timestamp + '.png';
                link.href = url;
                link.click();
                URL.revokeObjectURL(url);
            });
        }).catch(function(error) {
            $loading.remove();
            console.error('截图失败:', error);
            alert('生成图片失败,请重试');
        });
    }
    
})();