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
Comments
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']; Ofcourse this dosen't fix the bug, but might be useful to know for fixing it. |
CC Justin cc @justinfagnani. |
What type of object is returned from context['chrome']['runtime']['onMessage'] in dart2js? |
This comment was originally written by andi...@gmail.com The type is called "Event" |
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:
Adding Emily and Jacob who can comment on (2) and (3). |
ping. |
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. |
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. |
This comment was originally written by andi...@gmail.com I think only chrome extensions and apps have access to the chrome api. Event {addListener: function, removeListener: function, hasListener: function, hasListeners: function, dispatchToListener: function…}proto: Event |
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? |
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. |
So it would seem the Chrome API version of Event is a little different from the standard DOM Event... |
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? |
This comment was originally written by andi...@gmail.com Good morning. Object.getPrototypeOf(chrome.runtime.onMessage) === window.Event.prototype is false. |
This comment was originally written by andig...@gmail.com I did some research. Looks like chrome api has it's own Event-type: |
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? |
cc @rakudrama. |
FYI - Stephen is on vacation through 9/22 |
Yes, sounds like a dart2js/dart:html bug. Thanks for your help investigating this Andi. |
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()
orconsole.log()
viadart: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)
The text was updated successfully, but these errors were encountered: