Dart chrome扩展:如何在onMessage发送响应? [英] Dart chrome extension: How to send a response in onMessage?
问题描述
目前我正在使用Google的Dart开发Chrome扩充功能。
使用的Chrome软件包: API参考〜 OnMessageEvent参考
在后台脚本中我调用: / p>
import'package:chrome / chrome_ext.dart'as chrome;
void main(){
// ...
// Binde事件监听器
chrome.runtime.onMessage.listen(onMessage) ;
// ...
}
/ **
*消息事件处理
* /
bool onMessage(chrome.OnMessageEvent messageEvent ){
JsObject message = messageEvent.message;
JsFunction response = messageEvent.sendResponse;
switch(message ['action']){
// ...
//由弹出式窗口使用
case' refresh':
print('Refresh called!');
chrome.storage.sync.get()。then((_){
new JsObject(response,[new JsObject.jsify({'done':true})]);
});
//请求异步发送响应(所以在OnMessageEvent参考中说)
return true;
// ...
}
return false;
}
我的弹出式脚本如下所示:
chrome.runtime.sendMessage({
'action':refresh);
})。然后((_){
print('receive response!');
});
// ...
工作:
消息refresh从弹出脚本发送到后台脚本工程!
后台脚本打印刷新调用!
无效:
弹出窗口脚本从不会收到从后台脚本调用的响应: new JsObject(response,[new JsObject.jsify({'done':true})] / code>。
我不知道如何处理sendResponse以真正发送响应。也许我的JsObject调用是错误的?希望别人能帮忙。
(Btw。也是非异步响应在这里不起作用)
上 github.com/dart-gde/chrome.dart 。您的问题可能是您尝试将回复传回的方式:
b
new JsObject(response,[new JsObject.jsify({'done':true})]);
根据 dart.js docs,函数应该在以下情况下调用:
< pre class =lang-dart prettyprint-override>
response.apply([new JsObject.jsify({'done':true})]);
更新:这里的工作代码:
background.dart
import dart: js';
void main(){
print(main()...);
context ['chrome'] ['runtime'] ['onMessage']。callMethod('addListener',[onMessageListener]);
}
void onMessageListener(request,sender,sendResponse){
print(received action = $ {request ['action']});
sendResponse.apply([new JsObject.jsify({'done':true})]);
}
popup.dart
< pre class =lang-dart prettyprint-override>
import'dart:js';
void main(){
print(main():...);
context ['chrome'] ['runtime']。callMethod('sendMessage',[new JsObject.jsify({'action':'refresh'}),(response)=> print {response ['done']})]);
}
更新:使它在JavaScript中工作
如注释中所述,从 context ['chrome'] ['runtime '] ['onMessage']
...如果不是 JsObject
,应该使用 new JsObject.fromBrowserObject()
var jsObject = context ['chrome '] ['runtime'] ['onMessage'];
(jsObject is JsObject?jsObject:new JsObject.fromBrowserObject(jsObject))
.callMethod('addListener',[onMessageListener]);
希望它有帮助; - )
right now I'm developing a Chrome extension with Google's Dart. All works fine, except the onMessage-handling.
Chrome-Package used: API Ref. ~ OnMessageEvent Ref.
In the background-script i'm calling:
import 'package:chrome/chrome_ext.dart' as chrome;
void main() {
//...
//Binde event-listeners
chrome.runtime.onMessage.listen(onMessage);
//...
}
/**
* Message event handling
*/
bool onMessage(chrome.OnMessageEvent messageEvent){
JsObject message = messageEvent.message;
JsFunction response = messageEvent.sendResponse;
switch(message['action']){
//...
//Used by popup
case 'refresh':
print('Refresh called!');
chrome.storage.sync.get().then((_){
new JsObject(response, [new JsObject.jsify({'done': true})]);
});
//Idicate to send a response asynchronously (So it's said in the OnMessageEvent Ref.)
return true;
//...
}
return false;
}
My popup-scripts looks like this:
//...
chrome.runtime.sendMessage({
'action': "refresh"
}).then((_){
print('receiving response!');
});
//...
What is working:
The message 'refresh' send from popup-script to background-script works!
The background-script prints Refresh called!
What is not working:
The popup-script never receives the response called from the background-script: new JsObject(response, [new JsObject.jsify({'done': true})]);
.
I don't know how to handel sendResponse to really send an response. Maybe my call with JsObject is wrong? Hope someone else can help. (Btw. also a non async-response doesn't work here)
This problem is also open on github.com/dart-gde/chrome.dart.
Your problem might be in a way how you try to send the response back:
new JsObject(response, [new JsObject.jsify({'done': true})]);
According to the dart.js docs, the function should in your case be invoked like:
response.apply([new JsObject.jsify({'done': true})]);
Update: here the working code:
background.dart
import 'dart:js';
void main() {
print("main()...");
context['chrome']['runtime']['onMessage'].callMethod('addListener', [onMessageListener]);
}
void onMessageListener(request, sender, sendResponse) {
print("received action = ${request['action']}");
sendResponse.apply([new JsObject.jsify({'done': true})]);
}
popup.dart
import 'dart:js';
void main() {
print("main():...");
context['chrome']['runtime'].callMethod('sendMessage', [new JsObject.jsify({'action': 'refresh'}), (response) => print("received ${response['done']}")]);
}
Update: required patch to make it working in JavaScript too
As mentioned in comments, there is a problem with the returned object from context['chrome']['runtime']['onMessage']
... If it is not JsObject
it should be wrapped into it by using new JsObject.fromBrowserObject()
var jsObject = context['chrome']['runtime']['onMessage'];
(jsObject is JsObject ? jsObject : new JsObject.fromBrowserObject(jsObject))
.callMethod('addListener', [onMessageListener]);
Hope it helps ;-)
这篇关于Dart chrome扩展:如何在onMessage发送响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!