question

EJ-5254 avatar image
0 Votes"
EJ-5254 asked EJ-5254 commented

HybridWebView EvaluateJavaScriptAsync escaping '<' and tab characters

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?

dotnet-xamarin
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

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

Hi @EJ-5254 ,what is your specific problem? could you please post a basic demo to github or onedriver so that we can test on our side?

0 Votes 0 ·

Hi,

Problem is that I want to get original html, not an escaped version with all '\u003C' and '\\t' etc.?
Regarding demo, just simply assign some html and extract with

 webView.EvaluateJavaScriptAsync( "document.all[0].innerHTML" );

and you will get the result I'm talking about.

0 Votes 0 ·

1 Answer

JessieZhang-2116 avatar image
0 Votes"
JessieZhang-2116 answered EJ-5254 commented

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://docs.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://docs.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.



· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Jessie,

I'm changing contents of a WebView in the app (WebView is editable) and need to get original HTML string from the WebView and not from resource.

0 Votes 0 ·