3

dart2js运行我的 chrome 扩展的编译版本时出现以下问题:

Uncaught TypeError: undefined is not a function

执行时

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

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

背景.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'

构建 chrome 扩展时是否与Dartium 和 dart2js 之间的差异https://code.google.com/p/dart/issues/detail?id=17086)有关?

有人可以建议如何注册chrome.runtime.onConnect适用于 Dartium 和 Chrome 的监听器吗?

4

1 回答 1

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.

以下内容也需要相同:

  • 端口.onDisconnect
  • 端口.onMessage

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

于 2014-08-12T17:23:24.153 回答