-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
278f754
commit 41ebf2e
Showing
15 changed files
with
116 additions
and
50 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,5 +9,6 @@ | |
<body> | ||
<div id="root"></div> | ||
<script type="module" src="./main.tsx"></script> | ||
<script></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
- window.postMessage中的window指的是你想发送跨域消息的那个窗口(你需要通信的目标窗口),而不是自身窗口的window | ||
|
||
- 父页面中:父页面向子页面发送跨域信息,window就是在父页面中嵌入的iframe指向的子页面的window,即:iFrame.contentWindow子页面中:子页面想父页面发送跨域信息 | ||
- window就是父页面的window,在这里因为子页面是嵌入到父页面中的,对于子页面来讲,window就是top或者parent | ||
- 需要等到iframe中的子页面加载完成后才发送消息,否则子页面接收不到消息在监听message事件时需要判断一下消息来源origin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,21 @@ | ||
class PostMessage { | ||
#iFrame: HTMLIFrameElement|null =null; | ||
#targetOrigin: string = ''; | ||
constructor(el:HTMLIFrameElement, targetOrigin: string) { | ||
this.#iFrame = el; | ||
this.#targetOrigin = targetOrigin; | ||
iFrame: Window |null =null; | ||
targetOrigin: string|undefined = ''; | ||
constructor(el:Window,targetOrigin?: string) { | ||
this.iFrame = el; | ||
this.targetOrigin =targetOrigin; | ||
window.addEventListener('message',this.handle,false) | ||
} | ||
sendMessage(message: any) { | ||
this.#iFrame!.contentWindow!.postMessage(message,this.#targetOrigin); | ||
send(message: any) { | ||
if(this.targetOrigin)this.iFrame!.postMessage(message,this.targetOrigin); | ||
else this.iFrame!.postMessage(message,'*'); | ||
} | ||
private handle(e: MessageEvent) { | ||
if(e.origin===this.#targetOrigin){ | ||
console.log(e.origin) //父页面URL,这里是http://a.index.com | ||
console.log(e.source) // 父页面window对象,全等于window.parent/window.top | ||
console.log(e.data) //父页面发送的消息 | ||
} | ||
private handle(e: MessageEvent) { | ||
if(e.data.source!="react-devtools-content-script")console.log(e); | ||
|
||
} | ||
destroy() { | ||
window.removeEventListener('message',this.handle,false) | ||
} | ||
} | ||
export default PostMessage; | ||
export default PostMessage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters