Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dart2js bug: Listen to chrome api events causes an error #20800

Closed
DartBot opened this issue Sep 3, 2014 · 19 comments
Closed

Dart2js bug: Listen to chrome api events causes an error #20800

DartBot opened this issue Sep 3, 2014 · 19 comments
Assignees
Labels
closed-obsolete Closed as the reported issue is no longer relevant type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-dart2js

Comments

@DartBot
Copy link

DartBot commented Sep 3, 2014

This issue was originally filed by andi...@gmail.com


In short:

Calling context['chrome']['runtime']['onMessage'].callMethod('addListener', [onMessageListener]); in an dart-chrome-extension leads to the error "Uncaught TypeError: undefined is not a function" when running the compiled to Js.

==============

In Long:

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

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.


Also a discussion to this bug on stackoverflow: http://stackoverflow.com/questions/25631383/dart-chrome-extension-listen-to-chrome-api-events


Attachment:
TestApplication.zip (1.12 KB)

@DartBot
Copy link
Author

DartBot commented Sep 4, 2014

This comment was originally written by andi...@gmail.com


With the help of stackoverflow the current workaround looks like this:

var jsOnMessageEvent = context['chrome']['runtime']['onMessage'];
JsObject dartOnMessageEvent = (jsOnMessageEvent is JsObject ? jsOnMessageEvent : new JsObject.fromBrowserObject(jsOnMessageEvent));
dartOnMessageEvent.callMethod('addListener', [onMessageListener]);


Ofcourse this dosen't fix the bug, but might be useful to know for fixing it.

@sethladd
Copy link
Contributor

sethladd commented Sep 4, 2014

CC Justin


cc @justinfagnani.
Added Area-Dart2JS, Triaged labels.

@justinfagnani
Copy link
Contributor

What type of object is returned from context['chrome']['runtime']['onMessage'] in dart2js?

@DartBot
Copy link
Author

DartBot commented Sep 4, 2014

This comment was originally written by andi...@gmail.com


The type is called "Event"

@justinfagnani
Copy link
Contributor

I don't know enough about the Chrome APIs, but that seems odd. I would expect onMessage to be some kind of event provider, not an Event itself.

dart:js in dart2js simply performs an "is Event" check to decide if the Dart object should be returned rather than a wrapper. That check is returning true, so I would assert that dart:js/dart2js is working correctly, and one of the following is the real bug:

  1. chrome.runtime.onMessage shouldn't be an Event object. This is out of our scope though.
  2. dart2js isn't implementing "is Event" correctly, and it should return false.
  3. Dartium isn't implementing "is Event" correctly, and it should return true, or similarly Dartium isn't performing the Event type check properly.

Adding Emily and Jacob who can comment on (2) and (3).


cc @jacob314.
cc @efortuna.

@floitschG
Copy link
Contributor

ping.

@efortuna
Copy link
Contributor

I think this is a chrome library bug and not a dart2js bug -- we see similar problems with dart:html when we haven't correctly matched up the underlying JS prototype to the Dart version. I'll do some quick research on this API as I'm much less familiar with the Chrome APIs, but I think this is the direction of the bug.

@efortuna
Copy link
Contributor

What is the type of the object you get when you access chrome.runtime.onMessage in regular JS? That method isn't showing up in my JS console, but I suspect I'm "doing it wrong" as I have not worked with Chrome extensions until now.

@DartBot
Copy link
Author

DartBot commented Sep 11, 2014

This comment was originally written by andi...@gmail.com


I think only chrome extensions and apps have access to the chrome api.
Using native js "console.log(chrome.runtime.onMessage)" prints the following:

Event {addListener: function, removeListener: function, hasListener: function, hasListeners: function, dispatchToListener: function…}proto: Event

@efortuna
Copy link
Contributor

Hm... That definitely makes it seem like option #­1 (bug in the API itself) is the culprit. Is the regular JS return type (not involving Dart at all) of any of the other chrome EventHandlers an "Event"? Like chrome.alarms.onAlarm, or chrome.declarativeWebRequest.onRequest, or chrome.webNavigation.onCommitted?

@DartBot
Copy link
Author

DartBot commented Sep 11, 2014

This comment was originally written by andi...@gmail.com


Have tested for chrome.alarms.onAlarm and chrome.declarativeWebRequest.onRequest. They both return also an Event-object.

@efortuna
Copy link
Contributor

So it would seem the Chrome API version of Event is a little different from the standard DOM Event...

@justinfagnani
Copy link
Contributor

Honestly, this prototype organization seems a little crazy to me.

I wonder, is it the same Event prototype as regular DOM events?

Is

Object.getPrototypeOf(chrome.runtime.onMessage) === window.Event.prototype

true?

@DartBot
Copy link
Author

DartBot commented Sep 12, 2014

This comment was originally written by andi...@gmail.com


Good morning.

Object.getPrototypeOf(chrome.runtime.onMessage) === window.Event.prototype is false.

@DartBot
Copy link
Author

DartBot commented Sep 12, 2014

This comment was originally written by andig...@gmail.com


I did some research. Looks like chrome api has it's own Event-type:

https://developer.chrome.com/extensions/events#type-Event

@efortuna
Copy link
Contributor

Indeed, so the Chrome APIs have their own wacky Event type, then (confusing!). So this suggests it's maybe a problem with dart2js -- perhaps it sees "Event" and is incorrectly mapping it to the dart:html Event?

@floitschG
Copy link
Contributor

cc @rakudrama.

@efortuna
Copy link
Contributor

FYI - Stephen is on vacation through 9/22

@justinfagnani
Copy link
Contributor

Yes, sounds like a dart2js/dart:html bug.

Thanks for your help investigating this Andi.

@kevmoo kevmoo added type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) and removed triaged labels Mar 1, 2016
@matanlurey matanlurey added the closed-obsolete Closed as the reported issue is no longer relevant label Mar 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-obsolete Closed as the reported issue is no longer relevant type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-dart2js
Projects
None yet
Development

No branches or pull requests

8 participants