MediaWiki:Gadget-PrintOptions.js:修订间差异
外观
小无编辑摘要 |
小无编辑摘要 标签:手工回退 |
||
| (未显示同一用户的13个中间版本) | |||
| 第1行: | 第1行: | ||
(function() { | (function () { | ||
'use strict'; | 'use strict'; | ||
var windowManager; | |||
var printDialog; | |||
var printOptions = { | |||
install: function () { | |||
var $printLink = $('#t-print a'); | |||
if ($printLink.length === 0) return; | |||
$printLink | |||
.text('可打印版本') | |||
.off('click') | |||
.on('click', function (e) { | |||
mw.loader.using(['oojs-ui-core', 'oojs-ui-widgets', 'oojs-ui-windows']) | |||
.then(printOptions.createWindow) | |||
.catch(function (err) { | |||
mw.notify('打印功能加载失败: ' + err.message, { type: 'error' }); | |||
}); | |||
e.preventDefault(); | |||
}); | |||
// 预加载资源 | |||
mw.loader.load(['oojs-ui-core', 'oojs-ui-widgets', 'oojs-ui-windows']); | |||
}, | |||
createWindow: function () { | |||
function PrintDialog(config) { | function PrintDialog(config) { | ||
PrintDialog.super.call(this, config); | PrintDialog.super.call(this, config); | ||
} | } | ||
OO.inheritClass(PrintDialog, OO.ui.ProcessDialog); | OO.inheritClass(PrintDialog, OO.ui.ProcessDialog); | ||
PrintDialog.static.name = 'printdialog'; | PrintDialog.static.name = 'printdialog'; | ||
PrintDialog.static.title = | PrintDialog.static.title = '打印此页面'; | ||
PrintDialog.static.actions = [ | PrintDialog.static.actions = [ | ||
{ | { action: 'print', label: '打印', flags: ['primary', 'progressive'] }, | ||
{ action: 'cancel', label: '取消', flags: 'safe' } | |||
{ | |||
]; | ]; | ||
PrintDialog.prototype.initialize = function() { | PrintDialog.prototype.initialize = function () { | ||
PrintDialog.super.prototype.initialize.apply(this, arguments); | PrintDialog.super.prototype.initialize.apply(this, arguments); | ||
this.panel = new OO.ui.PanelLayout({ padded: true, expanded: false }); | |||
this.content = new OO.ui.FieldsetLayout(); | |||
printOptions.questions.forEach(function (question) { | |||
if (question.type === 'checkbox') { | if (question.type === 'checkbox') { | ||
var checkbox = new OO.ui.CheckboxInputWidget({ | |||
selected: | selected: question.checked | ||
}); | }); | ||
question.widget = checkbox; | |||
this.content.addItems([ | |||
new OO.ui.FieldLayout(checkbox, { | new OO.ui.FieldLayout(checkbox, { | ||
label: question.label, | label: question.label, | ||
align: 'inline' | align: 'inline' | ||
}) | }) | ||
]); | ]); | ||
} | } | ||
}); | }, this); | ||
panel.$element.append( | this.panel.$element.append(this.content.$element); | ||
this.$body.append(panel.$element); | this.$body.append(this.panel.$element); | ||
}; | }; | ||
PrintDialog.prototype.getActionProcess = function(action) { | // ✅ 修复点:明确处理取消动作 | ||
PrintDialog.prototype.getActionProcess = function (action) { | |||
if (action === 'print') { | if (action === 'print') { | ||
return new OO.ui.Process(() | return new OO.ui.Process(function () { | ||
printOptions.questions.forEach(function (question) { | |||
if ( | if (question.widget) { | ||
printOptions[question.returnvalue] = question.widget.isSelected(); | |||
} | } | ||
}); | }); | ||
return this.close({ action: action }).then(function () { | |||
return this.close({ action }).then(() | printOptions.changePrintCSS(); | ||
printOptions.otherEnhancements(); | |||
setTimeout(window.print, 100); | |||
}); | }); | ||
}); | }.bind(this)); | ||
} | |||
// 🔥 关键修复:取消时直接关闭对话框 | |||
if (action === 'cancel') { | |||
return new OO.ui.Process(function () { | |||
return this.close({ action: action }); | |||
}.bind(this)); | |||
} | } | ||
return PrintDialog.super.prototype.getActionProcess.call(this, action); | return PrintDialog.super.prototype.getActionProcess.call(this, action); | ||
}; | }; | ||
if (! | if (!windowManager) { | ||
windowManager = new OO.ui.WindowManager(); | |||
$('body').append( | $('body').append(windowManager.$element); | ||
} | |||
if (!printDialog) { | |||
printDialog = new PrintDialog({ size: 'medium' }); | |||
windowManager.addWindows([printDialog]); | |||
} | } | ||
windowManager.openWindow(printDialog); | |||
}, | }, | ||
changePrintCSS: function () { | |||
/* 原有CSS处理逻辑 */ | |||
var printStyle = ''; | |||
if (this.noimages) printStyle += 'img, .thumb { display:none !important; }\n'; | |||
if ( | if (this.norefs) printStyle += '.references, .reference { display:none !important; }\n'; | ||
if (this.notoc) printStyle += '#toc, .toc { display:none !important; }\n'; | |||
if (this.nobackground) printStyle += 'body { background: white !important; }\n'; | |||
if (this.blacktext) printStyle += '* { color: black !important; }\n'; | |||
$('style[media="print"]').remove(); // 清理旧样式 | |||
if (printStyle) { | |||
$('head').append('<style media="print">' + printStyle + '</style>'); | |||
} | } | ||
}, | |||
otherEnhancements: function () { | |||
var $link = $('div.printfooter a'); | |||
try { | |||
$link.text(decodeURIComponent($link.text())); | |||
} catch (e) { | |||
mw.log.warn('URL解码失败:', e); | |||
} | |||
} | } | ||
}, | }, | ||
questions: [ | questions: [ | ||
{ | { | ||
label: | label: '隐藏界面元素', | ||
type: 'checkbox', | type: 'checkbox', | ||
checked: true, | |||
returnvalue: 'enhanced' | returnvalue: 'enhanced' | ||
}, | }, | ||
{ | { | ||
label: | label: '隐藏图像', | ||
type: 'checkbox', | type: 'checkbox', | ||
checked: false, | |||
returnvalue: 'noimages' | returnvalue: 'noimages' | ||
}, | }, | ||
{ | { | ||
label: | label: '隐藏引用', | ||
type: 'checkbox', | type: 'checkbox', | ||
checked: false, | |||
returnvalue: 'norefs' | returnvalue: 'norefs' | ||
}, | }, | ||
{ | { | ||
label: | label: '隐藏目录', | ||
type: 'checkbox', | type: 'checkbox', | ||
checked: false, | |||
returnvalue: 'notoc' | returnvalue: 'notoc' | ||
}, | }, | ||
{ | { | ||
label: | label: '删除背景(部分浏览器可能无效)', | ||
type: 'checkbox', | type: 'checkbox', | ||
checked: false, | |||
returnvalue: 'nobackground' | returnvalue: 'nobackground' | ||
}, | }, | ||
{ | { | ||
label: | label: '强制文本为黑色', | ||
type: 'checkbox', | type: 'checkbox', | ||
checked: true, | |||
returnvalue: 'blacktext' | returnvalue: 'blacktext' | ||
} | } | ||
] | ] | ||
}; | }; | ||
mw. | // 延迟初始化以避免冲突 | ||
$(function () { | |||
if (mw.config.get('wgNamespaceNumber') >= 0) { | |||
setTimeout(printOptions.install, 100); | |||
} | |||
}); | }); | ||
})(); | |||
2025年11月1日 (六) 22:56的最新版本
(function () {
'use strict';
var windowManager;
var printDialog;
var printOptions = {
install: function () {
var $printLink = $('#t-print a');
if ($printLink.length === 0) return;
$printLink
.text('可打印版本')
.off('click')
.on('click', function (e) {
mw.loader.using(['oojs-ui-core', 'oojs-ui-widgets', 'oojs-ui-windows'])
.then(printOptions.createWindow)
.catch(function (err) {
mw.notify('打印功能加载失败: ' + err.message, { type: 'error' });
});
e.preventDefault();
});
// 预加载资源
mw.loader.load(['oojs-ui-core', 'oojs-ui-widgets', 'oojs-ui-windows']);
},
createWindow: function () {
function PrintDialog(config) {
PrintDialog.super.call(this, config);
}
OO.inheritClass(PrintDialog, OO.ui.ProcessDialog);
PrintDialog.static.name = 'printdialog';
PrintDialog.static.title = '打印此页面';
PrintDialog.static.actions = [
{ action: 'print', label: '打印', flags: ['primary', 'progressive'] },
{ action: 'cancel', label: '取消', flags: 'safe' }
];
PrintDialog.prototype.initialize = function () {
PrintDialog.super.prototype.initialize.apply(this, arguments);
this.panel = new OO.ui.PanelLayout({ padded: true, expanded: false });
this.content = new OO.ui.FieldsetLayout();
printOptions.questions.forEach(function (question) {
if (question.type === 'checkbox') {
var checkbox = new OO.ui.CheckboxInputWidget({
selected: question.checked
});
question.widget = checkbox;
this.content.addItems([
new OO.ui.FieldLayout(checkbox, {
label: question.label,
align: 'inline'
})
]);
}
}, this);
this.panel.$element.append(this.content.$element);
this.$body.append(this.panel.$element);
};
// ✅ 修复点:明确处理取消动作
PrintDialog.prototype.getActionProcess = function (action) {
if (action === 'print') {
return new OO.ui.Process(function () {
printOptions.questions.forEach(function (question) {
if (question.widget) {
printOptions[question.returnvalue] = question.widget.isSelected();
}
});
return this.close({ action: action }).then(function () {
printOptions.changePrintCSS();
printOptions.otherEnhancements();
setTimeout(window.print, 100);
});
}.bind(this));
}
// 🔥 关键修复:取消时直接关闭对话框
if (action === 'cancel') {
return new OO.ui.Process(function () {
return this.close({ action: action });
}.bind(this));
}
return PrintDialog.super.prototype.getActionProcess.call(this, action);
};
if (!windowManager) {
windowManager = new OO.ui.WindowManager();
$('body').append(windowManager.$element);
}
if (!printDialog) {
printDialog = new PrintDialog({ size: 'medium' });
windowManager.addWindows([printDialog]);
}
windowManager.openWindow(printDialog);
},
changePrintCSS: function () {
/* 原有CSS处理逻辑 */
var printStyle = '';
if (this.noimages) printStyle += 'img, .thumb { display:none !important; }\n';
if (this.norefs) printStyle += '.references, .reference { display:none !important; }\n';
if (this.notoc) printStyle += '#toc, .toc { display:none !important; }\n';
if (this.nobackground) printStyle += 'body { background: white !important; }\n';
if (this.blacktext) printStyle += '* { color: black !important; }\n';
$('style[media="print"]').remove(); // 清理旧样式
if (printStyle) {
$('head').append('<style media="print">' + printStyle + '</style>');
}
},
otherEnhancements: function () {
var $link = $('div.printfooter a');
try {
$link.text(decodeURIComponent($link.text()));
} catch (e) {
mw.log.warn('URL解码失败:', e);
}
},
questions: [
{
label: '隐藏界面元素',
type: 'checkbox',
checked: true,
returnvalue: 'enhanced'
},
{
label: '隐藏图像',
type: 'checkbox',
checked: false,
returnvalue: 'noimages'
},
{
label: '隐藏引用',
type: 'checkbox',
checked: false,
returnvalue: 'norefs'
},
{
label: '隐藏目录',
type: 'checkbox',
checked: false,
returnvalue: 'notoc'
},
{
label: '删除背景(部分浏览器可能无效)',
type: 'checkbox',
checked: false,
returnvalue: 'nobackground'
},
{
label: '强制文本为黑色',
type: 'checkbox',
checked: true,
returnvalue: 'blacktext'
}
]
};
// 延迟初始化以避免冲突
$(function () {
if (mw.config.get('wgNamespaceNumber') >= 0) {
setTimeout(printOptions.install, 100);
}
});
})();