From ac1881632fe6bc8c4b019fd95e0a957a637c37d9 Mon Sep 17 00:00:00 2001 From: WhaleSong <32156806+Xusenbiao@users.noreply.github.com> Date: Tue, 27 Dec 2022 19:17:48 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=80=82=E9=85=8Ddomain=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=20(#310)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/wujie-core/src/iframe.ts | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/packages/wujie-core/src/iframe.ts b/packages/wujie-core/src/iframe.ts index b688c4e8a..fe446adcf 100644 --- a/packages/wujie-core/src/iframe.ts +++ b/packages/wujie-core/src/iframe.ts @@ -618,16 +618,31 @@ function initIframeDom(iframeWindow: Window, wujie: WuJie, mainHostPath: string, * 防止运行主应用的js代码,给子应用带来很多副作用 */ // TODO 更加准确抓取停止时机 -function stopIframeLoading(iframeWindow: Window, mainHostPath: string, appHostPath: string, appRoutePath: string) { - const sandbox = iframeWindow.__WUJIE; +function stopIframeLoading( + iframeWindow: Window, + mainHostPath: string, + appHostPath: string, + appRoutePath: string, + sandbox: WuJie +) { + const oldDoc = iframeWindow.document; sandbox.iframeReady = new Promise((resolve) => { function loop() { setTimeout(() => { - // location ready - if (iframeWindow.location.href === "about:blank") { + let newDoc = null; + try { + newDoc = iframeWindow.document; + } catch (err) { + newDoc = null; + } + // wait for document ready + if (!newDoc || newDoc == oldDoc) { loop(); } else { iframeWindow.stop ? iframeWindow.stop() : iframeWindow.document.execCommand("Stop"); + if (!iframeWindow.__WUJIE) { + patchIframeVariable(iframeWindow, sandbox, appHostPath); + } initIframeDom(iframeWindow, sandbox, mainHostPath, appHostPath); /** * 如果有同步优先同步,非同步从url读取 @@ -775,6 +790,6 @@ export function iframeGenerator( const iframeWindow = iframe.contentWindow; // 变量需要提前注入,在入口函数通过变量防止死循环 patchIframeVariable(iframeWindow, sandbox, appHostPath); - stopIframeLoading(iframeWindow, mainHostPath, appHostPath, appRoutePath); + stopIframeLoading(iframeWindow, mainHostPath, appHostPath, appRoutePath, sandbox); return iframe; }