【问题标题】:Calling a javascript function from dart 2, that is passed as a parameter从 dart 2 调用 javascript 函数,该函数作为参数传递
【发布时间】:2018-08-10 02:12:20
【问题描述】:

我们如何在 dart 2 中实现这一点?

content_script.js:

chrome.runtime.sendMessage("hello", function(response) { console.log(response); });

背景.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

【问题讨论】:

    标签: dart dart-js-interop


    【解决方案1】:

    在绊了一会之后,我在 stackoverflow 上发现了这个关于 chrome.runtime.onMessage 的内容:

    Chrome Extension Message passing: response not sent

    chrome.runtime.onMessage.addListener:

    当事件侦听器返回时,此函数无效,除非您从事件侦听器返回 true 以指示您希望异步发送响应(这将保持消息通道对另一端开放,直到调用 sendResponse)。

    那些对使用 dart js 互操作实现事件监听器(“类似于上面”)感到好奇的人可以查看这个 repo:

    https://github.com/drbcode/dart_event_listener_example

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-13
      • 2019-08-08
      • 2012-10-28
      相关资源
      最近更新 更多