-
(function() { var minH = 50; var maxH = 30000; // Capture the specific iframe for this script instance synchronously var scripts = document.getElementsByTagName('script'); var currentScript = document.currentScript || scripts[scripts.length - 1]; var wrapper = currentScript; while (wrapper && (!wrapper.classList || !wrapper.classList.contains('raw-html-embed-wrapper'))) { wrapper = wrapper.parentNode; } if (!wrapper) return; var iframe = wrapper.querySelector('.raw-html-embed'); if (!iframe || iframe._rawHtmlResizerAttached) return; iframe._rawHtmlResizerAttached = true; function setupScrollCoordination(d) { if (iframe._scrollCoordinationAttached) return; iframe._scrollCoordinationAttached = true; // 1. In-page anchors (like href="#app"): scroll both iframe and parent page smoothly d.addEventListener('click', function(e) { var a = e.target.closest && e.target.closest('a[href^="#"]'); if (!a) return; var hash = a.getAttribute('href'); if (!hash || hash === '#') return; var id = decodeURIComponent(hash.slice(1)); var target = d.getElementById(id) || d.querySelector(hash); if (!target) return; e.preventDefault(); target.scrollIntoView({ behavior: 'smooth', block: 'start' }); try { var rect = iframe.getBoundingClientRect(); if (Math.abs(rect.top) > 5) { window.scrollBy({ top: rect.top, behavior: 'smooth' }); } } catch (err) {} }); // 2. Desktop Mouse Wheel / Trackpad coordination d.addEventListener('wheel', function(e) { var rect = iframe.getBoundingClientRect(); var iframeScroll = (d.documentElement.scrollTop || d.body.scrollTop || (iframe.contentWindow && iframe.contentWindow.scrollY) || 0); // If parent header is still visible above iframe, scroll the parent page first if (rect.top > 1 && e.deltaY > 0) { window.scrollBy({ top: e.deltaY, behavior: 'auto' }); e.preventDefault(); return; } // If at top of iframe and rolling wheel up, scroll parent page up if (iframeScroll = maxScroll - 2 && e.deltaY > 0) { window.scrollBy({ top: e.deltaY, behavior: 'auto' }); e.preventDefault(); return; } }, { passive: false }); // 3. Mobile touch coordination (smooth, no frame-by-frame jank) var touchStartY = 0; var isInteractive = false; try { d.addEventListener('touchstart', function(e) { if (e.touches && e.touches.length) { touchStartY = e.touches[0].clientY; isInteractive = !!(e.target.closest && e.target.closest('a, button, input, select, textarea, label, [role="button"]')); } }, { passive: true }); d.addEventListener('touchmove', function(e) { if (isInteractive || !e.touches || !e.touches.length) return; var currentY = e.touches[0].clientY; var diffY = touchStartY - currentY; var rect = iframe.getBoundingClientRect(); // If iframe is not at top yet and user swipes to scroll down, smoothly dock it if (rect.top > 15 && diffY > 10) { window.scrollBy({ top: rect.top, behavior: 'smooth' }); e.preventDefault(); return; } var iframeScroll = (d.documentElement.scrollTop || d.body.scrollTop || (iframe.contentWindow && iframe.contentWindow.scrollY) || 0); // If at top of iframe and pulling down if (diffY < -15 && iframeScroll 15 && iframeScroll >= maxScroll - 5) { window.scrollBy({ top: 150, behavior: 'smooth' }); return; } }, { passive: false }); } catch (e) {} } function updateHeight() { try { var d = iframe.contentDocument || iframe.contentWindow.document; if (!d || !d.body) return; var meta = d.querySelector('meta[name="embed-height"]'); if (meta && meta.content) { iframe.style.height = meta.content; iframe.style.overflow = 'auto'; iframe.removeAttribute('scrolling'); setupScrollCoordination(d); return; } if (d.body.style.height && d.body.style.height.indexOf('%') === -1) { iframe.style.height = d.body.style.height; return; } // Reset height so the iframe can shrink if the window got narrower iframe.style.height = minH + 'px'; var h = Math.max( d.body.scrollHeight, d.body.offsetHeight, d.documentElement.scrollHeight, d.documentElement.offsetHeight ); if (h > 0) { iframe.style.height = Math.min(Math.max(h, minH), maxH) + 'px'; } } catch (err) {} } iframe.addEventListener('load', function() { updateHeight(); try { var d = iframe.contentDocument || iframe.contentWindow.document; if (window.ResizeObserver && d && d.body) { new ResizeObserver(updateHeight).observe(d.body); } } catch (e) {} }); // Fallback in case it's already loaded (e.g. bfcache or fast render) if (iframe.contentDocument && iframe.contentDocument.readyState === 'complete') { updateHeight(); try { if (window.ResizeObserver && iframe.contentDocument && iframe.contentDocument.body) { new ResizeObserver(updateHeight).observe(iframe.contentDocument.body); } } catch (e) {} } })();
Comment