User:CannonProductions/monacobookbeta.js
From MediaWiki
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
- 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 = false; // 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();
});
})();

