MBS: ExecuteScriptCompleted event

What I could not get to work is the ExecuteScriptCompleted event.

In NavigationCompleted I did put:
//me.ExecuteScript("(p => ({Test: ‘Test’}))()", 1)
me.ExecuteScript("(p => { window.chrome.webview.postMessage({Test:‘2 Hello, world!’})} )()", 1)

While WebMessageReceived is triggered, ExecuteScriptCompleted does not trigger.
(Also not if first call is used returning a value).
How has the call to be done maybe to respect the asynch nature of the call?

Thanks a lot!

PostWebMessageAsString does crash the compiled Xojo exe.
PostWebMessageAsJson does complain “wrong argument”.

I tried many combinations also for return value of
window.chrome.webview.addEventListener('message'...

But, ExecuteScript method and WebMessageReceived event do work; so, complete communication between control and JS is possible!

String length transmitted until 100mio length is ok; after that or from 1billion, browser gives error.
But 100mio chars are sent ok -> should be enough :roll_eyes:
ExecuteScript with 33mio chars does also work…

Windows 10, 20.5pr7

Now as we have this split into a new topic, I can later try it myself.

web.ExecuteScript "1+5" , "tag value"

this will show 6 when it calls ExecuteScriptCompeleted event.

web.ExecuteScript("(p => { alert('Hello'); return 3; })()")

This will show the alert and then call the event with value 3 as result of the script. Please note that your code in first posting has ( ) and { } mismatched.

This seems to work:

web.ExecuteScript "window.chrome.webview.postMessage({'Test': 'Hello, world!'});"

And this triggers WebMessageReceived event here.
For strings, please don’t try 100 megabytes. That is huge and eventually you may hit some limits if you go bigger.

Maybe try again with 20.5pr8?

Ok, I tried 20.5pr8

web.PostWebMessageAsString works even with 33mio chars.
The “a lot of chars” test is only done not to be surprises later there may be a 65535 limit or something else. Everything fine here!

web.PostWebMessageAsJSON gives error “Falscher Parameter” (-2147024809).
Don’t know what to send to get it right.
“Is Web Message enabled” does not work either way.

Finally, not a big problem here; my complete tool does work in the control!

I can try it later here.
web.PostWebMessageAsJSON should take a text, which must be valid JSON.
And what is not working with IsWebMessageEnabled property?

I checked this

The IsWebMessageEnabled setting must be true or this method will fail with E_INVALIDARG.

Valid JSON like this…
{Test:'Hello, world!}
…or like this?
`{“Test”:“Hello, world!”}

Like this:

{“Test”: “Hello, world!”}

with quotes of course around Test.
Your first one is not valid as it has only one single quote.
And the second has curly quotes, which will not work.

Maybe just built JSON with JSONItem class and then use ToString?

Ok, this is seldom used…
Works as You said!

Thanks a lot!!!

1 Like

Unfortunately, the ExecuteScriptCompleted event is still not triggered.

I use version 20.5 (release) with Windows 10 and Xojo 2020 R1.2.

This is the code run by button after all things are loaded in WebView2ControlMBS:
WV.ExecuteScript "2 * 3", "tag value"

The MBS example source “WebView2 Test” does not use ExecuteScript either…

What concerns me: I want to switch from Vivaldi chromeless browser to Xojo WebView2 completely to get rid of websocket. And, this has to work overall.

Thanks a lot for any idea in advance!

Well, this works here.

I did take our example project “WebView2 Test.xojo_binary_project”. I added a button to the window with this action event:

Sub Action() Handles Action
  web.ExecuteScript "2 * 3", "tag value"
End Sub

and I use this event to catch answer:

Sub ExecuteScriptCompleted(JavaScript as String, ErrorCode as Integer, resultObjectAsJson as String, Tag as Variant) Handles ExecuteScriptCompleted
  System.DebugLog CurrentMethodName
  
  MsgBox resultObjectAsJson+EndOfLine+tag
End Sub

and that dialog is called here. Not sure what is different for you.

Ok, with this event given, everything works:

       Sub AddScriptToExecuteOnDocumentCreatedCompleted(JavaScript as String, ErrorCode as Integer, ID as String, Tag as Variant) Handles AddScriptToExecuteOnDocumentCreatedCompleted
      System.DebugLog CurrentMethodName+" "+ID+";"+Tag
  End Sub

This is interesting:

WV.ExecuteScript “(p => ({Test:{Value:${p}}}))(3)”, “Tag 1” //back quotes are deleted by forum…
This gives back "{Test:{Value:3}}" with double quotes.

WV.ExecuteScript "(p => ({Test:{Value:p}}))(3)", "Tag 1"
This gives back {"Test":{"Value":3}} without quotes.

There is no bracket mismatch; it’s a shorter version of the arrow function :man_student: :roll_eyes:

Ok, for me everything seems to be well done; simply have to pay attention what type comes back how.

This is strange:

window.chrome.webview.postMessage("web message as string")
gives
System.DebugLog(“JSON:” + webMessageAsJson) //wrong? // JSON:“web message as string”
System.DebugLog(“String:” + webMessageAsString) //correct! // String:web message as string (correct)

window.chrome.webview.postMessage({Test:"web message as JSON?"})
gives

System.DebugLog(“JSON:” + webMessageAsJson) //correct! // JSON:{“Test”:“web message as JSON?”}
System.DebugLog(“String:” + webMessageAsString) //correct! // String:

So, I would not expect a content in webMessageAsJSON of event WebMessageReceived when I post a string value from browser.

I will try to only use JSON for all communication.

As the plugin passes through the values, so not sure what happens here.
And still possible that there may be a bug in the framework from Microsoft.

As the plugin passes through the values, so not sure what happens here.

And no need to waste more time on this… :wink:
I will use it now and if things should get really wrong, I can post another entry!

Thanks a lot!

1 Like