本文介绍了在dart中构建chrome扩展时,与chrome.runtime.onConnect有关的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行 dart2js 编译版本的chrome扩展时遇到以下问题:

 未捕获TypeError:undefined不是函数




$ b

  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中:

  

它与()?



可能有人建议如何注册将在Dartium和Chrome中使用的 chrome.runtime.onConnect 侦听器。

解决方案

查看 chrome.dart 包中的 common.dart 包:

  void _ensureHandlerAdded(){
if(!_handlerAdded){
// TODO:不正确
//代理在M35和之后。
var jsEvent = _api [_eventName];
JsObject event =(jsEvent is JsObject?jsEvent:new JsObject.fromBrowserObject(jsEvent));
event.callMethod('addListener',[_listener]);
_handlerAdded = true;
}
}

看起来有必要换行

$ 中的到 b
$ b

以下也需要同样的功能:




  • port.onDisconnect

  • port.onMessage



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

I have the following problem when running dart2js compiled version of my chrome extension:

Uncaught TypeError: undefined is not a function

when executing

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

I have created an example which possibly points to the cause:

background.dart

import 'dart:js';

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

prints in Dartium:

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

but in Chrome:

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

Is it related to Difference between Dartium and dart2js when building chrome extensions (https://code.google.com/p/dart/issues/detail?id=17086)?

Could someone suggest how to register chrome.runtime.onConnect listener which would work in both Dartium and Chrome?

解决方案

After looking at common.dart in chrome.dart package:

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;
  }
}

it seems necessary to wrap Event into JsObject to make it working in dart:js.

The same was required for the following too:

  • port.onDisconnect
  • port.onMessage

If someone knows of an existing issue tracking this problem, please feel free to add it.

这篇关于在dart中构建chrome扩展时,与chrome.runtime.onConnect有关的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 04:33