企业网站建设费现金流科目企业信息查询网
企业网站建设费现金流科目,企业信息查询网,广州大型网站设计公司,仙居网站设计为什么要进行消息通信#xff1f; content_script和top窗口之间除了DOM共享之外#xff0c;window对象是不共享的。如果content_script需要获得top窗口中window对象的数据#xff0c;就需要使用到通信。反之#xff0c;也是相同的情况。
1、自定义监听事件#xff08;推荐…为什么要进行消息通信 content_script和top窗口之间除了DOM共享之外window对象是不共享的。如果content_script需要获得top窗口中window对象的数据就需要使用到通信。反之也是相同的情况。
1、自定义监听事件推荐
// 广播浏览器原生网页top位置进行消息广播// 发送广播: top
let customEvent new CustomEvent(my-message-type, { details: this is a message!})
window.dispatchEvent(customEvent)// 消息监听content_script内容脚本进行监听window.addEventListener(my-message-type, (e) {console.log(e)console.log(e?.details) // this is a message!// do something})
2、监听广播事件 // 发送广播消息top
window.postMessage({type: my-message-type, data: message!}, *)// 监听消息content_script
window.addEventListener(message, (e) {console.log(e:, e)console.log(e.data:, e?.data)console.log(e.data.type:, e?.data?.type)console.log(e.data.data:, e?.data?.data)if (e?.data?.type my-message-type) {// do something}
})
不推荐使用window.postMessage进行消息广播 来源CSDN论坛评论https://blog.csdn.net/dongzi_yu/article/details/128441466#comments_28957465
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/88798.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!