wps加载项打开的对话框页面与主程序之间通信
1.如果是同源页面在主程序中使用
const channel = new BroadcastChannel('myChannel') //js与页面间的通信
channel.postMessage('Hello from Tab 1!');
对话框页面:
const channel = new BroadcastChannel('myChannel');
channel.onmessage = function(event) {
console.log("接收到消息", e.data);
};
- 如果是非同源页面,则需要在对话框页面再使用iframe嵌入,使用postMessage通信:
<iframe src="http://localhost:8001/#/test/pagePostMessage" frameborder="0" id="myIframe"></iframe>
const channel = new BroadcastChannel('myChannel');
channel.onmessage = function(event) {
console.log("接收到消息", e.data);
let iframe = document.getElementById("myIframe");
iframe.contentWindow.postMessage("Hello from parent!", "http://localhost:8001");
};
嵌入页:
window.addEventListener("message", (e) => {
console.log("接收到消息1", e.data);
});