User:CannonProductions/monacobookbeta.js: Difference between revisions
From MediaWiki
Jump to navigationJump to search
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
/* User:CannonProductions/monacobookbeta.js | /* User:CannonProductions/monacobookbeta.js | ||
- Dynamically scales #top-ad iframe banner to fit available width on mobile | - Dynamically scales #top-ad iframe banner to fit available width on mobile | ||
- | - Adds/removes html.tf-nav-open when the sidebar (#column-one) is open | ||
*/ | */ | ||
| Line 8: | Line 8: | ||
// === SETTINGS === | // === SETTINGS === | ||
var MOBILE_MAX_WIDTH = 720; | |||
var MOBILE_MAX_WIDTH = 720; | |||
// Banner creative’s native size | // Banner creative’s native size | ||
| Line 25: | Line 24: | ||
fn(); | fn(); | ||
} | } | ||
} | } | ||
| Line 94: | Line 78: | ||
} | } | ||
// ===== Nav | // ===== Nav open/closed detection ===== | ||
function isNavOpen() { | function isNavOpen() { | ||
var nav = document.getElementById("column-one"); | var nav = document.getElementById("column-one"); | ||
| Line 119: | Line 87: | ||
var r = nav.getBoundingClientRect(); | var r = nav.getBoundingClientRect(); | ||
return r.width > 40 && r.height > 40; | return r.width > 40 && r.height > 40; | ||
} | } | ||
function | function updateNavClass() { | ||
// Only care on mobile | |||
if (!isMobileWidth()) { | if (!isMobileWidth()) { | ||
document.documentElement.classList.remove("tf-nav-open"); | |||
return; | return; | ||
} | } | ||
if (isNavOpen()) { | |||
document.documentElement.classList.add("tf-nav-open"); | |||
} else { | |||
document.documentElement.classList.remove("tf-nav-open"); | |||
} | |||
} | } | ||
| Line 137: | Line 110: | ||
t = setTimeout(function () { | t = setTimeout(function () { | ||
rescaleBanner(); | rescaleBanner(); | ||
updateNavClass(); | |||
}, 60); | }, 60); | ||
} | } | ||
| Line 144: | Line 117: | ||
// Run once now | // Run once now | ||
rescaleBanner(); | rescaleBanner(); | ||
updateNavClass(); | |||
window.addEventListener("resize", scheduleAll); | window.addEventListener("resize", scheduleAll); | ||
window.addEventListener("orientationchange", scheduleAll); | window.addEventListener("orientationchange", scheduleAll); | ||
if (window.mw && mw.hook) { | if (window.mw && mw.hook) { | ||
mw.hook("wikipage.content").add(function () { | mw.hook("wikipage.content").add(function () { | ||
| Line 157: | Line 128: | ||
} | } | ||
// Watch for DOM changes (menu open/close | // Watch for DOM + style changes (menu open/close) | ||
var obs = new MutationObserver(function () { | var obs = new MutationObserver(function () { | ||
scheduleAll(); | scheduleAll(); | ||
}); | }); | ||
obs.observe(document.documentElement, { childList: true, subtree: true, attributes: true }); | obs.observe(document.documentElement, { | ||
childList: true, | |||
subtree: true, | |||
attributes: true | |||
}); | |||
// Extra safety: | // Extra safety: tapping hamburger triggers class update | ||
document.addEventListener("click", function () { | document.addEventListener("click", function () { | ||
setTimeout( | setTimeout(updateNavClass, 0); | ||
}); | }); | ||
} | } | ||
onReady(function () { | onReady(function () { | ||
installObservers(); | installObservers(); | ||
}); | }); | ||
})(); | })(); | ||
Revision as of 12:17, 30 December 2025
/* User:CannonProductions/monacobookbeta.js
- Dynamically scales #top-ad iframe banner to fit available width on mobile
- Adds/removes html.tf-nav-open when the sidebar (#column-one) is open
*/
(function () {
"use strict";
// === SETTINGS ===
var MOBILE_MAX_WIDTH = 720;
// 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 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 open/closed detection =====
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();
return r.width > 40 && r.height > 40;
}
function updateNavClass() {
// Only care on mobile
if (!isMobileWidth()) {
document.documentElement.classList.remove("tf-nav-open");
return;
}
if (isNavOpen()) {
document.documentElement.classList.add("tf-nav-open");
} else {
document.documentElement.classList.remove("tf-nav-open");
}
}
// ===== Plumbing =====
var t = null;
function scheduleAll() {
if (t) clearTimeout(t);
t = setTimeout(function () {
rescaleBanner();
updateNavClass();
}, 60);
}
function installObservers() {
// Run once now
rescaleBanner();
updateNavClass();
window.addEventListener("resize", scheduleAll);
window.addEventListener("orientationchange", scheduleAll);
if (window.mw && mw.hook) {
mw.hook("wikipage.content").add(function () {
scheduleAll();
});
}
// Watch for DOM + style changes (menu open/close)
var obs = new MutationObserver(function () {
scheduleAll();
});
obs.observe(document.documentElement, {
childList: true,
subtree: true,
attributes: true
});
// Extra safety: tapping hamburger triggers class update
document.addEventListener("click", function () {
setTimeout(updateNavClass, 0);
});
}
onReady(function () {
installObservers();
});
})();

