Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions packages/wujie-core/src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
},
});
}
Expand Down