我们如何在dart 2中实现这一点?
content_script.js:
chrome.runtime.sendMessage("hello", function(response) { console.log(response); });
backgroud.js:
chrome.runtime.onMessage.addListener(function(a,b, responseCallback){ responseCallback("world"); });
当从dart调用responseCallback("world");
时,它不会将响应发送回发送者。
我已经实现了一个示例,它能够在触发事件时从事件中读取参数,但不能发送响应,因为responseCallback
是作为参数传递的:https://gitlab.com/drbcode/chrome.dart/blob/master/examples/runtime_test/lib/runtime.dart#L46
API文档:
https://developer.chrome.com/apps/runtime#method-sendMessage https://developer.chrome.com/apps/runtime#event-onMessage
发布于 2018-08-12 06:00:03
在跌跌撞撞了一段时间后,我在stackoverflow上发现了关于chrome.runtime.onMessage
的以下内容:
Chrome Extension Message passing: response not sent
chrome.runtime.onMessage.addListener
当事件侦听器返回时,此函数将无效,除非您从事件侦听器返回true以指示您希望异步发送响应(这将使消息通道保持对另一端的开放,直到调用sendResponse为止)。
那些对使用dart js interop实现事件侦听器(“类似于上面的”)感兴趣的人可以查看这个repo:
https://stackoverflow.com/questions/51777975
复制相似问题