PitchIn / embed-resize.js
Hayeonbang's picture
Match research page styling and support responsive public embedding
d7fa4ed verified
Raw History Blame Contribute Delete
1.64 kB
() => {
const allowedParents = new Set([
"https://hayeonbang.github.io",
"http://127.0.0.1:8765",
"http://localhost:8765"
]);
let parentOrigin;
try { parentOrigin = new URL(document.referrer).origin; }
catch (_) { return; }
if (window.parent === window || !allowedParents.has(parentOrigin)) return;
// Gradio calls this after mounting; clean up if its app is mounted again.
window.__pitchinEmbedCleanup?.();
let root;
let timer;
let lastHeight = 0;
const report = () => {
clearTimeout(timer);
timer = setTimeout(() => {
const nextRoot = document.getElementById("component-0");
if (nextRoot !== root) {
if (root) sizeObserver.unobserve(root);
root = nextRoot;
if (root) sizeObserver.observe(root);
}
if (!root) return;
// Measure content rather than iframe/body height to avoid a resize loop.
const height = Math.ceil(root.getBoundingClientRect().bottom + window.scrollY + 4);
if (height <= 0 || height === lastHeight) return;
lastHeight = height;
window.parent.postMessage({ type: "pitchin-embed-height", height }, parentOrigin);
}, 60);
};
const sizeObserver = new ResizeObserver(report);
const contentObserver = new MutationObserver(report);
contentObserver.observe(document.body, { childList: true, subtree: true });
window.addEventListener("resize", report);
document.fonts?.ready.then(report);
window.__pitchinEmbedCleanup = () => {
clearTimeout(timer);
sizeObserver.disconnect();
contentObserver.disconnect();
window.removeEventListener("resize", report);
};
report();
}