Dart Chrome扩展程序:监听chrome api事件
为了更好地描述我的问题,我创建了一个用Dart编写的chrome扩展的小例子。
您可以在 Gist 上查看代码或下载扩展程序。
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.
问题
此示例在Dartium中正常运行,但在编译为javascript时会发生类型错误:未捕获的TypeError:undefined不是以下行的函数
:
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]);
我已有多远
- 正如你在例子中看到的,
alert()
或console.log
通过dart:js
也在js-extension中工作。 - 还会打印出
context ['chrome'] ['runtime' ] ['onMessage']
显示正确的事件对象。 (例如:context ['console']。callMethod('log',[context ['chrome'] ['runtime'] ['onMessage']];
) - 我知道有一个chrome pub包,但是在onMessage中响应接收到的消息时仍然有一个错误。另请参阅此问题。使用chrome api直接通过dart:js是在dart版本上很好的解决方法。
- 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.
我玩了很多代码,但所有的结果都有相同的错误。现在我的想法。希望社群可以再次帮助我。
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.
编辑:
我现在已在 dartbug.com 。
As keerti already mentioned: A similar problem with a workaround can be find here.
我的解决方案如下:
//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]);