有一个新的API用于网页安全地进行通信,没有任何副作用(window.postMessage可以有其他侦听器!)内容脚本。
在网页上,使用runtime.sendMessage或runtime.connectAPI向特定的应用程序或扩展发送消息// The ID of the extension we want to talk to.var editorExtensionId = "abcdefghijklmnoabcdefhijklmnoabc";
// Make a simple request:chrome.runtime.sendMessage(editorExtensionId, {openUrlInEditor: url},
function(response) {
if (!response.success)
handleError(url);});
从您的应用程序或扩展中,您可以通过runtime.onMessageExond或runtime.onConnectExternalAPI(类似于跨扩展消息传递)收听来自网页的消息。只有网页才能启动连接。[.]
别问我这是怎么回事,这看起来很让人困惑。运行时是如何在网页上定义的?如果脚本由于某种原因已经定义了该变量,该怎么办?我也找不到铬缺陷报告,以了解这一功能的发展历史。