4

I am using Microsoft Webview2 to load a web page and below code is used to read the HTML content

string html = await webView1.ExecuteScriptAsync("document.documentElement.outerHTML");

The resulting output html appears with all tag opening, closing and other special characters encoded like below

\u003Chtml lang=\"en\" data-mode=\"light\">\u003Chead>\n    \u003Cmeta charset=\"utf-8\">\n    \u003Cmeta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\u003Ctitle data-argonaut-actions=\"true

Why does this happen? How do I get plain HTML?

2
  • 2
    Manual says that function returns "A JSON encoded string that represents the result of running the provided JavaScript." (learn.microsoft.com/en-us/dotnet/api/…) Commented May 4, 2023 at 21:57
  • Yes, the result of ExecuteScriptAsync in this case is a string containing a JSON encoded string. So you need to parse the result as JSON and get the string value out of it. This is described here: learn.microsoft.com/en-us/microsoft-edge/webview2/how-to/… Commented May 4, 2023 at 22:34

1 Answer 1

3
var html = await webView2.ExecuteScriptAsync("document.documentElement.outerHTML");
html = Regex.Unescape(html);
html = html.Remove(0, 1);
html = html.Remove(html.Length - 1, 1);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.