Chrome扩展程序中window.postMessage的安全性

时间:2018-12-28 03:12:11

标签: security google-chrome-extension postmessage

官方文档建议使用windows.postMessage在内容脚本和本地页面脚本之间进行通信:
https://developer.chrome.com/extensions/content_scripts#host-page-communication

document.getElementById("theButton").addEventListener("click",
    function() {
  window.postMessage({ type: "FROM_PAGE", text: "Hello from the webpage!" }, "*");
}, false);

但来自MDN:
https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

不建议使用“ *”作为目标,也不要在从某个扩展名接收到消息时定义“源”(并且原始文档正在使用源来检测是否是当前窗口)发送消息)

对此我有些困惑,使用window.postMessage在内容脚本和本地页面脚本之间进行通信是一种好习惯吗?
如果是的话,什么是安全的方法?

0 个答案:

没有答案