在dart中构建chrome扩展时出现chrome.runtime.onConnect问题

时间:2014-08-07 22:38:35

标签: google-chrome-extension dart dart-js-interop

运行我的chrome扩展程序的dart2js编译版时,我遇到以下问题:

Uncaught TypeError: undefined is not a function

执行时

  context['chrome']['runtime']['onConnect'].callMethod('addListener', [(port) { ... }]);

我创建了一个可能指向原因的例子:

background.dart

import 'dart:js';

void main() {
  print("main(): context['chrome']['runtime']['onConnect'] (${context['chrome']['runtime']['onConnect'].runtimeType}): ${context['chrome']['runtime']['onConnect']}");
}

在Dartium中打印:

main(): context['chrome']['runtime']['onConnect'] (JsObject): [object Object]

但在Chrome中:

main(): context['chrome']['runtime']['onConnect'] (Event): Instance of 'Event'

是否与Difference between Dartium and dart2js when building chrome extensionshttps://code.google.com/p/dart/issues/detail?id=17086)相关?

有人可以建议如何注册可在Dartium和Chrome中使用的chrome.runtime.onConnect听众吗?

1 个答案:

答案 0 :(得分:3)

查看common.dart包中的chrome.dart

void _ensureHandlerAdded() {
  if (!_handlerAdded) {
    // TODO: Workaround an issue where the event objects are not properly
    // proxied in M35 and after.
    var jsEvent = _api[_eventName];
    JsObject event = (jsEvent is JsObject ? jsEvent : new JsObject.fromBrowserObject(jsEvent));
    event.callMethod('addListener', [_listener]);
    _handlerAdded = true;
  }
}

似乎有必要将Event包装到JsObject中,以使其在dart:js中运行。

以下也是如此:

  • port.onDisconnect
  • port.onMessage

如果有人知道跟踪此问题的现有问题,请随时添加。