跳转到内容
主菜单
主菜单
移至侧栏
隐藏
导航
首页
分类索引
最近更改
随便看看
灵兰秘典
捐助本站
帮助
帮助
联系我们
关于本站
MediaWiki帮助
USER-SIDEBAR
GROUP-SIDEBAR
CATEGORY-SIDEBAR
中医百科
搜索
搜索
外观
登录
个人工具
登录
查看“︁MediaWiki:Gadget-PrintOptions.js”︁的源代码
系统消息
讨论
English
阅读
查看源代码
查看历史
工具
工具
移至侧栏
隐藏
操作
阅读
查看源代码
查看历史
常规
链入页面
相关更改
特殊页面
页面信息
外观
移至侧栏
隐藏
←
MediaWiki:Gadget-PrintOptions.js
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
此页面为本wiki上的软件提供界面文本,并受到保护以防止滥用。 如欲修改所有wiki的翻译,请访问
translatewiki.net
上的MediaWiki本地化项目。
您无权编辑此JavaScript页面,因为编辑此页面可能会影响所有访问者。
您可以查看和复制此页面的源代码。
// ==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(); }); })();
返回
MediaWiki:Gadget-PrintOptions.js
。