MediaWiki:Gadget-PrintOptions.js:修订间差异

勤求古训,博采众方
跳转到导航 跳转到搜索
删除的内容 添加的内容
小无编辑摘要
标签:手工回退
小无编辑摘要
标签:已被回退
第35行: 第35行:
PrintDialog.static.title = '打印此页面';
PrintDialog.static.title = '打印此页面';
PrintDialog.static.actions = [
PrintDialog.static.actions = [
{ action: 'print', label: '打印', flags: ['primary', 'progressive'] },
{
{ action: 'cancel', label: '取消', flags: 'safe' }
action: 'print',
label: '打印',
flags: ['primary', 'progressive'],
icon: 'printer'
},
{
action: 'cancel',
label: '取消',
flags: 'safe',
icon: 'close'
}
];
];
第63行: 第73行:
};
};
// ✅ 修复点:明确处理取消动作
PrintDialog.prototype.getActionProcess = function (action) {
PrintDialog.prototype.getActionProcess = function (action) {
if (action === 'print') {
if (action === 'print') {
return new OO.ui.Process(function () {
return new OO.ui.Process(function () {
// 保存选项设置
printOptions.questions.forEach(function (question) {
printOptions.questions.forEach(function (question) {
if (question.widget) {
if (question.widget) {
第72行: 第82行:
}
}
});
});
return this.close({ action: action }).then(function () {
return this.close({ action: action }).then(function () {
printOptions.changePrintCSS();
printOptions.changePrintCSS();
第79行: 第90行:
}.bind(this));
}.bind(this));
}
}
// 🔥 关键修复:取消时直接关闭对话框
if (action === 'cancel') {
if (action === 'cancel') {
return new OO.ui.Process(function () {
return new OO.ui.Process(function () {
第85行: 第96行:
}.bind(this));
}.bind(this));
}
}
return PrintDialog.super.prototype.getActionProcess.call(this, action);
return PrintDialog.super.prototype.getActionProcess.call(this, action);
};
};
第92行: 第104行:
$('body').append(windowManager.$element);
$('body').append(windowManager.$element);
}
}
if (!printDialog) {
// 每次都创建新的对话框实例,避免重复使用的问题
printDialog = new PrintDialog({ size: 'medium' });
windowManager.addWindows([printDialog]);
printDialog = new PrintDialog({ size: 'medium' });
}
windowManager.addWindows([printDialog]);
windowManager.openWindow(printDialog);
windowManager.openWindow(printDialog);
},
},
changePrintCSS: function () {
changePrintCSS: function () {
/* 原有CSS处理逻辑 */
var printStyle = '';
var printStyle = '';
if (this.noimages) printStyle += 'img, .thumb { display:none !important; }\n';
if (this.norefs) printStyle += '.references, .reference { display:none !important; }\n';
if (this.notoc) printStyle += '#toc, .toc { display:none !important; }\n';
if (this.nobackground) printStyle += 'body { background: white !important; }\n';
if (this.blacktext) printStyle += '* { color: black !important; }\n';
$('style[media="print"]').remove(); // 清理旧样式
if (this.noimages) {
printStyle += 'img, .thumb, .thumbinner, .thumbimage { display:none !important; }\n';
}
if (this.norefs) {
printStyle += '.references, .reference, .reflist, .ref-list { display:none !important; }\n';
}
if (this.notoc) {
printStyle += '#toc, .toc { display:none !important; }\n';
}
if (this.nobackground) {
printStyle += 'body { background: white !important; }\n';
}
if (this.blacktext) {
printStyle += '* { color: black !important; }\n';
}
// 清理旧样式
$('style[media="print"]').remove();
// 添加新样式
if (printStyle) {
if (printStyle) {
$('head').append('<style media="print">' + printStyle + '</style>');
$('<style>')
.attr('media', 'print')
.text(printStyle)
.appendTo('head');
}
}
},
},
第124行: 第153行:
questions: [
questions: [
{
label: '隐藏界面元素',
type: 'checkbox',
checked: true,
returnvalue: 'enhanced'
},
{
{
label: '隐藏图像',
label: '隐藏图像',
第149行: 第172行:
},
},
{
{
label: '删除背景(部分浏览器可能无效)',
label: '删除背景',
type: 'checkbox',
type: 'checkbox',
checked: false,
checked: false,
第161行: 第184行:
}
}
]
]
};
// 🌐 多语言支持扩展
var i18n = {
'title': mw.message('printdialog-title').plain() || '打印此页面',
'print-btn': mw.message('printdialog-print-btn').plain() || '打印',
'cancel-btn': mw.message('printdialog-cancel-btn').plain() || '取消',
'link-text': mw.message('printdialog-link-text').plain() || '可打印版本',
'options': {
'hideImages': mw.message('printdialog-opt-hide-images').plain() || '隐藏图像',
'hideRefs': mw.message('printdialog-opt-hide-refs').plain() || '隐藏引用',
'hideToc': mw.message('printdialog-opt-hide-toc').plain() || '隐藏目录',
'noBackground': mw.message('printdialog-opt-no-background').plain() || '删除背景',
'blackText': mw.message('printdialog-opt-black-text').plain() || '强制文本为黑色'
}
};
// 🔥 快捷键支持
function registerHotkeys() {
$(document).off('keydown.printdialog').on('keydown.printdialog', function (e) {
if (!printDialog || !windowManager) return;
// ESC - 取消
if (e.key === 'Escape') {
printDialog.executeAction('cancel');
e.preventDefault();
}
// Ctrl+Enter - 打印
if (e.ctrlKey && e.key === 'Enter') {
printDialog.executeAction('print');
e.preventDefault();
}
});
}
// 应用多语言文本
function applyI18n() {
// 更新对话框文本
if (printDialog && printDialog.constructor.static) {
printDialog.constructor.static.title = i18n.title;
printDialog.constructor.static.actions = [
{
action: 'print',
label: i18n['print-btn'],
flags: ['primary', 'progressive'],
icon: 'printer'
},
{
action: 'cancel',
label: i18n['cancel-btn'],
flags: 'safe',
icon: 'close'
}
];
}
// 更新选项标签
printOptions.questions = [
{
label: i18n.options.hideImages,
type: 'checkbox',
checked: false,
returnvalue: 'noimages'
},
{
label: i18n.options.hideRefs,
type: 'checkbox',
checked: false,
returnvalue: 'norefs'
},
{
label: i18n.options.hideToc,
type: 'checkbox',
checked: false,
returnvalue: 'notoc'
},
{
label: i18n.options.noBackground,
type: 'checkbox',
checked: false,
returnvalue: 'nobackground'
},
{
label: i18n.options.blackText,
type: 'checkbox',
checked: true,
returnvalue: 'blacktext'
}
];
// 更新链接文本
$('#t-print a').text(i18n['link-text']);
}
// 增强的安装函数
var originalInstall = printOptions.install;
printOptions.install = function() {
originalInstall.call(this);
applyI18n();
registerHotkeys();
};
};
第169行: 第293行:
}
}
});
});
// 暴露到全局以便调试
window.PrintDialog = printOptions;
})();
})();

