首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >调用dart 2中的javascript函数,该函数作为参数传递

调用dart 2中的javascript函数,该函数作为参数传递
EN

Stack Overflow用户
提问于 2018-08-10 10:12:20
回答 1查看 96关注 0票数 0

我们如何在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

EN

回答 1

Stack Overflow用户

发布于 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://github.com/drbcode/dart_event_listener_example

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51777975

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档