【问题标题】:Making Chrome App interact with Chrome Extensions使 Chrome 应用程序与 Chrome 扩展程序交互
【发布时间】:2017-07-25 22:28:01
【问题描述】:
【问题讨论】:
标签:
javascript
google-chrome
google-chrome-extension
【解决方案1】:
尽管被称为"cross-extension messaging",但它可以在扩展程序和应用程序之间工作。
大概,你会想要长期的连接;那么你可以使用:
// In one app/extension that initiates the connection
// (Probably the app upon a new client connecting)
var port = chrome.runtime.connect(secondID);
// In other app/extension
chrome.runtime.onConnectExternal.addListener(function(port) {
port.onMessage.addListener(function(msg) {
// See other examples for sample onMessage handlers.
});
});
请记住,您要确保连接来自您信任的东西。虽然防御不完善,但您应该检查发起者的扩展程序/应用程序 ID - 无论是在您的代码中还是通过使用 externally_connectable 清单键(默认为许可)过滤掉其余部分。