Zimo
Zimo

Reputation: 342

WebView2 - Call/Execute Javascript Functions

The InvokeScript method was deprecated in WebView2 so I am trying to learn the new alternative. According to the Microsft documentation, we have to call the ExecuteScriptAsync method in order to run a script. I was able to create an async method that executes my entire javascript function as a string and it worked. However, I find this approach not easy to debug; the ExecuteScriptAsync never tells you if there is a syntax error ...

How do you debug javascript code executed by the ExecuteScriptAsync? Is there any other option to call javascript functions (with parameters)?

Thanks

Upvotes: 4

Views: 6544

Answers (2)

darbid
darbid

Reputation: 2721

Run your app and press F12 or right click and choose inspect. In the dev tools go to the console. JS error will be shown there. Add console.log to your js code to see them also here.

Upvotes: 1

user3190036
user3190036

Reputation: 533

I found a way, if you use

await webView.EnsureCoreWebView2Async(null);
string result = await webView.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync(myScript);

(ExecuteScriptAsync is replace with AddScriptToExecuteOnDocumentCreatedAsync)

then it will log syntax errors to the console.

For more detail see https://stackoverflow.com/a/69810805/3190036

Upvotes: 0

Related Questions