MediaWiki:Gadget-PrintOptions.js
外观
注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5。
// ==UserScript==
// @name MediaWiki打印增强
// @description 为MediaWiki添加高级打印选项(优化布局、图片控制等)
// @namespace https://github.com/yourname/
// @version 1.1
// @license MIT
// @match *://*/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// ============== 核心配置 ==============
const PrintEnhancer = {
config: {
enhanced: true,
noimages: false,
norefs: false,
notoc: false,
nobackground: false,
blacktext: true
},
questions: [
{
type: 'checkbox',
returnvalue: 'enhanced',
label: mw.message('print-option-enhance').plain(),
help: 'print-option-enhance-help'
},
{
type: 'checkbox',
returnvalue: 'noimages',
label: mw.message('print-option-noimages').plain()
},
{
type: 'checkbox',
returnvalue: 'norefs',
label: mw.message('print-option-norefs').plain()
},
{
type: 'checkbox',
returnvalue: 'notoc',
label: mw.message('print-option-notoc').plain()
},
{
type: 'checkbox',
returnvalue: 'nobackground',
label: mw.message('print-option-nobg').plain(),
help: 'print-option-nobg-help'
},
{
type: 'checkbox',
returnvalue: 'blacktext',
label: mw.message('print-option-blacktext').plain()
}
],
// ============== 初始化 ==============
init() {
if (!document.getElementById('t-print')) return;
mw.loader.using(['oojs-ui', 'oojs-ui-windows']).then(() => {
const $printLink = $('#t-print a');
$printLink
.text(mw.message('print-version').plain())
.off('click')
.on('click', (e) => {
e.preventDefault();
e.stopPropagation();
this.createDialog();
});
});
},
// ============== 对话框 ==============
createDialog() {
function PrintDialog(config) {
PrintDialog.super.call(this, config);
}
OO.inheritClass(PrintDialog, OO.ui.ProcessDialog);
PrintDialog.static.name = 'printdialog';
PrintDialog.static.title = mw.message('print-dialog-title').plain();
PrintDialog.static.actions = [
{
action: 'print',
label: mw.message('print-action-print').plain(),
flags: ['primary', 'progressive']
},
{
action: 'cancel',
label: mw.message('print-action-cancel').plain(),
flags: 'safe'
}
];
PrintDialog.prototype.initialize = function() {
PrintDialog.super.prototype.initialize.apply(this, arguments);
const panel = new OO.ui.PanelLayout({ padded: true });
const fieldset = new OO.ui.FieldsetLayout({
label: mw.message('print-options').plain()
});
PrintEnhancer.questions.forEach(q => {
const checkbox = new OO.ui.CheckboxInputWidget({
selected: PrintEnhancer.config[q.returnvalue]
});
fieldset.addItems([
new OO.ui.FieldLayout(checkbox, {
label: q.label,
align: 'inline',
help: q.help ? mw.message(q.help).plain() : null
})
]);
q.widget = checkbox;
});
panel.$element.append(fieldset.$element);
this.$body.append(panel.$element);
};
PrintDialog.prototype.getActionProcess = function(action) {
const dialog = this;
return new OO.ui.Process(() => {
if (action === 'print') {
PrintEnhancer.questions.forEach(q => {
PrintEnhancer.config[q.returnvalue] = q.widget.isSelected();
});
return dialog.close({ action }).then(() => {
PrintEnhancer.applyPrintStyles();
setTimeout(() => {
window.print();
setTimeout(() => location.reload(), 1000);
}, 500);
});
}
return dialog.close({ action }); // 处理cancel
});
};
if (!this.windowManager) {
this.windowManager = new OO.ui.WindowManager();
$('body').append(this.windowManager.$element);
}
this.windowManager.addWindows([new PrintDialog({ size: 'medium' })]);
this.windowManager.openWindow(this.printDialog);
},
// ============== 打印样式 ==============
applyPrintStyles() {
let css = '@media print { ';
const c = this.config;
if (c.enhanced) css += `
.noprint, #siteNotice, .mw-indicators,
.mw-editsection, .firstHeading .mw-headline {
display: none !important;
}
`;
if (c.noimages) css += 'img, .video-container { display: none !important; }';
if (c.norefs) css += 'sup.reference, .references { display: none !important; }';
if (c.notoc) css += '#toc, .toc { display: none !important; }';
if (c.nobackground) css += 'body { background: white !important; }';
if (c.blacktext) css += 'body { color: black !important; }';
css += '}';
const style = document.createElement('style');
style.media = 'print';
style.textContent = css;
document.head.appendChild(style);
}
};
// ============== 消息初始化 ==============
mw.messages.set({
'print-version': '可打印版本',
'print-dialog-title': '打印设置',
'print-options': '自定义打印选项',
'print-action-print': '打印',
'print-action-cancel': '取消',
'print-option-enhance': '优化打印布局',
'print-option-enhance-help': '隐藏编辑按钮等非必要元素',
'print-option-noimages': '隐藏所有图片',
'print-option-norefs': '隐藏参考文献',
'print-option-notoc': '隐藏目录',
'print-option-nobg': '移除背景色',
'print-option-nobg-help': '节省墨水,但可能影响部分内容可见性',
'print-option-blacktext': '强制纯黑色文字'
});
// ============== 启动 ==============
mw.hook('wikipage.content').add(() => PrintEnhancer.init());
})();