User:CannonProductions/monacobookbeta.js

From MediaWiki
Revision as of 11:43, 30 December 2025 by CannonProductions (talk | contribs)
Jump to navigationJump to search

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.
/* User:CannonProductions/monacobookbeta.js
   - Dynamically scales #top-ad iframe banner to fit available width on mobile
   - Locks background scroll when the sidebar (#column-one) is open on mobile
*/

(function () {
  "use strict";

  // === SETTINGS ===
  var DEBUG_BADGE = false;         // keep false (you already confirmed it runs)
  var MOBILE_MAX_WIDTH = 720;      // rules apply on screens <= this width

  // Banner creative’s native size
  var BASE_W = 468;
  var BASE_H = 60;

  // Banner scale limits
  var MIN_SCALE = 0.55;
  var MAX_SCALE = 1.0;

  function onReady(fn) {
    if (document.readyState === "loading") {
      document.addEventListener("DOMContentLoaded", fn);
    } else {
      fn();
    }
  }

  function addBadge() {
    if (!DEBUG_BADGE) return;
    if (!document.body) return;
    if (document.getElementById("js-test-badge")) return;

    var d = document.createElement("div");
    d.id = "js-test-badge";
    d.textContent = "User JS RUNNING";
    d.style.cssText =
      "position:fixed;top:0;left:0;z-index:999999;" +
      "padding:6px 10px;background:#e11;color:#fff;" +
      "font:12px sans-serif";
    document.body.prepend(d);
  }

  function isMobileWidth() {
    return window.matchMedia("(max-width: " + MOBILE_MAX_WIDTH + "px)").matches;
  }

  // ===== Banner scaling =====
  function resetBannerStyles(ad, iframe) {
    if (ad) {
      ad.style.height = "";
      ad.style.overflow = "";
      ad.style.width = "";
    }
    if (iframe) {
      iframe.style.transform = "";
      iframe.style.transformOrigin = "";
      iframe.style.width = "";
      iframe.style.height = "";
      iframe.style.border = "";
      iframe.style.display = "";
    }
  }

  function rescaleBanner() {
    var ad = document.getElementById("top-ad");
    if (!ad) return;

    var iframe = ad.querySelector("iframe");
    if (!iframe) return;

    if (!isMobileWidth()) {
      resetBannerStyles(ad, iframe);
      return;
    }

    iframe.style.width = BASE_W + "px";
    iframe.style.height = BASE_H + "px";
    iframe.style.border = "0";
    iframe.style.display = "block";
    iframe.style.transformOrigin = "top left";

    var available = ad.getBoundingClientRect().width || window.innerWidth;

    var scale = available / BASE_W;
    if (scale > MAX_SCALE) scale = MAX_SCALE;
    if (scale < MIN_SCALE) scale = MIN_SCALE;

    iframe.style.transform = "scale(" + scale.toFixed(4) + ")";

    ad.style.width = "100%";
    ad.style.overflow = "hidden";
    ad.style.height = (BASE_H * scale) + "px";
  }

  // ===== Nav scroll locking =====
  // MonacoBookBeta toggles #column-one visibility; when it’s visible on mobile, stop the page behind from scrolling.
  function setScrollLocked(locked) {
    // Use a data flag so we don’t fight other styles
    if (locked) {
      document.documentElement.style.overflow = "hidden";
      document.body.style.overflow = "hidden";
      document.body.setAttribute("data-nav-locked", "1");
    } else {
      if (document.body.getAttribute("data-nav-locked") === "1") {
        document.documentElement.style.overflow = "";
        document.body.style.overflow = "";
        document.body.removeAttribute("data-nav-locked");
      }
    }
  }

  function isNavOpen() {
    var nav = document.getElementById("column-one");
    if (!nav) return false;

    var cs = window.getComputedStyle(nav);
    if (!cs || cs.display === "none" || cs.visibility === "hidden") return false;

    var r = nav.getBoundingClientRect();
    // If it has some real onscreen area, treat it as open
    return r.width > 40 && r.height > 40;
  }

  function updateNavLock() {
    if (!isMobileWidth()) {
      setScrollLocked(false);
      return;
    }
    setScrollLocked(isNavOpen());
  }

  // ===== Plumbing =====
  var t = null;
  function scheduleAll() {
    if (t) clearTimeout(t);
    t = setTimeout(function () {
      rescaleBanner();
      updateNavLock();
    }, 60);
  }

  function installObservers() {
    // Run once now
    rescaleBanner();
    updateNavLock();

    // Re-run on resize/rotate
    window.addEventListener("resize", scheduleAll);
    window.addEventListener("orientationchange", scheduleAll);

    // MediaWiki dynamic content hook
    if (window.mw && mw.hook) {
      mw.hook("wikipage.content").add(function () {
        scheduleAll();
      });
    }

    // Watch for DOM changes (menu open/close often changes styles/DOM)
    var obs = new MutationObserver(function () {
      scheduleAll();
    });
    obs.observe(document.documentElement, { childList: true, subtree: true, attributes: true });

    // Extra safety: click anywhere updates lock (covers purely-CSS toggles)
    document.addEventListener("click", function () {
      setTimeout(updateNavLock, 0);
    });
  }

  onReady(function () {
    addBadge();
    installObservers();
  });
})();