简体   繁体   English

从dart 2调用javascript函数,该函数作为参数传递

[英]Calling a javascript function from dart 2, that is passed as a parameter

How can we implement this in dart 2? 我们如何在飞镖2中实现这一点?

content_script.js: content_script.js:

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

backgroud.js: backgroud.js:

chrome.runtime.onMessage.addListener(function(a,b, responseCallback){ responseCallback("world"); });

When responseCallback("world"); responseCallback("world"); is called from dart it doesn't send the response back to sender. 从dart调用,它不会将响应发送回发件人。

I already have implemented an example that is able to read parameters from events when they are fired but it is not able to send a response because responseCallback is passed as a parameter : https://gitlab.com/drbcode/chrome.dart/blob/master/examples/runtime_test/lib/runtime.dart#L46 我已经实现了一个示例,该示例能够在事件被触发时从事件中读取参数,但是由于responseCallback作为参数传递而无法发送响应: https : //gitlab.com/drbcode/chrome.dart/blob /master/examples/runtime_test/lib/runtime.dart#L46

API docs: API文档:

https://developer.chrome.com/apps/runtime#method-sendMessage https://developer.chrome.com/apps/runtime#event-onMessage https://developer.chrome.com/apps/runtime#method-sendMessage https://developer.chrome.com/apps/runtime#event-onMessage

After stumbling for a while I found this about chrome.runtime.onMessage on stackoverflow: 绊了一会儿后,我在stackoverflow上发现了关于chrome.runtime.onMessage问题:

Chrome Extension Message passing: response not sent Chrome扩展程序消息传递:未发送响应

chrome.runtime.onMessage.addListener : chrome.runtime.onMessage.addListener

This function becomes invalid when the event listener returns, unless you return true from the event listener to indicate you wish to send a response asynchronously (this will keep the message channel open to the other end until sendResponse is called). 当事件侦听器返回时,该函数将变为无效,除非您从事件侦听器返回true表示您希望异步发送响应(这将使消息通道保持另一端打开,直到调用sendResponse为止)。

Those who are curious about implementing an event listener("similar to above") using dart js interop can check out this repo: 那些对使用dart js互操作实现事件侦听器(“与上述相似”)感到好奇的人可以签出此仓库:

https://github.com/drbcode/dart_event_listener_example https://github.com/drbcode/dart_event_listener_example

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM