Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在chrome 85以下版本,子应用执行window.window报错 #280

Closed
jardenliu opened this issue Nov 23, 2022 · 3 comments
Closed

在chrome 85以下版本,子应用执行window.window报错 #280

jardenliu opened this issue Nov 23, 2022 · 3 comments
Labels
to do list to do list

Comments

@jardenliu
Copy link
Contributor

描述bug
在Chrome 85以及以下版本中,子应用执行 window.window报错。

如何复现
给出详细的复现步骤
1、打开Chrome 85以及以下版本浏览器
2、 打开无界在线预览页面 https://wujie-micro.github.io/doc/wujie/
3、打开控制台执行 window[0].__WUJIE.proxy.window 约等于 子应用执行window.window

错误截图
image

最小复现仓库或者地址
https://wujie-micro.github.io/doc/wujie/

@jardenliu
Copy link
Contributor Author

原因分析

无界的子应用运行环境使用Proxy对原有的window进行代理,window.window会触发Proxy下面的代码 L57

const proxyWindow = new Proxy(iframe.contentWindow, {
get: (target: Window, p: PropertyKey): any => {
// location进行劫持
if (p === "location") {
return target.__WUJIE.proxyLocation;
}
// 判断自身
if (p === "self" || p === "window") {
return target.__WUJIE.proxy;
}
// 不要绑定this
if (p === "__WUJIE_RAW_DOCUMENT_QUERY_SELECTOR__" || p === "__WUJIE_RAW_DOCUMENT_QUERY_SELECTOR_ALL__") {
return target[p];
}
// 修正this指针指向
return getTargetValue(target, p);
},
set: (target: Window, p: PropertyKey, value: any) => {
checkProxyFunction(value);
target[p] = value;
return true;
},
has: (target: Window, p: PropertyKey) => p in target,
});

通过判断self和window会返回 target.__WUJIE.proxy,来实现proxyWindow的返回

此时会触发Proxy的handler.get()的约束机制
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/get#invariants

85版本之前

window的PropertyDescriptor
image

configurable 和 writable 为false,会触发约束无法正确返回 target.__WUJIE.proxy

**85版本之后

window的PropertyDescriptor

image

此时获取方式变成了getter,不会触发约束,正常返回

@Emmally
Copy link

Emmally commented Mar 16, 2023

请问该问题是否可解?

@yiludege
Copy link
Collaborator

暂时可以用这个插件来操作一波 https://wujie-polyfill.github.io/doc/plugins/windowGetter.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
to do list to do list
Projects
None yet
Development

No branches or pull requests

3 participants