Problem with chrome.runtime.onConnect when creating a chrome extension in a dart

I have the following problem when running a dart2jscompiled version of the chrome extension:

Uncaught TypeError: undefined is not a function

while doing

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

I created an example that could indicate a reason:

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 this related to the difference between Dartium and dart2js when building chrome extensions ( https://code.google.com/p/dart/issues/detail?id=17086 )?

Can anyone suggest registering a chrome.runtime.onConnectlistener that will work in both Dartium and Chrome?

+3
source share
1

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

- , , .

+3

Source: https://habr.com/ru/post/1529547/


All Articles