User:CannonProductions/monacobookbeta.js: Difference between revisions

From MediaWiki
Jump to navigationJump to search
No edit summary
No edit summary
Line 1: Line 1:
var d = document.createElement("div");
/* User:CannonProductions/monacobookbeta.js
d.id = "js-test-badge";
  - Shows optional "User JS RUNNING" badge (DEBUG_BADGE)
d.textContent = "User JS RUNNING";
  - Dynamically scales #top-ad iframe banner to fit available width on mobile
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 () {
   const BASE_W = 468;
  "use strict";
   const BASE_H = 60;
 
  // === SETTINGS ===
  var DEBUG_BADGE = true;          // set to false once you’ve confirmed scripts run
  var MOBILE_MAX_WIDTH = 720;      // only scale on screens <= this width
   var BASE_W = 468;               // banner’s native width
   var BASE_H = 60;                 // banner’s native height
  var MIN_SCALE = 0.55;            // don’t shrink below this (avoid ultra-tiny)
  var MAX_SCALE = 1.0;            // don’t upscale above 100%
 
  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;
  }
 
  function resetBannerStyles(ad, iframe) {
    // Return things to normal if you rotate to desktop width
    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() {
   function rescaleBanner() {
     const ad = document.getElementById("top-ad");
     var ad = document.getElementById("top-ad");
     if (!ad) return;
     if (!ad) return;


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


     const available = ad.getBoundingClientRect().width || window.innerWidth;
     // Only do the scaling on mobile widths
    if (!isMobileWidth()) {
      resetBannerStyles(ad, iframe);
      return;
    }
 
    // Ensure predictable base sizing, then scale visually
    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";
 
    // Available width = the ad container’s width (fallback to viewport)
    var available = ad.getBoundingClientRect().width || window.innerWidth;


     let scale = available / BASE_W;
     // Compute scale
     if (scale > 1) scale = 1;       // don’t upscale past 100%
    var scale = available / BASE_W;
     if (scale < 0.5) scale = 0.5;   // safety floor for tiny widths
     if (scale > MAX_SCALE) scale = MAX_SCALE;
     if (scale < MIN_SCALE) scale = MIN_SCALE;


     document.documentElement.style.setProperty("--tfwiki-banner-scale", scale.toFixed(4));
     // Apply scale
    iframe.style.transform = "scale(" + scale.toFixed(4) + ")";


     // Make layout height match the scaled visual height
     // IMPORTANT: make the container’s layout height match scaled banner height
    ad.style.width = "100%";
    ad.style.overflow = "hidden";
     ad.style.height = (BASE_H * scale) + "px";
     ad.style.height = (BASE_H * scale) + "px";
   }
   }


   // Run now + on resize (debounced)
   // Debounced resize handler (prevents it thrashing)
   let t;
   var t = null;
   function onResize() {
   function scheduleRescale() {
     clearTimeout(t);
     if (t) clearTimeout(t);
     t = setTimeout(rescaleBanner, 50);
     t = setTimeout(rescaleBanner, 60);
   }
   }


   window.addEventListener("load", rescaleBanner, { once: true });
   function installObservers() {
  window.addEventListener("resize", onResize);
    // Run now (in case #top-ad already exists)
  window.addEventListener("orientationchange", onResize);
    rescaleBanner();
 
    // Re-run on resize / rotate
    window.addEventListener("resize", scheduleRescale);
    window.addEventListener("orientationchange", scheduleRescale);
 
    // MediaWiki sometimes swaps content dynamically; hook if available
    if (window.mw && mw.hook) {
      mw.hook("wikipage.content").add(function () {
        scheduleRescale();
      });
    }
 
    // Watch for late insertion of #top-ad / iframe
    var obs = new MutationObserver(function () {
      scheduleRescale();
    });
    obs.observe(document.documentElement, { childList: true, subtree: true });
  }


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

Revision as of 11:33, 30 December 2025

/* User:CannonProductions/monacobookbeta.js
   - Shows optional "User JS RUNNING" badge (DEBUG_BADGE)
   - Dynamically scales #top-ad iframe banner to fit available width on mobile
*/

(function () {
  "use strict";

  // === SETTINGS ===
  var DEBUG_BADGE = true;          // set to false once you’ve confirmed scripts run
  var MOBILE_MAX_WIDTH = 720;      // only scale on screens <= this width
  var BASE_W = 468;                // banner’s native width
  var BASE_H = 60;                 // banner’s native height
  var MIN_SCALE = 0.55;            // don’t shrink below this (avoid ultra-tiny)
  var MAX_SCALE = 1.0;             // don’t upscale above 100%

  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;
  }

  function resetBannerStyles(ad, iframe) {
    // Return things to normal if you rotate to desktop width
    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;

    // Only do the scaling on mobile widths
    if (!isMobileWidth()) {
      resetBannerStyles(ad, iframe);
      return;
    }

    // Ensure predictable base sizing, then scale visually
    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";

    // Available width = the ad container’s width (fallback to viewport)
    var available = ad.getBoundingClientRect().width || window.innerWidth;

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

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

    // IMPORTANT: make the container’s layout height match scaled banner height
    ad.style.width = "100%";
    ad.style.overflow = "hidden";
    ad.style.height = (BASE_H * scale) + "px";
  }

  // Debounced resize handler (prevents it thrashing)
  var t = null;
  function scheduleRescale() {
    if (t) clearTimeout(t);
    t = setTimeout(rescaleBanner, 60);
  }

  function installObservers() {
    // Run now (in case #top-ad already exists)
    rescaleBanner();

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

    // MediaWiki sometimes swaps content dynamically; hook if available
    if (window.mw && mw.hook) {
      mw.hook("wikipage.content").add(function () {
        scheduleRescale();
      });
    }

    // Watch for late insertion of #top-ad / iframe
    var obs = new MutationObserver(function () {
      scheduleRescale();
    });
    obs.observe(document.documentElement, { childList: true, subtree: true });
  }

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