I am currently developing a Chrome extension using Google Dart. Everything works fine except for onMessage processing.
Used Chrome package: API Ref. ~ OnMessageEvent Ref.
In the background, the 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 said in the OnMessageEvent Ref.)
return true;
//...
}
return false;
}
My popup scripts look like this:
//...
chrome.runtime.sendMessage({
'action': "refresh"
}).then((_){
print('receiving response!');
});
//...
What works : A “Refresh” message is sent from the script popup to the background script! Background <script printsRefresh called!
What does not work : A pop-up script never gets a response elicited from the background - script: new JsObject(response, [new JsObject.jsify({'done': true})]);.
, sendResponse, . , JsObject ? , - .
(, )
github.com/dart-gde/chrome.dart.