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 高级打印选项(优化布局/隐藏图片等)
// @namespace https://github.com/yourname/
// @version 2.0
// @match *://*/*
// @grant none
// @require https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js
// ==/UserScript==
(function() {
'use strict';
// ============== 核心配置 ==============
const PrintEnhancer = {
// 默认配置
config: {
enhanced: true,
noimages: false,
norefs: false,
notoc: false,
nobackground: false,
blacktext: true
},
// 对话框选项
questions: [
{
type: 'checkbox',
name: 'enhanced',
label: '优化打印布局',
help: '隐藏编辑按钮等非必要元素'
},
{
type: 'checkbox',
name: 'noimages',
label: '隐藏所有图片'
},
{
type: 'checkbox',
name: 'norefs',
label: '隐藏参考文献'
},
{
type: 'checkbox',
name: 'notoc',
label: '隐藏目录'
},
{
type: 'checkbox',
name: 'nobackground',
label: '移除背景色',
help: '节省墨水,但可能影响部分内容可见性'
},
{
type: 'checkbox',
name: 'blacktext',
label: '强制纯黑色文字'
}
],
// ============== 初始化 ==============
init() {
const $printLink = $('#t-print a, .vector-menu-tabs a[href*="printable=yes"]').first();
if (!$printLink.length) {
console.warn('找不到打印链接');
return;
}
$printLink
.text('可打印版本')
.off('click.mwprint')
.on('click.mwprint', (e) => {
e.preventDefault();
this.openDialog();
});
},
// ============== 对话框控制 ==============
openDialog() {
// 动态加载OOUI库
if (!window.OO) {
mw.loader.using(['oojs-ui', 'oojs-ui-windows']).then(() => {
this._createDialog();
}).catch(err => {
console.error('加载OOUI失败:', err);
alert('打印功能初始化失败,请刷新页面重试');
});
} else {
this._createDialog();
}
},
_createDialog() {
// 1. 创建对话框类
function PrintDialog(config) {
PrintDialog.super.call(this, config);
}
OO.inheritClass(PrintDialog, OO.ui.ProcessDialog);
// 2. 对话框配置
PrintDialog.static.name = 'PrintEnhancerDialog';
PrintDialog.static.title = '打印设置';
PrintDialog.static.actions = [
{
action: 'print',
label: '打印',
flags: ['primary', 'progressive']
},
{
action: 'cancel',
label: '取消',
flags: 'safe'
}
];
// 3. 构建对话框内容
PrintDialog.prototype.initialize = function() {
PrintDialog.super.prototype.initialize.apply(this, arguments);
const panel = new OO.ui.PanelLayout({
padded: true,
expanded: false
});
const fieldset = new OO.ui.FieldsetLayout({
label: '打印选项'
});
// 添加所有复选框
PrintEnhancer.questions.forEach(q => {
const checkbox = new OO.ui.CheckboxInputWidget({
selected: PrintEnhancer.config[q.name]
});
const field = new OO.ui.FieldLayout(checkbox, {
label: q.label,
align: 'inline',
help: q.help ? new OO.ui.HtmlSnippet(q.help) : null
});
fieldset.addItems([field]);
this[q.name] = checkbox; // 保存引用
});
panel.$element.append(fieldset.$element);
this.$body.append(panel.$element);
};
// 4. 处理按钮动作
PrintDialog.prototype.getActionProcess = function(action) {
const dialog = this;
return new OO.ui.Process(() => {
if (action === 'print') {
// 保存用户选择
PrintEnhancer.questions.forEach(q => {
PrintEnhancer.config[q.name] = dialog[q.name].isSelected();
});
}
dialog.close({ action });
});
};
// 5. 创建窗口管理器
if (!this.windowManager) {
this.windowManager = new OO.ui.WindowManager();
$(document.body).append(this.windowManager.$element);
}
// 6. 显示对话框
const dialog = new PrintDialog({ size: 'medium' });
this.windowManager.addWindows([dialog]);
this.windowManager.openWindow(dialog).then(() => {
if (dialog.action === 'print') {
this.applyPrintStyles();
setTimeout(() => window.print(), 300);
}
});
},
// ============== 打印样式应用 ==============
applyPrintStyles() {
const cssRules = [];
const c = this.config;
if (c.enhanced) cssRules.push(`
.noprint, #siteNotice, .mw-editsection,
.mw-indicators, .firstHeading .mw-headline {
display: none !important;
}
`);
if (c.noimages) cssRules.push('img, video, .video-container { display: none !important; }');
if (c.norefs) cssRules.push('sup.reference, .references { display: none !important; }');
if (c.notoc) cssRules.push('#toc, .toc { display: none !important; }');
if (c.nobackground) cssRules.push('body { background: white !important; }');
if (c.blacktext) cssRules.push('body, body * { color: black !important; }');
const styleId = 'mw-print-styles';
let style = document.getElementById(styleId);
if (!style) {
style = document.createElement('style');
style.id = styleId;
document.head.appendChild(style);
}
style.textContent = `@media print { ${cssRules.join('')} }`;
}
};
// ============== 自动启动 ==============
$(document).ready(function() {
// 确保在DOM加载后运行
setTimeout(() => PrintEnhancer.init(), 300);
});
// 兼容皮肤动态加载
mw.hook('wikipage.content').add(function() {
PrintEnhancer.init();
});
})();