Jump to content

MediaWiki:Common.js: Difference between revisions

From Doom Relics
No edit summary
No edit summary
Line 41: Line 41:
   obs.observe(document.documentElement, { childList: true, subtree: true });
   obs.observe(document.documentElement, { childList: true, subtree: true });
})();
})();
// DOOM ARCHIVES header (Vector 2022): robust, survives re-renders
// DOOM ARCHIVES header: inject above your Doom Titles links (guaranteed)
(function () {
(function () {
   const HEADER_CLASS = 'doom-archives-header';
   const HEADER_CLASS = 'doom-archives-header';


   function styleHeader(header) {
   function styleHeader(header) {
    // Inline styles (no dependency on Common.css)
     header.style.display = 'block';
     header.style.display = 'block';
     header.style.width = '100%';
     header.style.width = '100%';
Line 63: Line 62:
   }
   }


   function findMenuContentContainer() {
   function findAnchor() {
     // Prefer Vector 2022 pinned container
     // Use a link you KNOW exists in your sidebar
     const pinned =
     return (
       document.querySelector('#vector-main-menu-pinned-container nav.vector-main-menu .vector-menu-content') ||
      document.querySelector('a[href*="Category:Doom_(1993)"]') ||
       document.querySelector('#vector-main-menu-pinned-container .vector-menu-content');
       document.querySelector('a[href*="Category:Doom_II"]') ||
     if (pinned) return pinned;
      document.querySelector('a[href*="Category:Doom_3"]') ||
       document.querySelector('a[href*="Category:Doom_Eternal"]')
     );
  }


    // Non-pinned Vector 2022 menu (some states)
  function ensureHeader() {
     const main =
     const a = findAnchor();
      document.querySelector('nav.vector-main-menu .vector-menu-content') ||
     if (!a) return;
      document.querySelector('#vector-main-menu .vector-menu-content');
     if (main) return main;


     // Logged-out dropdown menu content (hamburger)
     // Find the menu content container that holds the link list
     const dropdown =
     const menuContent =
       document.querySelector('#vector-main-menu-dropdown .vector-menu-content') ||
       a.closest('.vector-menu-content') ||
       document.querySelector('#vector-main-menu-dropdown .vector-dropdown-content');
       a.closest('ul') ||
    if (dropdown) return dropdown;
      a.parentElement;


    // Legacy fallback
     if (!menuContent) return;
    const legacy = document.querySelector('#mw-panel .vector-menu-content');
     if (legacy) return legacy;


     return null;
     // Avoid duplicates
  }
     if (menuContent.querySelector('.' + HEADER_CLASS)) return;
 
  function ensureHeader() {
    const container = findMenuContentContainer();
     if (!container) return;
 
    // If already exists, just ensure it’s styled & visible
    let existing = container.querySelector('.' + HEADER_CLASS);
    if (existing) {
      existing.style.display = 'block';
      styleHeader(existing);
      return;
    }


     const header = document.createElement('div');
     const header = document.createElement('div');
Line 106: Line 92:
     styleHeader(header);
     styleHeader(header);


     container.insertBefore(header, container.firstChild);
     // Insert header before the first list item / first link area
    menuContent.insertBefore(header, menuContent.firstChild);
   }
   }


   function start() {
   function run() {
     ensureHeader();
     ensureHeader();
    // Re-run a few times during init (Vector can rebuild the menu)
     setTimeout(ensureHeader, 150);
     setTimeout(ensureHeader, 150);
     setTimeout(ensureHeader, 600);
     setTimeout(ensureHeader, 600);
     setTimeout(ensureHeader, 1200);
     setTimeout(ensureHeader, 1200);
  }


    // Keep it enforced if DOM changes
  // Run now + keep alive if Vector re-renders
    const obs = new MutationObserver(() => ensureHeader());
  run();
    obs.observe(document.documentElement, { childList: true, subtree: true });
  window.addEventListener('load', run);
  }


   // Wait until Vector signals it’s ready (when possible)
   const obs = new MutationObserver(() => ensureHeader());
  if (document.documentElement.classList.contains('vector-animations-ready')) {
  obs.observe(document.documentElement, { childList: true, subtree: true });
    start();
  } else {
    const readyObs = new MutationObserver(() => {
      if (document.documentElement.classList.contains('vector-animations-ready')) {
        readyObs.disconnect();
        start();
      }
    });
    readyObs.observe(document.documentElement, { attributes: true, attributeFilter: ['class'] });
    // Also start on load as a fallback
    window.addEventListener('load', start);
  }
})();
})();
document.body.insertAdjacentHTML(
  'afterbegin',
  '<div style="position:fixed;top:0;left:0;right:0;z-index:999999;background:#b30000;color:#000;padding:8px 12px;font-weight:900;letter-spacing:.12em;text-transform:uppercase;text-align:center;">COMMON.JS LOADED</div>'
);

