File: //proc/thread-self/cwd/js/iframe-resizer-helper.js
(function () {
let lastHeight = 0;
let animationFrameId = null;
function sendHeightToParent(height) {
window.parent.postMessage({
type: 'ysnet-iframe-resize',
height: height
}, window.location.origin);
}
function trackHeightContinuously() {
const newHeight = document.body.scrollHeight;
if (newHeight !== lastHeight) {
lastHeight = newHeight;
sendHeightToParent(newHeight);
}
animationFrameId = requestAnimationFrame(trackHeightContinuously);
}
function startTracking() {
cancelAnimationFrame(animationFrameId);
animationFrameId = requestAnimationFrame(trackHeightContinuously);
}
// 確保 document.body 存在才執行
if (document.readyState === "complete" || document.readyState === "interactive") {
startTracking();
} else {
window.addEventListener("DOMContentLoaded", startTracking);
}
// 若有頁面跳轉到同一個 iframe 中的其他頁面,也重新啟動追蹤
window.addEventListener("load", () => {
setTimeout(startTracking, 200); // 預留一下 DOM 建立完成
});
// 提供外部手動呼叫
window.notifyParentOfHeightChange = () => {
sendHeightToParent(document.body.scrollHeight);
};
})();