HybridWebView EvaluateJavaScriptAsync escaping '<' and tab characters

EJ 366 Reputation points
2021-05-31T04:17:43.913+00:00

Hi,

I've strange issue, when trying to extract inner html from a WebView I get all '<' characters replaced with '\u003C' and also all tabs as '\t'

This is how I extract inner html:

private async Task<string> GetHtml()
{
return await webView.EvaluateJavaScriptAsync( "document.body.innerHTML" );
}

Any idea how can I avoid this?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,378 questions
{count} votes

1 answer

Sort by: Most helpful
  1. JessieZhang-MSFT 7,716 Reputation points Microsoft External Staff
    2021-06-04T10:03:53.89+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    Problem is that I want to get original html

    From WebView.EvaluateJavaScriptAsync(String) Method, we know that WebView includes the ability to invoke a JavaScript function from C#, and return any result to the calling C# code. For more details, you can check: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/webview?tabs=windows#invoking-javascript .

    If you want to get original html, you can use the following code:

        HtmlWebViewSource LoadHTMLFileFromResource()  
        {  
            var source = new HtmlWebViewSource();  
    
            // Load the HTML file embedded as a resource in the .NET Standard library  
            var assembly = typeof(EvaluateJavaScriptPage).GetTypeInfo().Assembly;  
            var stream = assembly.GetManifestResourceStream("WebViewSample.index.html");              
            using (var reader = new StreamReader(stream))  
            {  
                source.Html = reader.ReadToEnd();  
            }  
            return source;  
        }  
    

    You can refer to document: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/webview?tabs=windows#invoking-javascript

    Best Regards,

    Jessie Zhang

    ---
    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.