MediaWiki:Gadget-PrintOptions.js:修订间差异

勤求古训,博采众方
跳转到导航 跳转到搜索
删除的内容 添加的内容
小无编辑摘要
小无编辑摘要
第1行: 第1行:
(function () {
// ==UserScript==
// @name Safe Web Audio Implementation
// @namespace https://github.com/
// @version 1.0
// @description Proper AudioContext initialization with full error handling
// @author You
// @match *://*/*
// @grant none
// @require https://cdn.jsdelivr.net/npm/web-audio-api-shim@0.2.6/build/WebAudioAPI.min.js
// ==/UserScript==

(function() {
'use strict';
'use strict';

var windowManager;
// 🔊 Audio Context Manager Class
class AudioSystem {
var printDialog;
constructor(options = {}) {
// Modernized configuration object
this.options = {
var printOptions = {
latencyHint: 'interactive',
sampleRate: 44100,
install: function () {
...options
var $printLink = $('#t-print a');
};
if ($printLink.length === 0) {
this.context = null;
this.init();
}

// 🔍 Detect supported AudioContext class
static getAudioContextClass() {
const prefixes = ['', 'webkit', 'moz', 'ms'];
for (const prefix of prefixes) {
const name = prefix ? `${prefix}AudioContext` : 'AudioContext';
if (window[name]) return window[name];
}
return null;
}

// 🏗️ Initialize audio system
init() {
const AudioContextClass = AudioSystem.getAudioContextClass();
if (!AudioContextClass) {
console.warn('Web Audio API not supported');
return;
return;
}
}

try {
$printLink
this.context = new AudioContextClass(this.options);
.text('可打印版本')
this.setupEventListeners();
.off('click')
this.log('AudioContext created');
.on('click', function (e) {
} catch (error) {
mw.loader.using([
console.error('AudioContext initialization failed:', error);
'oojs-ui-core',
}
'oojs-ui-widgets',
'oojs-ui-windows',
}
'mediawiki.api'

]).then(printOptions.createWindow);
// 🎛️ Setup state management
setupEventListeners() {
e.preventDefault();
if (!this.context) return;
e.stopPropagation();
});

// Monitor state changes
this.context.onstatechange = () => {
// Preload resources
mw.loader.load(['oojs-ui-core', 'oojs-ui-widgets', 'oojs-ui-windows']);
this.log(`State changed: ${this.context.state}`);
},
if (this.context.state === 'suspended') {
createWindow: function () {
this.addActivationTriggers();
// Modern OOUI dialog implementation
var PrintDialog = function (config) {
PrintDialog.super.call(this, config);
};
OO.inheritClass(PrintDialog, OO.ui.ProcessDialog);
PrintDialog.static.name = 'printdialog';
PrintDialog.static.title = mw.message('printdialog-title').plain();
PrintDialog.static.actions = [
{
action: 'print',
label: mw.message('printdialog-action-print').plain(),
flags: ['primary', 'progressive']
},
{
action: 'cancel',
label: mw.message('cancel').plain(),
flags: 'safe'
}
}
];
PrintDialog.prototype.initialize = function () {
PrintDialog.super.prototype.initialize.apply(this, arguments);
this.contentPanel = new OO.ui.PanelLayout({
padded: true,
expanded: false
});
this.fieldset = new OO.ui.FieldsetLayout({
label: mw.message('printdialog-options').plain()
});
// Dynamically create checkboxes from configuration
printOptions.questions.forEach(function (question) {
if (question.type === 'checkbox') {
var checkbox = new OO.ui.CheckboxInputWidget({
selected: question.checked
});
question.widget = checkbox;
this.fieldset.addItems([
new OO.ui.FieldLayout(checkbox, {
label: mw.message(question.msgKey || question.label).plain(),
align: 'inline'
})
]);
}
}.bind(this));
this.contentPanel.$element.append(this.fieldset.$element);
this.$body.append(this.contentPanel.$element);
};
};

PrintDialog.prototype.getActionProcess = function (action) {
// Initial activation check
if (this.context.state === 'suspended') {
var dialog = this;
this.addActivationTriggers();
}
if (action === 'print') {
return new OO.ui.Process(function () {
}
// Gather all selected options

printOptions.questions.forEach(function (question) {
// 🖱️ Add user interaction triggers
if (question.type === 'checkbox' && question.widget) {
addActivationTriggers() {
printOptions[question.returnvalue] = question.widget.isSelected();
const events = ['click', 'keydown', 'touchstart'];
const handler = () => {
}
if (this.context && this.context.state === 'suspended') {
});
this.context.resume()
.then(() => this.log('Audio resumed successfully'))
return dialog.close({ action: action }).then(function () {
.catch(err => console.error('Resume failed:', err));
printOptions.prepareForPrinting();
window.print();
setTimeout(function () {
window.location.reload();
}, 500);
});
});
}
}
return PrintDialog.super.prototype.getActionProcess.call(this, action);
// Remove after first activation
events.forEach(e => document.removeEventListener(e, handler));
};
};

// Initialize window manager if needed
events.forEach(e => document.addEventListener(e, handler));
}
if (!windowManager) {
windowManager = new OO.ui.WindowManager();

windowManager.$element.css('z-index', '2000');
// 📝 Logging helper
$(document.body).append(windowManager.$element);
log(message) {
console.log(`[AudioSystem] ${message}`);
}

// 🎵 Safe audio playback
playBuffer(buffer) {
if (!this.context || this.context.state !== 'running') {
console.warn('Audio not ready');
return null;
}
}

try {
// Create dialog if needed
if (!printDialog) {
const source = this.context.createBufferSource();
source.buffer = buffer;
printDialog = new PrintDialog({
source.connect(this.context.destination);
size: 'medium',
source.start();
classes: ['print-dialog']
return source;
});
} catch (error) {
console.error('Playback failed:', error);
windowManager.addWindows([printDialog]);
return null;
}
}
}
windowManager.openWindow(printDialog);
}
},

// 🚀 Startup sequence
prepareForPrinting: function () {
document.addEventListener('DOMContentLoaded', () => {
const audioSystem = new AudioSystem({
this.updatePrintStyles();
latencyHint: 'balanced',
this.applyCustomPrintStyles();
sampleRate: 48000
this.enhancePrintFooter();
});
},

// Example usage
updatePrintStyles: function () {
setTimeout(() => {
if (this.enhanced === false) return;
if (audioSystem.context) {
Array.from(document.styleSheets).forEach(function (stylesheet) {
// Load and play dummy sound
const buffer = audioSystem.context.createBuffer(
try {
1,
if (!stylesheet.media) return;
audioSystem.context.sampleRate * 0.5,
audioSystem.context.sampleRate
// Handle media rules for stylesheets
);
var mediaText = stylesheet.media.mediaText || '';
audioSystem.playBuffer(buffer);
if (mediaText.includes('print')) {
if (!mediaText.includes('screen')) {
stylesheet.disabled = true;
}
} else if (mediaText.includes('screen')) {
try {
stylesheet.media.appendMedium('print');
} catch (e) {
stylesheet.media.mediaText += ',print';
}
}
// Handle media rules within stylesheets
var rules = stylesheet.cssRules || stylesheet.rules;
if (!rules) return;
for (var i = 0; i < rules.length; i++) {
var rule = rules[i];
if (rule.type !== CSSRule.MEDIA_RULE || !rule.media) continue;
var media = Array.from(rule.media);
if (media.includes('print') && !media.includes('screen')) {
stylesheet.deleteRule ? stylesheet.deleteRule(i) : stylesheet.removeRule(i);
i--;
} else if (media.includes('screen') && !media.includes('print')) {
try {
rule.media.appendMedium('print');
} catch (e) {
rule.media.mediaText += ',print';
}
}
}
} catch (e) {
mw.log.warn('Could not process stylesheet:', e.message);
}
});
},
applyCustomPrintStyles: function () {
var styles = [];
if (this.noimages) {
styles.push('img, .thumb, figure { display: none !important; }');
}
}
}, 3000);
if (this.norefs) {
styles.push('.references, .reference, .mw-references-wrap { display: none !important; }');
}
if (this.notoc) {
styles.push('#toc, .toc { display: none !important; }');
}
if (this.nobackground) {
styles.push('* { background: none !important; }');
styles.push('body { background-color: white !important; }');
}
if (this.blacktext) {
styles.push('* { color: black !important; }');
styles.push('a { color: #0645ad !important; }');
}
if (styles.length) {
var styleEl = document.createElement('style');
styleEl.setAttribute('media', 'print');
styleEl.textContent = styles.join('\n');
document.head.appendChild(styleEl);
}
},
enhancePrintFooter: function () {
var printFooter = document.querySelector('.printfooter');
if (printFooter) {
var link = printFooter.querySelector('a');
if (link) {
try {
link.textContent = decodeURIComponent(link.textContent);
} catch (e) {
mw.log.warn('Could not decode print footer URL:', e.message);
}
}
}
},
// Updated configuration with i18n message keys
questions: [
{
msgKey: 'printdialog-option-enhance',
type: 'checkbox',
checked: true,
returnvalue: 'enhanced'
},
{
msgKey: 'printdialog-option-noimages',
type: 'checkbox',
checked: false,
returnvalue: 'noimages'
},
{
msgKey: 'printdialog-option-norefs',
type: 'checkbox',
checked: false,
returnvalue: 'norefs'
},
{
msgKey: 'printdialog-option-notoc',
type: 'checkbox',
checked: false,
returnvalue: 'notoc'
},
{
msgKey: 'printdialog-option-nobackground',
type: 'checkbox',
checked: false,
returnvalue: 'nobackground'
},
{
msgKey: 'printdialog-option-blacktext',
type: 'checkbox',
checked: true,
returnvalue: 'blacktext'
}
]
};
// Localization messages
mw.messages.set({
'printdialog-title': '打印此页面',
'printdialog-options': '打印选项',
'printdialog-action-print': '打印',
'printdialog-option-enhance': '隐藏界面元素',
'printdialog-option-noimages': '隐藏图像',
'printdialog-option-norefs': '隐藏引用',
'printdialog-option-notoc': '隐藏目录',
'printdialog-option-nobackground': '删除背景(您的浏览器可能会或可能不会覆盖此背景)',
'printdialog-option-blacktext': '强制所有文本为黑色'
});
// Run installation after page load
mw.hook('wikipage.content').add(function () {
if (mw.config.get('wgNamespaceNumber') >= 0) {
printOptions.install();
}
});
});
}());

})();

