dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.24k stars 1.76k forks source link

Webview.EvaluateJavaScriptAsync does not execute with newline #11905

Open trivalik opened 1 year ago

trivalik commented 1 year ago

Description

If you want to execute EvaluateJavaScriptAsync with multiple lines you have to make sure that no \r and no \n is inside.

Steps to Reproduce

  1. Create a File > New .NET MAUI App
  2. Add a Webview like so: <WebView Source="https://www.bing.com" x:Name="Browser" HeightRequest="500" WidthRequest="600" Navigated="Browser_Navigated"/>
  3. Implement Browser_Navigated with await Browser.EvaluateJavaScriptAsync("alert('abc');\r\nalert('def');");

Expected outcome: two alearts will appear Actual outcome: no aleat is shown

Link to public reproduction project repository

https://github.com/trivalik/EvaluateJavaScriptAsync/tree/main

Version with bug

7.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

Windows, I was not able test on other platforms

Affected platform versions

Windows SDK 10.0.17763.0

Did you find any workaround?

Get all calls through:

private async Task ExecuteJavaScriptAsync(string js)
{
    await Browser.EvaluateJavaScriptAsync(js.Replace("\r\n", ""));
}

Relevant log output

No response

ghost commented 1 year ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

ghost commented 1 year ago

Hi @trivalik. We have added the "s/try-latest-version" label to this issue, which indicates that we'd like you to try and reproduce this issue on the latest available public version. This can happen because we think that this issue was fixed in a version that has just been released, or the information provided by you indicates that you might be working with an older version.

You can install the latest version by installing the latest Visual Studio (Preview) with the .NET MAUI workload installed. If the issue still persists, please let us know with any additional details and ideally a reproduction project provided through a GitHub repository.

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

homeyf commented 1 year ago

Verified this issue with Visual Studio Enterprise 17.7.0 Preview 2.0. Not repro on windows platform with sample project. https://github.com/trivalik/EvaluateJavaScriptAsync/tree/main image

trivalik commented 1 year ago

I updated https://github.com/trivalik/EvaluateJavaScriptAsync/tree/main for 8.0.0-preview.6.8686.8. The behavior is unchanged.

hansmbakker commented 1 year ago

We have this problem targeting Android using .NET 8 RC2. Also, the output returned from ExecuteJavaScriptAsync is still encoded in an ugly way, as documented in https://github.com/xamarin/Xamarin.Forms/issues/11648

mnazers734 commented 7 months ago

We are experiencing this issue on .NET 8.0.202 (at least I am assuming it is this issue). Longer scripts do not run. We returned to platform specific implementation we had in XF and everything works fine. Would have been better and saved us a lot of time if EvaluateJavaScriptAsync was not even attempted by the MAUI team.

mycroes commented 1 month ago

Also experiencing this on: maui-windows 8.0.82/8.0.100 SDK 8.0.400, VS 17.11.35312.102

The issue is especially very annoying because there are no exceptions, nothing in the inspector log. Would appreciate an update on this issue.

mycroes commented 1 month ago

I've written a very rudimentary parser to convert multiline JS to single line:

public static string ToSingleLine(string input)
{
    var sb = new StringBuilder(input.Length);

    var state = State.Code;
    for (int i = 0; i < input.Length; i++)
    {
        var chr = input[i];
        switch (chr)
        {
            case '\r':
            case '\n':
                state = State.Code;
                break;
            case '/':
                switch (state)
                {
                    case State.Slash:
                        state = State.Comment;
                        // Remove previous slash
                        sb.Remove(sb.Length - 1, 1);
                        break;
                    case State.Code:
                        state = State.Slash;
                        sb.Append(chr);
                        break;
                }
                break;
            default:
                if (state == State.Comment) break;

                sb.Append(chr);
                break;
        }
    }

    return sb.ToString();
}

This has at least the following known issues:

However, it does work for basic scripts, permitting comments in there as well.