Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

MediaWiki:Common.js: Difference between revisions

MediaWiki interface page
Wikiadmin (talk | contribs)
Created page with "→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 =..."
 
Wikiadmin (talk | contribs)
Tag: Replaced
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/* 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);
  });
})();

Revision as of 18:14, 20 October 2025

/* Any JavaScript here will be loaded for all users on every page load. */