From 628cf3c7f978f304f1af370e6ec76c793e051032 Mon Sep 17 00:00:00 2001 From: WhaleSong <1196032724@qq.com> Date: Sun, 4 Jun 2023 20:16:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=BD=93=E5=AD=90=E5=BA=94=E7=94=A8loca?= =?UTF-8?q?tion.replace=E5=88=B0=E5=85=B6=E4=BB=96=E5=9F=9F=E5=90=8D?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E6=94=B9=E4=B8=BA=E6=B8=B2=E6=9F=93=E5=88=B0?= =?UTF-8?q?=E5=8F=A6=E4=B8=80=E4=B8=AAiframe=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/wujie-core/src/proxy.ts | 38 +++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/packages/wujie-core/src/proxy.ts b/packages/wujie-core/src/proxy.ts index 0abe359f0..ab296741b 100644 --- a/packages/wujie-core/src/proxy.ts +++ b/packages/wujie-core/src/proxy.ts @@ -13,23 +13,37 @@ import { stopMainAppRun, } from "./utils"; -/** - * location href 的set劫持操作 - */ -function locationHrefSet(iframe: HTMLIFrameElement, value: string, appHostPath: string): boolean { - const { shadowRoot, id, degrade, document, degradeAttrs } = iframe.contentWindow.__WUJIE; - let url = value; +function generateUrl(url: string, appHostPath: string) { + let newUrl = url; if (!/^http/.test(url)) { let hrefElement = anchorElementGenerator(url); - url = appHostPath + hrefElement.pathname + hrefElement.search + hrefElement.hash; + newUrl = appHostPath + hrefElement.pathname + hrefElement.search + hrefElement.hash; hrefElement = null; } + return newUrl; +} + +function renderInIframe(iframe: HTMLIFrameElement, url: string) { + const { shadowRoot, id, degrade, degradeAttrs } = iframe.contentWindow.__WUJIE; + if (degrade) { + renderIframeReplaceApp(window.decodeURIComponent(url), getDegradeIframe(id).parentElement, degradeAttrs); + return; + } + renderIframeReplaceApp(url, shadowRoot.host.parentElement, degradeAttrs); +} + +/** + * location href 的set劫持操作 + */ +function locationHrefSet(iframe: HTMLIFrameElement, value: string, appHostPath: string): boolean { + const { id, degrade, document } = iframe.contentWindow.__WUJIE; iframe.contentWindow.__WUJIE.hrefFlag = true; if (degrade) { const iframeBody = rawDocumentQuerySelector.call(iframe.contentDocument, "body"); renderElementToContainer(document.documentElement, iframeBody); - renderIframeReplaceApp(window.decodeURIComponent(url), getDegradeIframe(id).parentElement, degradeAttrs); - } else renderIframeReplaceApp(url, shadowRoot.host.parentElement, degradeAttrs); + } + const url = generateUrl(value, appHostPath); + renderInIframe(iframe, url); pushUrlToWindow(id, url); return true; } @@ -220,7 +234,11 @@ export function proxyGenerator( if (propKey === "replace") { return new Proxy(location[propKey], { apply(replace, _ctx, args) { - return replace.call(location, args[0]?.replace(appHostPath, mainHostPath)); + if (args[0]?.includes?.(appHostPath)) { + return replace.call(location, args[0]?.replace(appHostPath, mainHostPath)); + } + const url = generateUrl(args[0], appHostPath); + return renderInIframe(iframe, url); }, }); }