簡體   English   中英

從dart 2調用javascript函數,該函數作為參數傳遞

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

我們如何在飛鏢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"); });

responseCallback("world"); 從dart調用,它不會將響應發送回發件人。

我已經實現了一個示例,該示例能夠在事件被觸發時從事件中讀取參數,但是由於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

絆了一會兒后,我在stackoverflow上發現了關於chrome.runtime.onMessage問題:

Chrome擴展程序消息傳遞:未發送響應

chrome.runtime.onMessage.addListener

當事件偵聽器返回時,該函數將變為無效,除非您從事件偵聽器返回true表示您希望異步發送響應(這將使消息通道保持另一端打開,直到調用sendResponse為止)。

那些對使用dart js互操作實現事件偵聽器(“與上述相似”)感到好奇的人可以簽出此倉庫:

https://github.com/drbcode/dart_event_listener_example

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM