MediaWiki:Common.js: Difference between revisions
Jump to navigation
Jump to search
CaptainChris (talk | contribs) No edit summary |
CaptainChris (talk | contribs) No edit summary |
||
| Line 2: | Line 2: | ||
(function () { | |||
function findIframe() { | |||
return ( | |||
document.querySelector('iframe[key="wikiphone"]') || | |||
Array.from(document.querySelectorAll('iframe')).find(el => | |||
(el.getAttribute('src') || '').includes('/jonesfamilyfarmsrecordings') | |||
) | |||
); | |||
} | |||
function setH(iframe, h) { | |||
const height = Math.max(200, Math.floor(h || 0)); | const height = Math.max(200, Math.floor(h || 0)); | ||
if (iframe.style.height !== height + | if (iframe.style.height !== height + 'px') { | ||
iframe.style.height = height + | iframe.style.height = height + 'px'; | ||
} | } | ||
} | } | ||
function | function wireSameOrigin(iframe) { | ||
try { | try { | ||
const doc = iframe.contentDocument || iframe.contentWindow?.document; | const doc = iframe.contentDocument || iframe.contentWindow?.document; | ||
if (!doc) return false; | if (!doc) return false; | ||
const | const measure = () => { | ||
const b = doc.body | const b = doc.body, e = doc.documentElement; | ||
const h = Math.max( | const h = Math.max( | ||
b.scrollHeight, | b.scrollHeight, b.offsetHeight, | ||
e.clientHeight, e.scrollHeight, e.offsetHeight | |||
e.clientHeight, | |||
); | ); | ||
setH(h); | setH(iframe, h); | ||
}); | }; | ||
const ro = new ResizeObserver(measure); | |||
ro.observe(doc.documentElement); | ro.observe(doc.documentElement); | ||
ro.observe(doc.body); | ro.observe(doc.body); | ||
iframe.addEventListener('load', measure); | |||
window.addEventListener('resize', measure); | |||
measure(); | |||
return true; | return true; | ||
} catch | } catch { | ||
return false; | return false; | ||
} | } | ||
} | } | ||
function | function wireCrossOrigin(iframe) { | ||
window.addEventListener( | window.addEventListener('message', (event) => { | ||
const | const d = event.data; | ||
if (! | if (!d || typeof d !== 'object') return; | ||
if ( | if (d.type === 'wikiphone:height' && typeof d.height === 'number') { | ||
setH( | setH(iframe, d.height); | ||
} | } | ||
}); | }); | ||
const | const ping = () => { | ||
iframe.contentWindow?.postMessage({ type: | try { iframe.contentWindow?.postMessage({ type: 'wikiphone:getHeight' }, '*'); } catch {} | ||
}; | }; | ||
iframe.addEventListener( | iframe.addEventListener('load', () => { | ||
setH(400); | setH(iframe, 400); | ||
let | let n = 0; | ||
const t = setInterval(() => { | const t = setInterval(() => { | ||
ping(); | |||
if (++ | if (++n >= 20) clearInterval(t); | ||
}, 500); | }, 500); | ||
}); | }); | ||
setInterval(ping, 1500); | |||
} | |||
function styleIframe(iframe) { | |||
iframe.style.display = 'block'; | |||
iframe.style.width = '100vw'; | |||
iframe.style.maxWidth = '100%'; | |||
iframe.style.marginLeft = 'calc(-50vw + 50%)'; | |||
iframe.style.border = 'none'; | |||
iframe.style.overflow = 'hidden'; | |||
iframe.setAttribute('scrolling', 'no'); | |||
} | |||
function initWith(iframe) { | |||
if (!iframe.__wikiphoneWired) { | |||
iframe.__wikiphoneWired = true; | |||
styleIframe(iframe); | |||
const ok = wireSameOrigin(iframe); | |||
if (!ok) wireCrossOrigin(iframe); | |||
} | |||
} | |||
function boot() { | |||
const existing = findIframe(); | |||
if (existing) initWith(existing); | |||
const mo = new MutationObserver(() => { | |||
const el = findIframe(); | |||
if (el) initWith(el); | |||
}); | |||
mo.observe(document.documentElement, { childList: true, subtree: true }); | |||
} | } | ||
if (document.readyState === 'loading') { | |||
document.addEventListener('DOMContentLoaded', boot); | |||
}); | } else { | ||
boot(); | |||
} | |||
})(); | |||
Revision as of 21:29, 10 November 2025
/* Any JavaScript here will be loaded for all users on every page load. */
(function () {
function findIframe() {
return (
document.querySelector('iframe[key="wikiphone"]') ||
Array.from(document.querySelectorAll('iframe')).find(el =>
(el.getAttribute('src') || '').includes('/jonesfamilyfarmsrecordings')
)
);
}
function setH(iframe, h) {
const height = Math.max(200, Math.floor(h || 0));
if (iframe.style.height !== height + 'px') {
iframe.style.height = height + 'px';
}
}
function wireSameOrigin(iframe) {
try {
const doc = iframe.contentDocument || iframe.contentWindow?.document;
if (!doc) return false;
const measure = () => {
const b = doc.body, e = doc.documentElement;
const h = Math.max(
b.scrollHeight, b.offsetHeight,
e.clientHeight, e.scrollHeight, e.offsetHeight
);
setH(iframe, h);
};
const ro = new ResizeObserver(measure);
ro.observe(doc.documentElement);
ro.observe(doc.body);
iframe.addEventListener('load', measure);
window.addEventListener('resize', measure);
measure();
return true;
} catch {
return false;
}
}
function wireCrossOrigin(iframe) {
window.addEventListener('message', (event) => {
const d = event.data;
if (!d || typeof d !== 'object') return;
if (d.type === 'wikiphone:height' && typeof d.height === 'number') {
setH(iframe, d.height);
}
});
const ping = () => {
try { iframe.contentWindow?.postMessage({ type: 'wikiphone:getHeight' }, '*'); } catch {}
};
iframe.addEventListener('load', () => {
setH(iframe, 400);
let n = 0;
const t = setInterval(() => {
ping();
if (++n >= 20) clearInterval(t);
}, 500);
});
setInterval(ping, 1500);
}
function styleIframe(iframe) {
iframe.style.display = 'block';
iframe.style.width = '100vw';
iframe.style.maxWidth = '100%';
iframe.style.marginLeft = 'calc(-50vw + 50%)';
iframe.style.border = 'none';
iframe.style.overflow = 'hidden';
iframe.setAttribute('scrolling', 'no');
}
function initWith(iframe) {
if (!iframe.__wikiphoneWired) {
iframe.__wikiphoneWired = true;
styleIframe(iframe);
const ok = wireSameOrigin(iframe);
if (!ok) wireCrossOrigin(iframe);
}
}
function boot() {
const existing = findIframe();
if (existing) initWith(existing);
const mo = new MutationObserver(() => {
const el = findIframe();
if (el) initWith(el);
});
mo.observe(document.documentElement, { childList: true, subtree: true });
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', boot);
} else {
boot();
}
})();