运行我的chrome扩展程序的dart2js
编译版时,我遇到以下问题:
Uncaught TypeError: undefined is not a function
执行时
context['chrome']['runtime']['onConnect'].callMethod('addListener', [(port) { ... }]);
我创建了一个可能指向原因的例子:
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 extensions(https://code.google.com/p/dart/issues/detail?id=17086)相关?
有人可以建议如何注册可在Dartium和Chrome中使用的chrome.runtime.onConnect
听众吗?
答案 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
中运行。
以下也是如此:
如果有人知道跟踪此问题的现有问题,请随时添加。