2025年11月1日 (六) 22:32的版本

(function () {
    'use strict';
    
    var windowManager;
    var printDialog;
    
    var printOptions = {
        install: function () {
            var $printLink = $('#t-print a');
            if ($printLink.length === 0) return;
            
            $printLink
                .text('可打印版本')
                .off('click')
                .on('click', function (e) {
                    mw.loader.using(['oojs-ui-core', 'oojs-ui-widgets', 'oojs-ui-windows'])
                        .then(printOptions.createWindow)
                        .catch(function (err) {
                            mw.notify('打印功能加载失败: ' + err.message, { type: 'error' });
                        });
                    e.preventDefault();
                });
            
            // 预加载资源
            mw.loader.load(['oojs-ui-core', 'oojs-ui-widgets', 'oojs-ui-windows']);
        },
        
        createWindow: function () {
            function PrintDialog(config) {
                PrintDialog.super.call(this, config);
            }
            OO.inheritClass(PrintDialog, OO.ui.ProcessDialog);
            
            PrintDialog.static.name = 'printdialog';
            PrintDialog.static.title = '打印此页面';
            PrintDialog.static.actions = [
                { 
                    action: 'print', 
                    label: '打印', 
                    flags: ['primary', 'progressive'],
                    icon: 'printer'
                },
                { 
                    action: 'cancel', 
                    label: '取消', 
                    flags: 'safe',
                    icon: 'close'
                }
            ];
            
            PrintDialog.prototype.initialize = function () {
                PrintDialog.super.prototype.initialize.apply(this, arguments);
                this.panel = new OO.ui.PanelLayout({ padded: true, expanded: false });
                this.content = new OO.ui.FieldsetLayout();
                
                printOptions.questions.forEach(function (question) {
                    if (question.type === 'checkbox') {
                        var checkbox = new OO.ui.CheckboxInputWidget({
                            selected: question.checked
                        });
                        question.widget = checkbox;
                        this.content.addItems([
                            new OO.ui.FieldLayout(checkbox, {
                                label: question.label,
                                align: 'inline'
                            })
                        ]);
                    }
                }, this);
                
                this.panel.$element.append(this.content.$element);
                this.$body.append(this.panel.$element);
            };
            
            PrintDialog.prototype.getActionProcess = function (action) {
                if (action === 'print') {
                    return new OO.ui.Process(function () {
                        // 保存选项设置
                        printOptions.questions.forEach(function (question) {
                            if (question.widget) {
                                printOptions[question.returnvalue] = question.widget.isSelected();
                            }
                        });
                        
                        return this.close({ action: action }).then(function () {
                            printOptions.changePrintCSS();
                            printOptions.otherEnhancements();
                            setTimeout(window.print, 100);
                        });
                    }.bind(this));
                }
                
                if (action === 'cancel') {
                    return new OO.ui.Process(function () {
                        return this.close({ action: action });
                    }.bind(this));
                }
                
                return PrintDialog.super.prototype.getActionProcess.call(this, action);
            };
            
            if (!windowManager) {
                windowManager = new OO.ui.WindowManager();
                $('body').append(windowManager.$element);
            }
            
            // 每次都创建新的对话框实例,避免重复使用的问题
            printDialog = new PrintDialog({ size: 'medium' });
            windowManager.addWindows([printDialog]);
            
            windowManager.openWindow(printDialog);
        },
        
        changePrintCSS: function () {
            var printStyle = '';
            
            if (this.noimages) {
                printStyle += 'img, .thumb, .thumbinner, .thumbimage { display:none !important; }\n';
            }
            if (this.norefs) {
                printStyle += '.references, .reference, .reflist, .ref-list { display:none !important; }\n';
            }
            if (this.notoc) {
                printStyle += '#toc, .toc { display:none !important; }\n';
            }
            if (this.nobackground) {
                printStyle += 'body { background: white !important; }\n';
            }
            if (this.blacktext) {
                printStyle += '* { color: black !important; }\n';
            }
            
            // 清理旧样式
            $('style[media="print"]').remove();
            
            // 添加新样式
            if (printStyle) {
                $('<style>')
                    .attr('media', 'print')
                    .text(printStyle)
                    .appendTo('head');
            }
        },
        
        otherEnhancements: function () {
            var $link = $('div.printfooter a');
            try {
                $link.text(decodeURIComponent($link.text()));
            } catch (e) {
                mw.log.warn('URL解码失败:', e);
            }
        },
        
        questions: [
            {
                label: '隐藏图像',
                type: 'checkbox',
                checked: false,
                returnvalue: 'noimages'
            },
            {
                label: '隐藏引用',
                type: 'checkbox',
                checked: false,
                returnvalue: 'norefs'
            },
            {
                label: '隐藏目录',
                type: 'checkbox',
                checked: false,
                returnvalue: 'notoc'
            },
            {
                label: '删除背景',
                type: 'checkbox',
                checked: false,
                returnvalue: 'nobackground'
            },
            {
                label: '强制文本为黑色',
                type: 'checkbox',
                checked: true,
                returnvalue: 'blacktext'
            }
        ]
    };
    
    // 🌐 多语言支持扩展
    var i18n = {
        'title': mw.message('printdialog-title').plain() || '打印此页面',
        'print-btn': mw.message('printdialog-print-btn').plain() || '打印',
        'cancel-btn': mw.message('printdialog-cancel-btn').plain() || '取消',
        'link-text': mw.message('printdialog-link-text').plain() || '可打印版本',
        'options': {
            'hideImages': mw.message('printdialog-opt-hide-images').plain() || '隐藏图像',
            'hideRefs': mw.message('printdialog-opt-hide-refs').plain() || '隐藏引用',
            'hideToc': mw.message('printdialog-opt-hide-toc').plain() || '隐藏目录',
            'noBackground': mw.message('printdialog-opt-no-background').plain() || '删除背景',
            'blackText': mw.message('printdialog-opt-black-text').plain() || '强制文本为黑色'
        }
    };
    
    // 🔥 快捷键支持
    function registerHotkeys() {
        $(document).off('keydown.printdialog').on('keydown.printdialog', function (e) {
            if (!printDialog || !windowManager) return;
            
            // ESC - 取消
            if (e.key === 'Escape') {
                printDialog.executeAction('cancel');
                e.preventDefault();
            }
            
            // Ctrl+Enter - 打印
            if (e.ctrlKey && e.key === 'Enter') {
                printDialog.executeAction('print');
                e.preventDefault();
            }
        });
    }
    
    // 应用多语言文本
    function applyI18n() {
        // 更新对话框文本
        if (printDialog && printDialog.constructor.static) {
            printDialog.constructor.static.title = i18n.title;
            printDialog.constructor.static.actions = [
                { 
                    action: 'print', 
                    label: i18n['print-btn'], 
                    flags: ['primary', 'progressive'],
                    icon: 'printer'
                },
                { 
                    action: 'cancel', 
                    label: i18n['cancel-btn'], 
                    flags: 'safe',
                    icon: 'close'
                }
            ];
        }
        
        // 更新选项标签
        printOptions.questions = [
            {
                label: i18n.options.hideImages,
                type: 'checkbox',
                checked: false,
                returnvalue: 'noimages'
            },
            {
                label: i18n.options.hideRefs,
                type: 'checkbox',
                checked: false,
                returnvalue: 'norefs'
            },
            {
                label: i18n.options.hideToc,
                type: 'checkbox',
                checked: false,
                returnvalue: 'notoc'
            },
            {
                label: i18n.options.noBackground,
                type: 'checkbox',
                checked: false,
                returnvalue: 'nobackground'
            },
            {
                label: i18n.options.blackText,
                type: 'checkbox',
                checked: true,
                returnvalue: 'blacktext'
            }
        ];
        
        // 更新链接文本
        $('#t-print a').text(i18n['link-text']);
    }
    
    // 增强的安装函数
    var originalInstall = printOptions.install;
    printOptions.install = function() {
        originalInstall.call(this);
        applyI18n();
        registerHotkeys();
    };
    
    // 延迟初始化以避免冲突
    $(function () {
        if (mw.config.get('wgNamespaceNumber') >= 0) {
            setTimeout(printOptions.install, 100);
        }
    });
    
    // 暴露到全局以便调试
    window.PrintDialog = printOptions;
})();