MediaWiki:Gadget-PrintOptions.js:修订间差异
外观
小无编辑摘要 |
无编辑摘要 |
||
| 第1行: | 第1行: | ||
// ==UserScript== | |||
// @name MediaWiki打印增强 | |||
// @description 为MediaWiki添加高级打印选项(优化布局、图片控制等) | |||
// @namespace https://github.com/yourname/ | |||
// @version 1.1 | |||
// @license MIT | |||
// @match *://*/* | |||
// @grant none | |||
// ==/UserScript== | |||
(function() { | (function() { | ||
'use strict'; | 'use strict'; | ||
// ============== 核心配置 ============== | |||
const PrintEnhancer = { | const PrintEnhancer = { | ||
config: { | config: { | ||
enhanced: true, | enhanced: true, | ||
| 第15行: | 第23行: | ||
}, | }, | ||
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() { | init() { | ||
if ( | if (!document.getElementById('t-print')) return; | ||
mw. | mw.loader.using(['oojs-ui', 'oojs-ui-windows']).then(() => { | ||
const $printLink = $('#t-print a'); | const $printLink = $('#t-print a'); | ||
$printLink | $printLink | ||
.text(mw.message('print-version').plain()) | .text(mw.message('print-version').plain()) | ||
| 第27行: | 第69行: | ||
.on('click', (e) => { | .on('click', (e) => { | ||
e.preventDefault(); | e.preventDefault(); | ||
e.stopPropagation(); | e.stopPropagation(); | ||
this.createDialog(); | |||
}); | }); | ||
}); | }); | ||
}, | }, | ||
// ============== 对话框 ============== | |||
createDialog() { | createDialog() { | ||
function PrintDialog(config) { | function PrintDialog(config) { | ||
| 第56行: | 第99行: | ||
PrintDialog.prototype.initialize = function() { | PrintDialog.prototype.initialize = function() { | ||
PrintDialog.super.prototype.initialize.apply(this, arguments); | PrintDialog.super.prototype.initialize.apply(this, arguments); | ||
const panel = new OO.ui.PanelLayout({ padded: true }); | |||
const panel = new OO.ui.PanelLayout({ | |||
const fieldset = new OO.ui.FieldsetLayout({ | const fieldset = new OO.ui.FieldsetLayout({ | ||
label: mw.message('print-options').plain() | label: mw.message('print-options').plain() | ||
}); | }); | ||
PrintEnhancer.questions.forEach( | 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; | |||
}); | }); | ||
| 第90行: | 第124行: | ||
PrintDialog.prototype.getActionProcess = function(action) { | PrintDialog.prototype.getActionProcess = function(action) { | ||
const dialog = this; | const dialog = this; | ||
return new OO.ui.Process( | return new OO.ui.Process(() => { | ||
if (action === 'print') { | if (action === 'print') { | ||
PrintEnhancer.questions.forEach(q => { | PrintEnhancer.questions.forEach(q => { | ||
PrintEnhancer.config[q.returnvalue] = q.widget.isSelected(); | |||
}); | }); | ||
return dialog.close({ action }).then(() => { | return dialog.close({ action }).then(() => { | ||
PrintEnhancer.applyPrintStyles(); | PrintEnhancer.applyPrintStyles(); | ||
setTimeout(() => { | |||
window.print(); | |||
setTimeout(() => location.reload(), 1000); | |||
}, 500); | |||
}); | }); | ||
} | } | ||
return dialog.close({ action }); // 处理cancel | |||
}); | }); | ||
}; | }; | ||
| 第118行: | 第145行: | ||
$('body').append(this.windowManager.$element); | $('body').append(this.windowManager.$element); | ||
} | } | ||
this.windowManager.addWindows([new PrintDialog({ size: 'medium' })]); | |||
this. | |||
this.windowManager.openWindow(this.printDialog); | this.windowManager.openWindow(this.printDialog); | ||
}, | }, | ||
// ============== 打印样式 ============== | |||
applyPrintStyles() { | applyPrintStyles() { | ||
let | let css = '@media print { '; | ||
const | const c = this.config; | ||
if (c.enhanced) css += ` | |||
if ( | .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()); | mw.hook('wikipage.content').add(() => PrintEnhancer.init()); | ||
}( | })(); | ||
2025年11月1日 (六) 20:22的版本
// ==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());
})();