-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[Bug] WebView.EvaluateJavascriptAsync is encoding input and output #11648
Comments
It seems that the 3rd party Plugin.HybridWebView strips out the extraneous stuff:
Xam.Forms just returns whatever the platform returns from the native web view EvaluateJavascript methods. |
any update on this? the solution that jgold6 has mentioned doesn't fully work for me. |
Why JSON.stringify the result? Xamarin.Forms/Xamarin.Forms.Core/WebView.cs Line 104 in 4b59b6f
|
Is this going to be fixed? |
Stumbled on this bug. I noticed that I can send Update: if I escape |
This is no bug. I created a bug for MAUI WebView and it seems it is wanted, for whatever reason. |
Thanks @n-ate! So basically, the workaround is: // Your js
string script = @"
alert(99);
true;";
await webView.EvaluateJavaScriptAsync(script.Trim().Replace("\r", "\\r").Replace("\n", "\\n"); At least it works on my side |
@bricefriha it is enought to replace with an empty string. This will save you a few nanoseconds on the browser side ignoring newlines. The only exception is if you have text which require the newline. |
It's what I was thinking about at first as well, but when I tested it didn't work. But feel free to test it on your side, I might have done something wrong 😉 |
It looks like this bug will be fixed for MAUI: dotnet/maui#11905 |
Description
The implementation of WebView.EvaluateJavascriptAsync is non-standard and appears to be encoding the input and output in unexpected ways. Other 3rd party plugins and other toolsets/languages (such as flutter) are behaving as expected.
Steps to Reproduce
var webView = sender as WebView; var html = webView.EvaluateJavascriptAsync("document.documentElement.outerHTML");
See https://stackoverflow.com/questions/63228670/get-html-document-from-xamarin-forms-webview and attached sample for more info.
Expected Behavior
The html should a standard HTML string with no encoding.
Actual Behavior
The html is encoded instead the straight HTML. Even after decoding, you will notice that string literals are double-escaped.
NOTE: If this behavior is deemed correct for some reason, then I would convert this into a feature request for an additional method that does not munge the input/output.
Basic Information
Reproduction Link
Workaround
Lucas Zhang suggested using WebUtility.HtmlDecode(html) and regex to decode the string, but other well-formed JS scripts still fail (whereas they do not with other 3rd party implementations and other tools such as flutter), leading me to believe that the implementation is targeted at some very specific use case and is doing more than it should.
TestWebView.zip
The text was updated successfully, but these errors were encountered: