MediaWiki:Common.js: Difference between revisions

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




(function () {
(function(){
   function findIframe() {
   const SEL='#Iframe1,iframe[data-src*="/jonesfamilyfarmsrecordings"],iframe[src*="/jonesfamilyfarmsrecordings"]';
    return (
   function setH(i,h){const px=Math.max(200,Math.floor(h||0))+'px';if(i.style.height!==px)i.style.height=px;i.removeAttribute('height');i.setAttribute('scrolling','no');}
      document.querySelector('iframe[key="wikiphone"]') ||
   function measure(i){try{const d=i.contentDocument||i.contentWindow?.document;if(!d)return false;const f=()=>{const b=d.body,e=d.documentElement;const h=Math.max(b.scrollHeight,b.offsetHeight,e.clientHeight,e.scrollHeight,e.offsetHeight);setH(i,h);};const r=new ResizeObserver(f);r.observe(d.documentElement);r.observe(d.body);i.addEventListener('load',f);window.addEventListener('resize',f);f();return true;}catch{return false;}}
      Array.from(document.querySelectorAll('iframe')).find(el =>
   function cross(i){window.addEventListener('message',e=>{const d=e.data;if(!d||typeof d!=='object')return;if(d.type==='wikiphone:height'&&typeof d.height==='number')setH(i,d.height);});const ping=()=>{try{i.contentWindow?.postMessage({type:'wikiphone:getHeight'},'*');}catch{}};i.addEventListener('load',()=>{setH(i,400);let n=0;const t=setInterval(()=>{ping();if(++n>=20)clearInterval(t);},500);});setInterval(ping,1500);}
        (el.getAttribute('src') || '').includes('/jonesfamilyfarmsrecordings')
   function style(i){i.style.display='block';i.style.width='100vw';i.style.maxWidth='100%';i.style.marginLeft='calc(-50vw + 50%)';i.style.border='0';i.style.overflow='hidden';i.setAttribute('scrolling','no');i.removeAttribute('height');i.removeAttribute('width');}
      )
   function wire(i){if(i.__wired)return;i.__wired=true;style(i);const mo=new MutationObserver(()=>{if(i.getAttribute('src')||i.getAttribute('data-src'))style(i);});mo.observe(i,{attributes:true});const ok=measure(i);if(!ok)cross(i);let t=0;const tick=setInterval(()=>{style(i);if(i.getAttribute('src'))clearInterval(tick);if(++t>60)clearInterval(tick);},250);}
    );
  function find(){document.querySelectorAll(SEL).forEach(wire);}
  }
   if(document.readyState==='loading'){document.addEventListener('DOMContentLoaded',find);}else{find();}
 
   const obs=new MutationObserver(find);obs.observe(document.documentElement,{childList:true,subtree:true});
   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();
   }
})();
})();