MicrosoftEdge / WebView2Browser

A web browser built with the Microsoft Edge WebView2 control.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Possible to view-source:url in code?

daniel-rck opened this issue · comments

Hello,
I'm doing a little sample application with WebView2. I try to get the source-code of the current page,
the same as you can see in e.g. view-source:https://www.bing.com/
Is this possible from within C#?

Already did this with javascript, but there is missing data in comparison to the browser variant.

        try
        {
            string html = await webView.CoreWebView2.ExecuteScriptAsync("document.documentElement.outerHTML");
            string unescaped = Regex.Unescape(html);
            html = unescaped.Substring(1, unescaped.Length - 2).Replace("&", "&");

            if (!html.StartsWith("<html"))
            {
                Debug.WriteLine("Invalid source");
                return null;
            }
            var doc = new HtmlDocument();
            doc.LoadHtml(html);
            return doc;
        }
        catch (Exception exc)
        {
            Debug.WriteLine(exc.Message);
            return null;
        }

`