MediaWiki:Common.js: Difference between revisions

No edit summary
Tag: Replaced
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/* 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();
  }
})();