Dart Chrome扩展程序:监听chrome api事件 [英] Dart chrome extension: Listen to chrome api events

查看:657
本文介绍了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() 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.

编辑
我现在已在 dartbug.com

推荐答案

由于 keerti 已提及:解决方法的类似问题可以查找

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]);

这篇关于Dart Chrome扩展程序:监听chrome api事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