MediaWiki:Mobile.js

Revision as of 17:42, 17 October 2025 by Wikilah admin (talk | contribs) (Created page with "All JavaScript here will be loaded for users of the mobile site: // Back to top (CapSach) — all skins; mobile widths (function () { var ID = 'cps-backtotop'; function ensureButton() { if (document.getElementById(ID)) return; var btn = document.createElement('button'); btn.id = ID; btn.type = 'button'; btn.setAttribute('aria-label', 'Back to top'); btn.title = 'Back to top'; btn.innerHTML = '<span aria-hidden="true">↑</span>';...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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.
/* All JavaScript here will be loaded for users of the mobile site */

// Back to top (CapSach) — all skins; mobile widths
(function () {
  var ID = 'cps-backtotop';

  function ensureButton() {
    if (document.getElementById(ID)) return;

    var btn = document.createElement('button');
    btn.id = ID;
    btn.type = 'button';
    btn.setAttribute('aria-label', 'Back to top');
    btn.title = 'Back to top';
    btn.innerHTML = '<span aria-hidden="true">↑</span>';

    document.body.appendChild(btn);
    updateVisibility();

    var prefersReducedMotion = false;
    try {
      prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
    } catch (e) {}

    btn.addEventListener('click', function () {
      try {
        window.scrollTo({ top: 0, behavior: prefersReducedMotion ? 'auto' : 'smooth' });
      } catch (e) {
        // Older browsers
        window.scrollTo(0, 0);
      }
    });

    window.addEventListener('scroll', onScroll, { passive: true });
    window.addEventListener('resize', onScroll, { passive: true });
  }

  var ticking = false;
  function onScroll() {
    if (!ticking) {
      window.requestAnimationFrame(function () {
        updateVisibility();
        ticking = false;
      });
      ticking = true;
    }
  }

  function updateVisibility() {
    var show = window.pageYOffset > 300 &&
               document.documentElement.scrollHeight > window.innerHeight * 2;
    var el = document.getElementById(ID);
    if (el) el.classList.toggle('is-visible', show);
  }

  // Run on initial load
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', ensureButton);
  } else {
    ensureButton();
  }

  // Also run when MediaWiki re-initializes page content (preview, some SPA-ish flows)
  if (window.mw && mw.hook) {
    mw.hook('wikipage.content').add(ensureButton);
  }
})();