MediaWiki:Common.js: Difference between revisions

No edit summary
No edit summary
Line 2: Line 2:




document.addEventListener("DOMContentLoaded", () => {
(function () {
   const iframe = document.querySelector('iframe[key="wikiphone"]');
   function findIframe() {
  if (!iframe) return;
    return (
      document.querySelector('iframe[key="wikiphone"]') ||
      Array.from(document.querySelectorAll('iframe')).find(el =>
        (el.getAttribute('src') || '').includes('/jonesfamilyfarmsrecordings')
      )
    );
  }


   iframe.style.display = "block";
   function setH(iframe, h) {
  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");
 
  const setH = (h) => {
     const height = Math.max(200, Math.floor(h || 0));
     const height = Math.max(200, Math.floor(h || 0));
     if (iframe.style.height !== height + "px") {
     if (iframe.style.height !== height + 'px') {
       iframe.style.height = height + "px";
       iframe.style.height = height + 'px';
     }
     }
   };
   }


   function trySameOriginWiring() {
   function wireSameOrigin(iframe) {
     try {
     try {
       const doc = iframe.contentDocument || iframe.contentWindow?.document;
       const doc = iframe.contentDocument || iframe.contentWindow?.document;
       if (!doc) return false; // not ready yet / cross-origin
       if (!doc) return false;


       const ro = new ResizeObserver(() => {
       const measure = () => {
         const b = doc.body;
         const b = doc.body, e = doc.documentElement;
        const e = doc.documentElement;
         const h = Math.max(
         const h = Math.max(
           b.scrollHeight,
           b.scrollHeight, b.offsetHeight,
          b.offsetHeight,
           e.clientHeight, e.scrollHeight, e.offsetHeight
           e.clientHeight,
          e.scrollHeight,
          e.offsetHeight
         );
         );
         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);


       const kick = () => {
       iframe.addEventListener('load', measure);
        const b = doc.body;
       window.addEventListener('resize', measure);
        const e = doc.documentElement;
       measure();
        const h = Math.max(
          b.scrollHeight,
          b.offsetHeight,
          e.clientHeight,
          e.scrollHeight,
          e.offsetHeight
        );
        setH(h);
       };
      iframe.addEventListener("load", kick, { once: true });
      kick();
 
       window.addEventListener("resize", kick);
       return true;
       return true;
     } catch (e) {
     } catch {
       return false;
       return false;
     }
     }
   }
   }


   function wireCrossOriginMessaging() {
   function wireCrossOrigin(iframe) {
     window.addEventListener("message", (event) => {
     window.addEventListener('message', (event) => {
       const data = event.data;
       const d = event.data;
       if (!data || typeof data !== "object") return;
       if (!d || typeof d !== 'object') return;
       if (data.type === "wikiphone:height" && typeof data.height === "number") {
       if (d.type === 'wikiphone:height' && typeof d.height === 'number') {
         setH(data.height);
         setH(iframe, d.height);
       }
       }
     });
     });


     const pingChild = () => {
     const ping = () => {
       iframe.contentWindow?.postMessage({ type: "wikiphone:getHeight" }, "*");
       try { iframe.contentWindow?.postMessage({ type: 'wikiphone:getHeight' }, '*'); } catch {}
     };
     };


     iframe.addEventListener("load", () => {
     iframe.addEventListener('load', () => {
       setH(400); // give it a starter height so page isn’t tiny
       setH(iframe, 400);
       let count = 0;
       let n = 0;
       const t = setInterval(() => {
       const t = setInterval(() => {
         pingChild();
         ping();
         if (++count >= 10) clearInterval(t);
         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 });
   }
   }


   const wired = trySameOriginWiring();
   if (document.readyState === 'loading') {
   if (!wired) wireCrossOriginMessaging();
    document.addEventListener('DOMContentLoaded', boot);
});
   } else {
    boot();
  }
})();