2025年11月1日 (六) 21:05的版本

(function () {
    'use strict';
    
    var windowManager;
    var printDialog;
    
    // Modernized configuration object
    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',
                        'mediawiki.api'
                    ]).then(printOptions.createWindow);
                    e.preventDefault();
                    e.stopPropagation();
                });
            
            // Preload resources
            mw.loader.load(['oojs-ui-core', 'oojs-ui-widgets', 'oojs-ui-windows']);
        },
        
        createWindow: function () {
            // Modern OOUI dialog implementation
            var PrintDialog = function (config) {
                PrintDialog.super.call(this, config);
            };
            
            OO.inheritClass(PrintDialog, OO.ui.ProcessDialog);
            
            PrintDialog.static.name = 'printdialog';
            PrintDialog.static.title = mw.message('printdialog-title').plain();
            PrintDialog.static.actions = [
                { 
                    action: 'print',
                    label: mw.message('printdialog-action-print').plain(),
                    flags: ['primary', 'progressive']
                },
                {
                    action: 'cancel',
                    label: mw.message('cancel').plain(),
                    flags: 'safe'
                }
            ];
            
            PrintDialog.prototype.initialize = function () {
                PrintDialog.super.prototype.initialize.apply(this, arguments);
                
                this.contentPanel = new OO.ui.PanelLayout({
                    padded: true,
                    expanded: false
                });
                
                this.fieldset = new OO.ui.FieldsetLayout({
                    label: mw.message('printdialog-options').plain()
                });
                
                // Dynamically create checkboxes from configuration
                printOptions.questions.forEach(function (question) {
                    if (question.type === 'checkbox') {
                        var checkbox = new OO.ui.CheckboxInputWidget({
                            selected: question.checked
                        });
                        
                        question.widget = checkbox;
                        
                        this.fieldset.addItems([
                            new OO.ui.FieldLayout(checkbox, {
                                label: mw.message(question.msgKey || question.label).plain(),
                                align: 'inline'
                            })
                        ]);
                    }
                }.bind(this));
                
                this.contentPanel.$element.append(this.fieldset.$element);
                this.$body.append(this.contentPanel.$element);
            };
            
            PrintDialog.prototype.getActionProcess = function (action) {
                var dialog = this;
                
                if (action === 'print') {
                    return new OO.ui.Process(function () {
                        // Gather all selected options
                        printOptions.questions.forEach(function (question) {
                            if (question.type === 'checkbox' && question.widget) {
                                printOptions[question.returnvalue] = question.widget.isSelected();
                            }
                        });
                        
                        return dialog.close({ action: action }).then(function () {
                            printOptions.prepareForPrinting();
                            window.print();
                            setTimeout(function () {
                                window.location.reload();
                            }, 500);
                        });
                    });
                }
                
                return PrintDialog.super.prototype.getActionProcess.call(this, action);
            };
            
            // Initialize window manager if needed
            if (!windowManager) {
                windowManager = new OO.ui.WindowManager();
                windowManager.$element.css('z-index', '2000');
                $(document.body).append(windowManager.$element);
            }
            
            // Create dialog if needed
            if (!printDialog) {
                printDialog = new PrintDialog({
                    size: 'medium',
                    classes: ['print-dialog']
                });
                
                windowManager.addWindows([printDialog]);
            }
            
            windowManager.openWindow(printDialog);
        },
        
        prepareForPrinting: function () {
            this.updatePrintStyles();
            this.applyCustomPrintStyles();
            this.enhancePrintFooter();
        },
        
        updatePrintStyles: function () {
            if (this.enhanced === false) return;
            
            Array.from(document.styleSheets).forEach(function (stylesheet) {
                try {
                    if (!stylesheet.media) return;
                    
                    // Handle media rules for stylesheets
                    var mediaText = stylesheet.media.mediaText || '';
                    if (mediaText.includes('print')) {
                        if (!mediaText.includes('screen')) {
                            stylesheet.disabled = true;
                        }
                    } else if (mediaText.includes('screen')) {
                        try {
                            stylesheet.media.appendMedium('print');
                        } catch (e) {
                            stylesheet.media.mediaText += ',print';
                        }
                    }
                    
                    // Handle media rules within stylesheets
                    var rules = stylesheet.cssRules || stylesheet.rules;
                    if (!rules) return;
                    
                    for (var i = 0; i < rules.length; i++) {
                        var rule = rules[i];
                        if (rule.type !== CSSRule.MEDIA_RULE || !rule.media) continue;
                        
                        var media = Array.from(rule.media);
                        if (media.includes('print') && !media.includes('screen')) {
                            stylesheet.deleteRule ? stylesheet.deleteRule(i) : stylesheet.removeRule(i);
                            i--;
                        } else if (media.includes('screen') && !media.includes('print')) {
                            try {
                                rule.media.appendMedium('print');
                            } catch (e) {
                                rule.media.mediaText += ',print';
                            }
                        }
                    }
                } catch (e) {
                    mw.log.warn('Could not process stylesheet:', e.message);
                }
            });
        },
        
        applyCustomPrintStyles: function () {
            var styles = [];
            
            if (this.noimages) {
                styles.push('img, .thumb, figure { display: none !important; }');
            }
            
            if (this.norefs) {
                styles.push('.references, .reference, .mw-references-wrap { display: none !important; }');
            }
            
            if (this.notoc) {
                styles.push('#toc, .toc { display: none !important; }');
            }
            
            if (this.nobackground) {
                styles.push('* { background: none !important; }');
                styles.push('body { background-color: white !important; }');
            }
            
            if (this.blacktext) {
                styles.push('* { color: black !important; }');
                styles.push('a { color: #0645ad !important; }');
            }
            
            if (styles.length) {
                var styleEl = document.createElement('style');
                styleEl.setAttribute('media', 'print');
                styleEl.textContent = styles.join('\n');
                document.head.appendChild(styleEl);
            }
        },
        
        enhancePrintFooter: function () {
            var printFooter = document.querySelector('.printfooter');
            if (printFooter) {
                var link = printFooter.querySelector('a');
                if (link) {
                    try {
                        link.textContent = decodeURIComponent(link.textContent);
                    } catch (e) {
                        mw.log.warn('Could not decode print footer URL:', e.message);
                    }
                }
            }
        },
        
        // Updated configuration with i18n message keys
        questions: [
            {
                msgKey: 'printdialog-option-enhance',
                type: 'checkbox',
                checked: true,
                returnvalue: 'enhanced'
            },
            {
                msgKey: 'printdialog-option-noimages',
                type: 'checkbox',
                checked: false,
                returnvalue: 'noimages'
            },
            {
                msgKey: 'printdialog-option-norefs',
                type: 'checkbox',
                checked: false,
                returnvalue: 'norefs'
            },
            {
                msgKey: 'printdialog-option-notoc',
                type: 'checkbox',
                checked: false,
                returnvalue: 'notoc'
            },
            {
                msgKey: 'printdialog-option-nobackground',
                type: 'checkbox',
                checked: false,
                returnvalue: 'nobackground'
            },
            {
                msgKey: 'printdialog-option-blacktext',
                type: 'checkbox',
                checked: true,
                returnvalue: 'blacktext'
            }
        ]
    };
    
    // Localization messages
    mw.messages.set({
        'printdialog-title': '打印此页面',
        'printdialog-options': '打印选项',
        'printdialog-action-print': '打印',
        'printdialog-option-enhance': '隐藏界面元素',
        'printdialog-option-noimages': '隐藏图像',
        'printdialog-option-norefs': '隐藏引用',
        'printdialog-option-notoc': '隐藏目录',
        'printdialog-option-nobackground': '删除背景(您的浏览器可能会或可能不会覆盖此背景)',
        'printdialog-option-blacktext': '强制所有文本为黑色'
    });
    
    // Run installation after page load
    mw.hook('wikipage.content').add(function () {
        if (mw.config.get('wgNamespaceNumber') >= 0) {
            printOptions.install();
        }
    });
}());