MediaWiki:Gadget-PrintOptions.js:修订间差异
外观
创建页面,内容为“( function () { 'use strict'; const PrintEnhancer = { windowManager: null, printDialog: null, config: { enhanced: true, noimages: false, norefs: false, notoc: false, nobackground: false, blacktext: true }, init() { // Only run in content namespaces if (mw.config.get('wgNamespaceNumber') < 0) return;…” |
小无编辑摘要 |
||
| 第1行: | 第1行: | ||
( function () { | (function() { | ||
'use strict'; | 'use strict'; | ||
| 第16行: | 第16行: | ||
init() { | init() { | ||
if (mw.config.get('wgNamespaceNumber') < 0) return; | if (mw.config.get('wgNamespaceNumber') < 0) return; | ||
| 第28行: | 第27行: | ||
.on('click', (e) => { | .on('click', (e) => { | ||
e.preventDefault(); | e.preventDefault(); | ||
mw.loader.using('oojs-ui').then(() => this.createDialog()); | mw.loader.using('oojs-ui').then(() => this.createDialog()); | ||
}); | }); | ||
mw.loader.load('oojs-ui'); | mw.loader.load('oojs-ui'); | ||
}); | }); | ||
| 第43行: | 第40行: | ||
OO.inheritClass(PrintDialog, OO.ui.ProcessDialog); | OO.inheritClass(PrintDialog, OO.ui.ProcessDialog); | ||
PrintDialog.static.name = 'printdialog'; | PrintDialog.static.name = 'printdialog'; | ||
PrintDialog.static.title = mw.message('print-dialog-title').plain(); | PrintDialog.static.title = mw.message('print-dialog-title').plain(); | ||
| 第81行: | 第77行: | ||
label: question.label, | label: question.label, | ||
align: 'inline', | align: 'inline', | ||
help: question.help ? mw.message( | help: question.help ? mw.message(questionhelp).plain() : null | ||
}) | }) | ||
]); | ]); | ||
question.widget = checkbox; | question.widget = checkbox; | ||
} | } | ||
| 第97行: | 第92行: | ||
if (action === 'print') { | if (action === 'print') { | ||
return new OO.ui.Process(() => { | return new OO.ui.Process(() => { | ||
PrintEnhancer.questions.forEach(q => { | PrintEnhancer.questions.forEach(q => { | ||
if (q.type === 'checkbox' && q.widget) { | if (q.type === 'checkbox' && q.widget) { | ||
| 第106行: | 第100行: | ||
return this.close({ action }).then(() => { | return this.close({ action }).then(() => { | ||
PrintEnhancer.applyPrintStyles(); | PrintEnhancer.applyPrintStyles(); | ||
window.print(); | |||
return new Promise(resolve => { | |||
setTimeout(() => { | |||
window.print(); | |||
setTimeout(() => location.reload(), 500); | |||
resolve(); | |||
}, 300); | |||
}); | |||
}); | }); | ||
}); | }); | ||
} | } | ||
return PrintDialog.super.prototype.getActionProcess.call(this, action); | return PrintDialog.super.prototype.getActionProcess.call(this, action); | ||
}; | }; | ||
if (!this.windowManager) { | if (!this.windowManager) { | ||
this.windowManager =new OO.ui.WindowManager(); | this.windowManager = new OO.ui.WindowManager(); | ||
$('body').append(this.windowManager.$element); | $('body').append(this.windowManager.$element); | ||
} | } | ||
this.printDialog = new PrintDialog({ size: 'medium' }); | this.printDialog = new PrintDialog({ size: 'medium' }); | ||
this.windowManager.addWindows([this.printDialog]); | this.windowManager.addWindows([this.printDialog]); | ||
| 第131行: | 第128行: | ||
const { config } = this; | const { config } = this; | ||
if (config.enhanced) { | if (config.enhanced) { | ||
Array.from(document.styleSheets).forEach(sheet => { | Array.from(document.styleSheets).forEach(sheet => { | ||
| 第137行: | 第133行: | ||
try { | try { | ||
if (sheet.media.mediaText.includes('print') && | if (sheet.media.mediaText.includes('print') && | ||
!sheet.media.mediaText.includes('screen')) { | !sheet.media.mediaText.includes('screen')) { | ||
| 第143行: | 第138行: | ||
} | } | ||
if (sheet.media.mediaText.includes('screen') && | if (sheet.media.mediaText.includes('screen') && | ||
!sheet.media.mediaText.includes('print')) { | !sheet.media.mediaText.includes('print')) { | ||
| 第149行: | 第143行: | ||
} | } | ||
const rules = sheet.cssRules || sheet.rules; | const rules = sheet.cssRules || sheet.rules; | ||
if (!rules) return; | if (!rules) return; | ||
| 第173行: | 第166行: | ||
} | } | ||
if (config.noimages) { | if (config.noimages) { | ||
printStyle += 'img, .thumb, figure, .gallerybox { display:none !important; }'; | printStyle += 'img, .thumb, figure, .gallerybox { display:none !important; }'; | ||
} | } | ||
if (config.norefs) { | if (config.norefs) { | ||
printStyle += ` | printStyle += `.references, .reference, | ||
.mw-references-wrap, | |||
.citation, .cite_journal { | |||
display:none !important; | |||
}`; | |||
} | } | ||
if (config.notoc) { | if (config.notoc) { | ||
| 第240行: | 第230行: | ||
}; | }; | ||
mw.messages.set({ | mw.messages.set({ | ||
'print-version': '可打印版本', | 'print-version': '可打印版本', | ||
| 第257行: | 第246行: | ||
}); | }); | ||
mw.hook('wikipage.content').add(() => PrintEnhancer.init()); | mw.hook('wikipage.content').add(() => PrintEnhancer.init()); | ||
}() ); | }()); | ||
2025年11月1日 (六) 19:53的版本
(function() {
'use strict';
const PrintEnhancer = {
windowManager: null,
printDialog: null,
config: {
enhanced: true,
noimages: false,
norefs: false,
notoc: false,
nobackground: false,
blacktext: true
},
init() {
if (mw.config.get('wgNamespaceNumber') < 0) return;
mw.hook('wikipage.content').add(() => {
const $printLink = $('#t-print a');
if (!$printLink.length) return;
$printLink
.text(mw.message('print-version').plain())
.off('click')
.on('click', (e) => {
e.preventDefault();
mw.loader.using('oojs-ui').then(() => this.createDialog());
});
mw.loader.load('oojs-ui');
});
},
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,
expanded: false
});
const fieldset = new OO.ui.FieldsetLayout({
label: mw.message('print-options').plain()
});
PrintEnhancer.questions.forEach(question => {
if (question.type === 'checkbox') {
const checkbox = new OO.ui.CheckboxInputWidget({
selected: PrintEnhancer.config[question.returnvalue]
});
fieldset.addItems([
new OO.ui.FieldLayout(checkbox, {
label: question.label,
align: 'inline',
help: question.help ? mw.message(questionhelp).plain() : null
})
]);
question.widget = checkbox;
}
});
panel.$element.append(fieldset.$element);
this.$body.append(panel.$element);
};
PrintDialog.prototype.getActionProcess = function(action) {
if (action === 'print') {
return new OO.ui.Process(() => {
PrintEnhancer.questions.forEach(q => {
if (q.type === 'checkbox' && q.widget) {
PrintEnhancer.config[q.returnvalue] = q.widget.isSelected();
}
});
return this.close({ action }).then(() => {
PrintEnhancer.applyPrintStyles();
return new Promise(resolve => {
setTimeout(() => {
window.print();
setTimeout(() => location.reload(), 500);
resolve();
}, 300);
});
});
});
}
return PrintDialog.super.prototype.getActionProcess.call(this, action);
};
if (!this.windowManager) {
this.windowManager = new OO.ui.WindowManager();
$('body').append(this.windowManager.$element);
}
this.printDialog = new PrintDialog({ size: 'medium' });
this.windowManager.addWindows([this.printDialog]);
this.windowManager.openWindow(this.printDialog);
},
applyPrintStyles() {
let printStyle = '';
const { config } = this;
if (config.enhanced) {
Array.from(document.styleSheets).forEach(sheet => {
if (!sheet.media) return;
try {
if (sheet.media.mediaText.includes('print') &&
!sheet.media.mediaText.includes('screen')) {
sheet.disabled = true;
}
if (sheet.media.mediaText.includes('screen') &&
!sheet.media.mediaText.includes('print')) {
sheet.media.appendMedium('print');
}
const rules = sheet.cssRules || sheet.rules;
if (!rules) return;
Array.from(rules).forEach((rule, index) => {
if (rule.type === CSSRule.MEDIA_RULE && rule.media) {
const media = Array.from(rule.media);
const hasPrint = media.includes('print');
const hasScreen = media.includes('screen');
if (hasPrint && !hasScreen) {
sheet.deleteRule(index);
} else if (hasScreen && !hasPrint) {
rule.media.appendMedium('print');
}
}
});
} catch (e) {
mw.log.warn('PrintEnhancer: Could not process stylesheet', sheet.href, e);
}
});
}
if (config.noimages) {
printStyle += 'img, .thumb, figure, .gallerybox { display:none !important; }';
}
if (config.norefs) {
printStyle += `.references, .reference,
.mw-references-wrap,
.citation, .cite_journal {
display:none !important;
}`;
}
if (config.notoc) {
printStyle += '#toc, .toc, .toccolours { display:none !important; }';
}
if (config.nobackground) {
printStyle += '* { background:none !important; }';
}
if (config.blacktext) {
printStyle += '* { color:#000 !important; }';
}
if (printStyle) {
const style = document.createElement('style');
style.setAttribute('media', 'print');
style.textContent = printStyle;
document.head.appendChild(style);
}
},
questions: [
{
label: mw.message('print-option-enhance').plain(),
type: 'checkbox',
help: 'print-option-enhance-help',
returnvalue: 'enhanced'
},
{
label: mw.message('print-option-noimages').plain(),
type: 'checkbox',
returnvalue: 'noimages'
},
{
label: mw.message('print-option-norefs').plain(),
type: 'checkbox',
returnvalue: 'norefs'
},
{
label: mw.message('print-option-notoc').plain(),
type: 'checkbox',
returnvalue: 'notoc'
},
{
label: mw.message('print-option-nobg').plain(),
type: 'checkbox',
help: 'print-option-nobg-help',
returnvalue: 'nobackground'
},
{
label: mw.message('print-option-blacktext').plain(),
type: 'checkbox',
returnvalue: 'blacktext'
}
]
};
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());
}());