2017-07-25 110 views
0

那么我的问题是很像Proper way to listen as TCP server in Chrome extension制作Chrome应用与Chrome浏览器扩展互动

但问题是,上面的问题没有正确回答和问题仍然存在。

那么,有没有办法让Chrome应用程序与Chrome扩展程序交互。目的非常类似于上面引用的问题,在恢复中它用于构建TCP服务器并管理标签网页中的内容。

据我所知,没有办法做到这一点,因为Chrome应用程序无法访问网页内容,并且Chrome扩展程序不允许使用套接字。

因此,欢迎任何想法。提前致谢。

+2

https://developer.chrome.com/extensions/messaging#external –

回答

0

尽管被称为"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清单键(默认为宽容)过滤掉其余部分。

相关问题