Revision as of 16:28, 24 December 2025

/* Any JavaScript here will be loaded for all users on every page load. */
// Vector 2022: pin main menu for logged-out users on desktop
if (!mw.config.get('wgUserName') && window.matchMedia('(min-width: 1120px)').matches) {
  const observer = new MutationObserver((_, obs) => {
    if (document.documentElement.classList.contains('vector-animations-ready')) {
      const pin = document.querySelector('[data-event-name="pinnable-header.vector-main-menu.pin"]');
      if (pin) pin.click();
      obs.disconnect();
    }
  });
  observer.observe(document.documentElement, { attributes: true, attributeFilter: ['class'] });
}
// Remove "Main menu" headings/labels injected by Vector/Vector 2022 (works across variants)
(function () {
  function killMainMenuText(root = document) {
    // Look for common heading/label elements
    const candidates = root.querySelectorAll(
      '#vector-main-menu, #vector-main-menu-pinned-container, #mw-panel, nav.vector-main-menu, #vector-main-menu-dropdown'
    );

    candidates.forEach(container => {
      // Remove elements whose visible text is exactly "Main menu"
      container.querySelectorAll('*').forEach(el => {
        const txt = (el.textContent || '').trim();
        if (txt === 'Main menu') {
          el.style.display = 'none';
        }
        // Also catch the longer hint text variants
        if (txt.toLowerCase().includes('main menu has moved here')) {
          el.style.display = 'none';
        }
      });
    });
  }

  // Run now, on load, and after Vector finishes UI init
  killMainMenuText();
  window.addEventListener('load', () => killMainMenuText());

  const obs = new MutationObserver(() => killMainMenuText());
  obs.observe(document.documentElement, { childList: true, subtree: true });
})();
// DOOM ARCHIVES header: inject above your Doom Titles links (guaranteed)
(function () {
  const HEADER_CLASS = 'doom-archives-header';

  function styleHeader(header) {
    header.style.display = 'block';
    header.style.width = '100%';
    header.style.boxSizing = 'border-box';
    header.style.margin = '10px 0 10px';
    header.style.padding = '12px 14px';
    header.style.background = 'rgba(0,0,0,0.75)';
    header.style.border = '1px solid #3a0000';
    header.style.borderRadius = '12px';
    header.style.boxShadow = 'inset 0 0 20px rgba(180,0,0,0.30), 0 0 8px rgba(0,0,0,0.8)';
    header.style.color = '#b30000';
    header.style.fontWeight = '900';
    header.style.letterSpacing = '0.14em';
    header.style.textTransform = 'uppercase';
    header.style.textAlign = 'center';
  }

  function findAnchor() {
    // Use a link you KNOW exists in your sidebar
    return (
      document.querySelector('a[href*="Category:Doom_(1993)"]') ||
      document.querySelector('a[href*="Category:Doom_II"]') ||
      document.querySelector('a[href*="Category:Doom_3"]') ||
      document.querySelector('a[href*="Category:Doom_Eternal"]')
    );
  }

  function ensureHeader() {
    const a = findAnchor();
    if (!a) return;

    // Find the menu content container that holds the link list
    const menuContent =
      a.closest('.vector-menu-content') ||
      a.closest('ul') ||
      a.parentElement;

    if (!menuContent) return;

    // Avoid duplicates
    if (menuContent.querySelector('.' + HEADER_CLASS)) return;

    const header = document.createElement('div');
    header.className = HEADER_CLASS;
    header.textContent = 'DOOM ARCHIVES';
    styleHeader(header);

    // Insert header before the first list item / first link area
    menuContent.insertBefore(header, menuContent.firstChild);
  }

  function run() {
    ensureHeader();
    setTimeout(ensureHeader, 150);
    setTimeout(ensureHeader, 600);
    setTimeout(ensureHeader, 1200);
  }

  // Run now + keep alive if Vector re-renders
  run();
  window.addEventListener('load', run);

  const obs = new MutationObserver(() => ensureHeader());
  obs.observe(document.documentElement, { childList: true, subtree: true });
})();