Skip to content

Possible to view-source:url in code? #296

@daniel-rck

Description

@daniel-rck

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;
        }

`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions