MediaWiki:Common.js
MediaWiki interface page
More actions
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* Any JavaScript here will be loaded for all users on every page load. */
// --- UploadWizard quick-skip hack ---
// Put this in MediaWiki:Common.js
(function() {
if (window.UPW_SKIP_INSTALL) return;
window.UPW_SKIP_INSTALL = true;
// Utility: hide element(s) containing given label text (case-insensitive)
function hideFieldsByLabelText(text) {
const txt = text.toLowerCase();
// Find labels, headings or containers that mention the text
const elems = Array.from(document.querySelectorAll('label, legend, h2, h3, .mwe-upwiz-field, .mwe-upwiz-section'));
elems.forEach(el => {
try {
if (el.textContent && el.textContent.toLowerCase().includes(txt)) {
// hide nearest sensible container
let container = el.closest('.mwe-upwiz-field') || el.closest('.mwe-upwiz-section') || el.parentElement;
if (container) container.style.display = 'none';
}
} catch(e) { /* ignore */ }
});
}
// Utility: remove "required" attributes and aria-required
function removeRequiredAttributes(root=document) {
const reqs = root.querySelectorAll('[required], [aria-required="true"]');
reqs.forEach(el => {
try {
el.removeAttribute('required');
el.removeAttribute('aria-required');
} catch(e){}
});
}
// Utility: try to auto-select an "own work" or similar license option
function autoSelectLicense(root=document) {
// try selects
const selects = root.querySelectorAll('select');
selects.forEach(sel => {
const options = Array.from(sel.options);
// prefer options that contain 'own', 'self', 'all rights', or 'copyright' (case-ins)
const match = options.find(o => /own|self|all rights|copyright/i.test(o.text));
if (match) sel.value = match.value;
});
// try radio buttons / labels
const labels = root.querySelectorAll('label');
labels.forEach(lbl => {
if (/own work|my own work|self|all rights reserved/i.test(lbl.textContent || '')) {
const forId = lbl.getAttribute('for');
if (forId) {
const el = document.getElementById(forId);
if (el && (el.type === 'radio' || el.type === 'checkbox')) el.checked = true;
} else {
// maybe the label wraps an input
const input = lbl.querySelector('input[type="radio"], input[type="checkbox"]');
if (input) input.checked = true;
}
}
});
}
// Called when UploadWizard UI appears / changes
function scrubUploadWizard(container) {
try {
// Remove required attrs
removeRequiredAttributes(container);
// Hide fields by their visible labels
['license', 'select a license', 'author', 'source', 'description', 'upload summary'].forEach(t => hideFieldsByLabelText(t));
// Auto-select a reasonable license option if present
autoSelectLicense(container);
// Also hide the license panel entirely if present
const licensePanels = container.querySelectorAll('.mwe-upwiz-licenses, .mwe-upwiz-license-list, .mwe-upwiz-license');
licensePanels.forEach(p => p.style.display = 'none');
// Hide any modal or step that looks like a tutorial
const tutorial = container.querySelector('.mwe-upwiz-tutorial, .mwe-upwiz-intro');
if (tutorial) tutorial.style.display = 'none';
} catch(e){ console.error('UPW-scrub error', e); }
}
// Observe the document for UploadWizard insertion
const observer = new MutationObserver((mutations) => {
mutations.forEach(m => {
m.addedNodes && m.addedNodes.forEach(node => {
if (!(node instanceof HTMLElement)) return;
// Detect UploadWizard root by class names or id patterns
if (node.matches && (node.matches('.mw-uploadwizard') || node.matches('.mwe-upwiz-dialog') || node.querySelector && node.querySelector('.mwe-upwiz'))) {
scrubUploadWizard(node);
}
// Also scrub if UploadWizard content appears deeper
const inner = node.querySelector && (node.querySelector('.mwe-upwiz') || node.querySelector('.mw-uploadwizard'));
if (inner) scrubUploadWizard(inner);
});
});
});
observer.observe(document.body, { childList: true, subtree: true });
// Defensive periodic scrub while UploadWizard is open (handles race conditions)
let periodic = setInterval(() => {
const upw = document.querySelector('.mwe-upwiz, .mw-uploadwizard, .mwe-upwiz-dialog');
if (upw) {
scrubUploadWizard(upw);
}
}, 400);
// Stop periodic scrub after a while to avoid perpetual work
setTimeout(() => clearInterval(periodic), 60 * 1000);
// Also run once now in case UploadWizard already present
document.addEventListener('DOMContentLoaded', () => {
const upw = document.querySelector('.mwe-upwiz, .mw-uploadwizard, .mwe-upwiz-dialog');
if (upw) scrubUploadWizard(upw);
});
})();