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

postWebMessage failed #277

Open
summanzhao opened this issue Jan 28, 2024 · 2 comments
Open

postWebMessage failed #277

summanzhao opened this issue Jan 28, 2024 · 2 comments

Comments

@summanzhao
Copy link

Hello, I would like to send a message to JavaScript using the postWebMessage method:

WebviewController kWebController=WebviewController();

//Several initialization tasks for WebWiew
........
_ Subscriptions. add (kWebController. loadingState. listen ((state)){

DebugPrint ('Loading state: $state ');

If (state==LoadingState. navigation Completed){

KWebController.executeScript(

"Window. addEventListener ('message ', event=>{console. log ('Received message:', event. data);});";

KWebController. postWebMessage ("hello world");

}

});

After completion

KWebController. postWebMessage ("hello world");

There was an error in this call:
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(not_supported, Posting the message failed., null, null)
#0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:652:7)
#1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:310:18)

How should I solve it? Looking forward to your reply!

@bladeofgod
Copy link

same issue.
And can you post msg from web ?

@xorespesp
Copy link

Hello, I would like to send a message to JavaScript using the postWebMessage method:

WebviewController kWebController=WebviewController();

//Several initialization tasks for WebWiew ........ _ Subscriptions. add (kWebController. loadingState. listen ((state)){

DebugPrint ('Loading state: $state ');

If (state==LoadingState. navigation Completed){

KWebController.executeScript(

"Window. addEventListener ('message ', event=>{console. log ('Received message:', event. data);});";

KWebController. postWebMessage ("hello world");

}

});

After completion

KWebController. postWebMessage ("hello world");

There was an error in this call: [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(not_supported, Posting the message failed., null, null) #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:652:7) #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:310:18)

How should I solve it? Looking forward to your reply!

The WebviewController.postWebMessage method internally calls ICoreWebView2.PostWebMessageAsJson,
so the message passed to the WebviewController.postWebMessage method must be in JSON format.
otherwise, it will fail internally with HRESULT 0x80070057(E_INVALIDARG) error.

... also if you want to receive messages on the JavaScript side, you need to use window.chrome.webview.addEventListener, not Window.addEventListener.

Try this:

WebviewController _webviewController = WebviewController();

// ... 

_subscriptions.add(_webviewController.loadingState.listen((state) {
  debugPrint('Loading state: $state ');

  if (state == LoadingState.navigationCompleted) {
    _webviewController.executeScript(
      "window.chrome.webview.addEventListener('message', event => { console.log('Received message:', event.data.message); });"
    );
    _webviewController.postWebMessage("{ \"message\": \"hello world\" }");
  }
}));

See:

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