Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught exception when trying to chrome.runtime.sendMessage() #30

Open
matofesi opened this issue Aug 8, 2024 · 3 comments
Open

Uncaught exception when trying to chrome.runtime.sendMessage() #30

matofesi opened this issue Aug 8, 2024 · 3 comments

Comments

@matofesi
Copy link

matofesi commented Aug 8, 2024

I'm not sure if it's me or is it a problem withe the library... What I'm trying to do is send a message between extensions blocks. I set a listener in one file with chrome.runtime.onMessage.listen() and then send messages with

chrome.runtime.sendMessage(
null,
{ "cmd": "saved" },
SendMessageOptions(includeTlsChannelId: false)
);

It seems to be working - messages are being delivered, but every time I do sendMessage I get exception like that in console:

Uncaught (in promise) Error: TypeError: null: type 'minified:aH' is not a subtype of type 'Object'
    at Object.b (offscreen.dart.js:271:18)
    at H.hr [as a] (offscreen.dart.js:783:9)
    at H.hD (offscreen.dart.js:742:10)
    at b_.a5 (offscreen.dart.js:2491:16)
    at dJ.$1 (offscreen.dart.js:3171:21)

Should I try to do something else or is there something wrong with the library?

@xvrh
Copy link
Owner

xvrh commented Aug 8, 2024

Hi, can you compile the application with --profile mode to get an unminified stack trace.
That would help. Thanks.

@matofesi
Copy link
Author

matofesi commented Aug 8, 2024

I know that's probably a stupid question, but... how do I do that? I'm building an extension and I compile it with webdev build. I found some options for build_web_compilers but I don't see how to add --profile.

Oh... Unless what you asked about was this:

Uncaught (in promise) Error: TypeError: null: type 'JSNull' is not a subtype of type 'Object'
    at Object.wrapException (js_helper.dart:1190:19)
    at Rti._asObject [as _as] (rti.dart:1466:3)
    at _AsyncCompleter.complete$1 (future_impl.dart:44:41)
    at promiseToFuture_closure.call$1 (js_util_patch.dart:483:49)

@mishkov
Copy link

mishkov commented Sep 8, 2024

I am sure in all JS stuff and I cannot debug the code but after many tries and experiments I think I have found the solution of this exception. The reason is that the the method sendMessage have non-nullable return type Object and inside of it also there is a ! as well as the generic type for promiseToFuture call is not nullable. See:

Future<Object> sendMessage(
  String? extensionId,
  Object message,
  SendMessageOptions? options,
) async {
  var $res = await promiseToFuture<JSAny>($js.chrome.runtime.sendMessage(
    extensionId,
    message.jsify()!,
    options?.toJS,
  ));
  return $res.dartify()!;
}

So when as I understand

$js.chrome.runtime.sendMessage(
  extensionId,
  message.jsify()!,
  options?.toJS,
)

returns null it causes the exception because the JSAny cannot be null

/// A non-nullish JavaScript value.
///
/// A [JSAny] can be any JavaScript value except JavaScript `null` and
/// `undefined`. JavaScript `null` and `undefined` are instead converted to Dart
/// `null` by the compiler. Therefore, <code>[JSAny]?</code> is the top type of
/// the type hierarchy as it includes nullish JavaScript values as well.
extension type JSAny._(JSAnyRepType _jsAny) implements Object {}

and the line return $res.dartify()!; will contain call on null object.

@matofesi The temporary solution is to locally modify the source code of the library as follow

- Future<Object> sendMessage(
+ Future<Object?> sendMessage(
  String? extensionId,
  Object message,
  SendMessageOptions? options,
) async {
-   var $res = await promiseToFuture<JSAny>($js.chrome.runtime.sendMessage(
+   var $res = await promiseToFuture<JSAny?>($js.chrome.runtime.sendMessage(
    extensionId,
    message.jsify()!,
    options?.toJS,
  ));
-   return $res.dartify()!;
+  return $res?.dartify();
}

I am gonna prepare PR for this Issue as soon as possible

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants