2

How do you debug the ExecuteScriptAsync method? I am trying to set a value for innerHTML but it does not work. I had the method wrapped around a try-catch but no exception is returned.

Below is the code I use to update the innerHTML: ExecuteScriptAsync("document.getElementById('" + elementID + "')." + property + " = \'" + value + "\'");

It works fine if I set a dummy string as a value. I need to know what it is making it reject HTML contents.

Thanks

Zimo
  • 312
  • 5
  • 21
  • Does this answer your question? [WebView2 - Update innerHTML](https://stackoverflow.com/questions/66991516/webview2-update-innerhtml) – Poul Bak Apr 07 '21 at 19:35

2 Answers2

1

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. If there is such an error it will not actually run the script so try/catch and log statements make no difference. Do not ask me why ExecuteScriptAsync does not report syntax errors or why that function is even recommended in the guides. My issue is that I was using a typescript script (naively thinking microsoft would support their own language in their webview2 component) so it wasn't running at all. Once you have it running you can log to the console as normal.

Right click -> inspect is the easiest way to open the console but there are other ways as detailed in the debug documentation.

user3190036
  • 463
  • 6
  • 15
0

thanks for the question.

Here is a doc on how to debug in WebView2. Since your javascript code is inline in the ExecuteScriptAsync function debugging the js might be a bit tricky. However, if you move the js contents to its own file as described here, You can set breakpoints with the javascript debugger and step through your script file if you have one.

Also for future needs, here's a link to the WebView2 github Q&A board.

  • Thanks for the links. Still not sure how to step through the `ExecuteScriptAsync` in my case. The content I am trying to set to the innerHTML has nothing wrong. It works with no issues using `WebBrowser`. It's like `ExecuteScriptAsync` is rejecting or pausing for long strings set for innerHTML – Zimo Apr 07 '21 at 15:53