跳转到内容

User:Kurgenera/smallTW.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
// 【V4.4 多模板添加器】 - 时间戳最终修正 (全部模板都添加 time 参数)

function getISOTimestamp() {
    var now = new Date();
    return now.toISOString().replace(/\.\d{3}Z$/, 'Z');
}

$(function() {
    // 1. 配置:常用维护模板列表
    const MAINT_TEMPLATES = [
        { 
            name: '🚨 机器人/AWB滥建', 
            // 关键:将模板分解为数组,方便后续单独添加时间戳
            template: ['{{No footnotes}}', '{{Onesource}}'], 
            summary: '[[Template:No footnotes]] + [[Template:Onesource]]',
            is_combo: true 
        },
        // -------------------------------------------------------------------
        { name: '需要补充来源', template: ['{{Refimprove}}'], summary: '[[Template:Refimprove]]' },
        { name: '单一来源', template: ['{{Onesource}}'], summary: '[[Template:Onesource]]' },
        { name: '没有来源', template: ['{{Unreferenced}}'], summary: '[[Template:Unreferenced]]' },
        { name: '原创研究', template: ['{{Original research}}'], summary: '[[Template:Original research]]' },
        { name: '翻译腔', template: ['{{Translation}}'], summary: '[[Template:Translation]]' },
        { name: '年表', template: ['{{Prose}}'], summary: '[[Template:Prose]]' },
        { name: '引用清理', template: ['{{Citation style}}'], summary: '[[Template:Citation style]]' },
        { name: '脚注', template: ['{{no footnotes}}'], summary: '[[Template:No footnotes]]' }
    ];
    
    // 2. 注入必要的 CSS 样式(保持不变)
    var modalCSS = 
        '#maint-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.7); z-index: 99999; }' +
        '#maint-modal { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); background: #fff; padding: 25px; border: 1px solid #ccc; border-radius: 5px; z-index: 100000; width: 400px; max-width: 90%; box-shadow: 0 0 15px rgba(0,0,0,0.5); }' +
        '#maint-modal h2 { margin-top: 0; border-bottom: 1px solid #ddd; padding-bottom: 10px; }' +
        '#maint-modal label { display: block; margin-bottom: 8px; cursor: pointer; }' +
        '#maint-modal button { margin-top: 15px; padding: 10px 15px; background-color: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; }' +
        '#maint-modal .maint-close { float: right; cursor: pointer; font-size: 1.5em; color: #aaa; }';
    
    mw.util.addCSS(modalCSS);
    
    // 3. 添加用户界面按钮 (保持不变)
    mw.util.addPortletLink(
        'p-cactions',                                    
        '#',                                             
        '批量维护模板',                                  
        'ca-quick-maint',                                
        '快速批量添加维护模板(我阐述你的梦!我讨厌条目精!——作者)', 
        null,                                            
        '#ca-viewsource'
    );
    
    // 4. 绑定点击事件:打开模态窗口
    $('#ca-quick-maint a').on('click', function(e) {
        e.preventDefault();
        
        if ($('#maint-overlay').length) {
            return;
        }

        var $overlay = $('<div>').attr('id', 'maint-overlay');
        var $modal = $('<div>').attr('id', 'maint-modal');
        $modal.html('<h2>添加维护模板<span class="maint-close">&times;</span></h2>');
        
        var $form = $('<form>').attr('id', 'maint-form');
        
        // 修正:在显示时将模板数组组合成字符串,并添加 index 属性
        MAINT_TEMPLATES.forEach(function(item, index) {
            const displayTemplate = item.template.join(' / ');
            $form.append(
                $('<label>').append(
                    $('<input>').attr({
                        type: 'checkbox',
                        name: 'template',
                        value: item.template.join(' '), // 值为数组的空格连接
                        'data-summary': item.summary,
                        'data-index': index 
                    }),
                    ' ' + item.name + ' (' + displayTemplate + ')'
                )
            );
        });
        
        $form.append($('<button>').attr('type', 'submit').text('确认添加并提交'));
        $modal.append($form);
        
        $('body').append($overlay, $modal);
        
        // 绑定关闭事件 (保持不变)
        $('.maint-close, #maint-overlay').on('click', function(e) {
            if (e.target.id === 'maint-overlay' || $(e.target).hasClass('maint-close')) {
                $overlay.remove();
                $modal.remove();
            }
        });
        
        // 4.5. 绑定提交事件
        $('#maint-form').on('submit', function(submitEvent) {
            submitEvent.preventDefault();
            
            var allTemplatesToPrepend = []; // 最终要添加到页面顶部的所有模板列表
            var summaries = [];
            
            // 收集选中的模板和摘要
            $('#maint-form input:checked').each(function() {
                var $input = $(this);
                const index = $input.data('index');
                const item = MAINT_TEMPLATES[index];

                // 无论是单一模板还是组合模板,都将其 template 数组中的每个子模板加入
                // 例如:如果是组合模板,将 ['{{No footnotes}}', '{{Onesource}}'] 的两个元素分别加入
                allTemplatesToPrepend.push(...item.template);
                summaries.push(item.summary);
            });
            
            if (allTemplatesToPrepend.length === 0) {
                alert('请至少选择一个模板。');
                return;
            }
            
            var dynamicTime = getISOTimestamp();
            
            // **核心修正逻辑:对列表中的每个模板都添加时间戳**
            var templatesWithTime = allTemplatesToPrepend.map(function(tpl) {
                // 将 time 参数添加到模板的闭合 }} 之前
                return tpl.replace(/\}\}$/, '|time=' + dynamicTime + '}}');
            });
            
            // 组合最终要提交的内容和摘要
            // 使用双换行符来分隔不同的模板
            var prependText = templatesWithTime.join('\n\n') + '\n';
            var combinedSummary = '添加维护模板: ' + summaries.join('、');

            // 执行 API 编辑
            new mw.Api().postWithToken('csrf', {
                action: 'edit',
                title: mw.config.get('wgPageName'),
                prependtext: prependText,
                summary: combinedSummary + ' (使用[[User:Kurgenera/smallTW.js]])',
                nocreate: true
            }).then(function() {
                alert('维护模板添加成功!页面即将刷新。');
                $overlay.remove();
                $modal.remove();
                window.location.reload(); 
            }, function(error) {
                alert('编辑失败,请检查控制台获取更多信息: ' + error.error.code);
            });
        });
    });
});