3

To better describe my problem i have created a small example of a chrome extension written in Dart. You can see the code or download the extension on Gist.

The problem

This example is running fine in Dartium, but when compiled to javascript a typeerror occurs: Uncaught TypeError: undefined is not a function for the line:

context['chrome']['runtime']['onMessage'].callMethod('addListener', [onMessageListener]);

How far i already am

  • As you may see in the example the functions alert() or console.log() via dart:js are also working in the js-extension. So it could be a special problem with dart2js and adding eventlisteners?
  • Also printing out context['chrome']['runtime']['onMessage'] shows the right event-object. (E.g.: context['console'].callMethod('log', [context['chrome']['runtime']['onMessage']]);)
  • I know that there exist an chrome pub package, but there is still a bug when responding to received messages in onMessage. See also this question. Using the chrome api directly via dart:js was the workaround which was fine at that dart version.

I played a lot with the code but all results in the same error. Now i am out of ideas. Hope the community can help me again.

Edit: I have now reported this bug on dartbug.com as Robert suggested. Anyway, I'm still open for a workaround or something if someone know one.

Community
  • 1
  • 1
Andi
  • 224
  • 2
  • 10
  • 2
    Andi, take a look at http://stackoverflow.com/questions/25193392/issue-with-chrome-runtime-onconnect-when-building-chrome-extension-in-dart. It worked for me. – keerti Sep 04 '14 at 15:43
  • Yeah! Very thanks. I already knew that thread but didn't recognized that it was about the same problem. Got stuck at this bug for month ... Thanks man :) – Andi Sep 04 '14 at 18:22

2 Answers2

3

So your example is working fine for me:

//Placed in web/

import 'dart:js';

void main() {
  //This doesnt work in js
  context['chrome']['runtime']['onMessage'].callMethod('addListener', [onMessageListener]);
  context['chrome']['runtime'].callMethod('sendMessage', ['someMessage']);
  context['chrome']['runtime'].callMethod('sendMessage', [null, 'someMessage']);
}


void onMessageListener(message, sender, sendResponse) {
  print("test");
  print(message);
}

Output

test (:1)
someMessage (:1)
test (:1)
someMessage (:1)

Regards, Robert

// Sorry missed the exception you get

You should file a bug about this at www.dartbug.com

Regards, Robert

// For now you should be able to use the chrome package. It works fine here:

import 'dart:js';
import 'package:chrome/chrome_ext.dart' as chrome;

void onMessageListener(message, sender, sendResponse) {
  print("test");
  print(message);
}

void main() {
  chrome.runtime.onMessage.listen((chrome.OnMessageEvent event) {
    print(event.message);
  });

  JsObject runtime = context['chrome']['runtime'];
  runtime.callMethod('sendMessage', ['someMessage']);
  runtime.callMethod('sendMessage', [null, 'someMessage']);
}

Regards, Robert

Robert
  • 5,484
  • 1
  • 22
  • 35
  • Have you compiled the example to Js? The dart-version works for me as well, but not the compiled Js-version. – Andi Sep 03 '14 at 11:00
  • Nope that was my mistake. Debugging right now. Give me a second :) – Robert Sep 03 '14 at 11:03
  • So the error comes from the addListener access. But I cant tell you why at the moment. – Robert Sep 03 '14 at 11:09
  • That's right. My results are similar. As described above: chrome.runtime.onMessage can be accessed. `context['console'].callMethod('log', [context['chrome']['runtime']['onMessage']]);` prints the Event-object. But calling addListiner on the object breaks the extension :( – Andi Sep 03 '14 at 11:26
  • To your edit "For now you should be able to use the chrome package": As said in the question, i used `chrome.runtime.onMessage.listen` but the package has a bug in the **responding-functionality** which i also want to use. That was the main reason i switch to the js-interop variant. Sadly also using dart:js instead of chrome(-package) points me this bug. (For the chrome-package bug see also the posted [issue](https://github.com/dart-gde/chrome.dart/issues/150) long time ago) – Andi Sep 03 '14 at 11:58
  • Looks like you just can file a bug here. – Robert Sep 03 '14 at 13:41
0

As keerti already mentioned: A similar problem with a workaround can be find here.

My solution looks like this:

  //Tmp: sendResponse is buged, so we use the js-version
  //chrome.runtime.onMessage.listen(onMessageDartListener);

  //..and ofcourse the js-version is buged too. So this workaround here:
  var jsOnMessageEvent = context['chrome']['runtime']['onMessage'];
  JsObject dartOnMessageEvent = (jsOnMessageEvent is JsObject ? jsOnMessageEvent : new JsObject.fromBrowserObject(jsOnMessageEvent));
  dartOnMessageEvent.callMethod('addListener', [onMessageListener]);
Community
  • 1
  • 1
Andi
  • 224
  • 2
  • 10