Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Wasm] WebView.executeScriptAsync must be a Function but was undefined #19635

Closed
jeromelaban opened this issue Mar 5, 2025 · 4 comments · Fixed by #19672
Closed

[Wasm] WebView.executeScriptAsync must be a Function but was undefined #19635

jeromelaban opened this issue Mar 5, 2025 · 4 comments · Fixed by #19672
Assignees

Comments

@jeromelaban
Copy link
Member

I haven't seen this anywhere and, before filing a but report, I wanted to ask if ExecuteScriptAsync() is available for the WASM implementation of WebView2? When I try it ...

                try
                {
                    await WebView.ExecuteScriptAsync("document.getElementById('test').style.color = 'red';");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"EXCEPTION: [{ex}] [{ex.StackTrace}]");
                }

... I get the following (and more) in the JavaScript console:

EXCEPTION: [Error: globalThis.Microsoft.UI.Xaml.Controls.WebView.executeScriptAsync must be a Function but was undefined
dotnet.native.8bti36lthm.js:8     at http://localhost:5000/_framework/dotnet.runtime.tsg4gsv2hg.js:3:170299
dotnet.native.8bti36lthm.js:8     at http://localhost:5000/_framework/dotnet.runtime.tsg4gsv2hg.js:3:170373
dotnet.native.8bti36lthm.js:8     at Fc (http://localhost:5000/_framework/dotnet.runtime.tsg4gsv2hg.js:3:171729)
dotnet.native.8bti36lthm.js:8     at wasm://wasm/00b59456:wasm-function[287]:0x1f0c7
dotnet.native.8bti36lthm.js:8     at wasm://wasm/00b59456:wasm-function[218]:0x1c7c6
dotnet.native.8bti36lthm.js:8     at wasm://wasm/00b59456:wasm-function[209]:0xea13
dotnet.native.8bti36lthm.js:8     at wasm://wasm/00b59456:wasm-function[246]:0x1d33e
dotnet.native.8bti36lthm.js:8     at wasm://wasm/00b59456:wasm-function[3221]:0xf0ea8
dotnet.native.8bti36lthm.js:8     at wasm://wasm/00b59456:wasm-function[2544]:0xc69fe
dotnet.native.8bti36lthm.js:8     at wasm://wasm/00b59456:wasm-function[2549]:0xc71f6] [   at System.Runtime.InteropServices.JavaScript.JSFunctionBinding.BindJSImportImpl(String functionName, String moduleName, ReadOnlySpan`1 signatures)
dotnet.native.8bti36lthm.js:8    at System.Runtime.InteropServices.JavaScript.JSFunctionBinding.BindJSFunction(String functionName, String moduleName, ReadOnlySpan`1 signatures)
dotnet.native.8bti36lthm.js:8    at __Microsoft.UI.Xaml.Controls.NativeWebView.NativeMethods.ExecuteScript(IntPtr htmlId, String script) in /agent/_work/1/s/src/Uno.UI/obj/Uno.UI.Wasm/Release/net9.0/Microsoft.Interop.JavaScript.JSImportGenerator/Microsoft.Interop.JavaScript.JSImportGenerator/JSImports.g.cs:line 298
dotnet.native.8bti36lthm.js:8    at Microsoft.UI.Xaml.Controls.NativeWebView.ExecuteScriptAsync(String script, CancellationToken token) in /agent/_work/1/s/src/Uno.UI/UI/Xaml/Controls/WebView/Native/Wasm/NativeWebView.wasm.cs:line 51
dotnet.native.8bti36lthm.js:8    at Microsoft.Web.WebView2.Core.CoreWebView2.<>c__DisplayClass160_0.<ExecuteScriptAsync>b__0(CancellationToken ct) in /agent/_work/1/s/src/Uno.UI/UI/Xaml/Controls/WebView/Core/CoreWebView2.cs:line 141
dotnet.native.8bti36lthm.js:8    at Windows.Foundation.AsyncOperation.<>c__DisplayClass2_0`1[[System.String, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].<FromTask>b__0(CancellationToken ct, AsyncOperation`1 _) in /agent/_work/1/s/src/Uno.Foundation/Internal/AsyncOperation.cs:line 15
dotnet.native.8bti36lthm.js:8    at Windows.Foundation.AsyncOperation`1.<BuildTaskAsync>d__23[[System.String, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext() in /agent/_work/1/s/src/Uno.Foundation/Internal/AsyncOperation.TResult.cs:line 83
dotnet.native.8bti36lthm.js:8    at System.WindowsRuntimeSystemExtensions.<AsTaskCore>d__11`1[[System.String, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext() in /agent/_work/1/s/src/Uno.UWP/System.Runtime/WindowsRuntimeSystemExtensions.cs:line 66
dotnet.native.8bti36lthm.js:8    at UnoResourceLoadingTest.MainPage.NavWebViewButton_Click(Object sender, RoutedEventArgs e) in C:\Users\ben\RiderProjects\UnoResourceLoadingTest\UnoResourceLoadingTest\MainPage.xaml.cs:line 124]

Happens with any Uno.Sdk 5.6.xx implementation I've tried, both in VisualStudio Windows and JetBrains Rider for MacOS.

Originally posted by @baskren in #19627

@baskren
Copy link
Contributor

baskren commented Mar 5, 2025

DEMO REPO: https://github.com/baskren/UnoWasmWebView2ExecuteScriptAsyncFail

Also note that, in the demo app, after ExecuteScriptAsync is run, the app is frozen.

@baskren
Copy link
Contributor

baskren commented Mar 5, 2025

@jeromelaban : Thank you for taking a look at this!

@jeromelaban
Copy link
Member Author

@MartinZikmund It looks like:

public Task<string> ExecuteScriptAsync(string script, CancellationToken token) => Task.FromResult(NativeMethods.ExecuteScript(HtmlId, script));

should be using an actual async JSImport? Do you recall if WebView2 actually supports async calls?

@baskren
Copy link
Contributor

baskren commented Mar 19, 2025

Thank you @MartinZikmund and @jeromelaban

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants