MediaWiki:Gadget-PrintOptions.js:修订间差异
跳转到导航
跳转到搜索
删除的内容 添加的内容
小无编辑摘要 |
小无编辑摘要 |
||
| 第27行: | 第27行: | ||
.on('click', (e) => { |
.on('click', (e) => { |
||
e.preventDefault(); |
e.preventDefault(); |
||
e.stopPropagation(); // 关键修复:阻止事件冒泡 |
|||
mw.loader.using(['oojs-ui', 'oojs-ui-windows']).then(() => this.createDialog()); |
|||
}); |
}); |
||
}); |
}); |
||
| 第87行: | 第88行: | ||
}; |
}; |
||
PrintDialog.prototype.getActionProcess = function(action) { |
PrintDialog.prototype.getActionProcess = function(action) { |
||
const dialog = this; |
|||
if (action === 'print') { |
|||
return new OO.ui.Process(() |
return new OO.ui.Process(function() { |
||
if (action === 'print') { |
|||
// 保存用户选择 |
|||
PrintEnhancer.questions.forEach(q => { |
|||
PrintEnhancer.questions.forEach(q => { |
|||
PrintEnhancer.config[q.returnvalue] = q.widget.isSelected(); |
if (q.widget) PrintEnhancer.config[q.returnvalue] = q.widget.isSelected(); |
||
} |
}); |
||
// 关闭对话框 -> 应用样式 -> 延迟打印 |
|||
return dialog.close({ action }).then(() => { |
|||
// 2. 关闭对话框 |
|||
PrintEnhancer.applyPrintStyles(); |
|||
return new Promise(resolve => { |
|||
setTimeout(() => { |
|||
window.print(); |
|||
setTimeout(() => location.reload(), 1000); |
|||
// 4. 延迟确保样式生效后再触发打印 |
|||
resolve(); |
|||
}, 500); // 关键延迟:确保样式生效 |
|||
}); |
|||
}); |
|||
} else if (action === 'cancel') { |
|||
return dialog.close({ action }); // 关键修复:直接关闭 |
|||
} |
} |
||
}); |
}); |
||
} |
}; |
||
return PrintDialog.super.prototype.getActionProcess.call(this, action); |
|||
}; |
|||
if (!this.windowManager) { |
if (!this.windowManager) { |
||
| 第121行: | 第120行: | ||
this.printDialog = new PrintDialog({ size: 'medium' }); |
this.printDialog = new PrintDialog({ size: 'medium' }); |
||
this.windowManager.addWindows([this. |
this.windowManager.addWindows([this.printradleDialog]); |
||
this.windowManager.openWindow(this.printDialog); |
this.windowManager.openWindow(this.printDialog); |
||
}, |
}, |
||
| 第129行: | 第128行: | ||
const { config } = this; |
const { config } = this; |
||
// [...原有样式处理逻辑保持不变...] |
|||
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) { |
if (printStyle) { |
||
const style = document.createElement('style'); |
const style = document.createElement('style'); |
||
| 第196行: | 第138行: | ||
questions: [ |
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' |
|||
} |
|||
] |
] |
||
}; |
}; |
||
// [...原有i18n消息配置保持不变...] |
|||
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:19的版本
(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();
e.stopPropagation(); // 关键修复:阻止事件冒泡
mw.loader.using(['oojs-ui', 'oojs-ui-windows']).then(() => 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,
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(question.help).plain() : null
})
]);
question.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(function() {
if (action === 'print') {
// 保存用户选择
PrintEnhancer.questions.forEach(q => {
if (q.widget) PrintEnhancer.config[q.returnvalue] = q.widget.isSelected();
});
// 关闭对话框 -> 应用样式 -> 延迟打印
return dialog.close({ action }).then(() => {
PrintEnhancer.applyPrintStyles();
return new Promise(resolve => {
setTimeout(() => {
window.print();
setTimeout(() => location.reload(), 1000);
resolve();
}, 500); // 关键延迟:确保样式生效
});
});
} else if (action === 'cancel') {
return dialog.close({ 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.printradleDialog]);
this.windowManager.openWindow(this.printDialog);
},
applyPrintStyles() {
let printStyle = '';
const { config } = this;
// [...原有样式处理逻辑保持不变...]
if (printStyle) {
const style = document.createElement('style');
style.setAttribute('media', 'print');
style.textContent = printStyle;
document.head.appendChild(style);
}
},
questions: [
// [...原有问题配置保持不变...]
]
};
// [...原有i18n消息配置保持不变...]
mw.hook('wikipage.content').add(() => PrintEnhancer.init());
}());