MicrosoftEdge / MicrosoftEdge/WebView2Feedback

`CoreWebView2Frame.ExecuteScriptAsync` doesn't seem to do anything

Open
#2,927 4 comments 0 reactions 1 assignee View on GitHub

@vbryh-msft is already working on this.

Since Nov 2, 2022.

bug
Dominant language
PowerShell
Stars
526
Forks
67
PR merge metrics
No merged PRs in 30d

Description

Description
Trying to run JavaScript within an <iframe> using CoreWebView2Frame.ExecuteScriptAsync() doesn't seem to do anything.

Version
SDK: 1.0.1466-prerelease
Runtime: 107.0.1418.26 (but probably doesn't matter for this issue)
Framework: WPF (but probably doesn't matter for this issue)
OS: Win11 (but probably doesn't matter for this issue)

Repro Steps
git clone https://github.com/MicrosoftEdge/WebView2Samples.git then apply the below patch and compile and run the WebView2WpfBrowser VS solution:

diff --git a/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs b/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs
index e0a9084..c9ac47e 100644
--- a/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs
+++ b/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs
@@ -120,6 +120,7 @@ namespace WebView2WpfBrowser
         IReadOnlyList<CoreWebView2ProcessInfo> _processList = new List<CoreWebView2ProcessInfo>();

         public CoreWebView2CreationProperties CreationProperties { get; set; } = null;
+        private WebView2 _webView;

         public MainWindow()
         {
@@ -142,6 +143,7 @@ namespace WebView2WpfBrowser

         void AttachControlEventHandlers(WebView2 control)
         {
+            _webView = control;
             // <NavigationStarting>
             control.NavigationStarting += WebView_NavigationStarting;
             // </NavigationStarting>
@@ -1608,6 +1610,12 @@ namespace WebView2WpfBrowser
         {
             _isNavigating = true;
             RequeryCommands();
+            _webView.ExecuteScriptAsync("window.debugTopLevelProp = 42");
+            _webView.CoreWebView2.FrameCreated += async (object sender1, CoreWebView2FrameCreatedEventArgs args) =>
+            {
+                await args.Frame.ExecuteScriptAsync("alert('this alert is within the frame')");
+                await args.Frame.ExecuteScriptAsync("window.debugFrameProp = 42");
+            };
         }

         void WebView_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
@@ -1668,7 +1676,7 @@ namespace WebView2WpfBrowser

         private string GetStartPageUri(CoreWebView2 webView2)
         {
-            string uri = "https://appassets.example/AppStartPage.html";
+            string uri = "https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe";
             if (webView2 == null)
             {
                 return uri;
diff --git a/SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj b/SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj
index 2ea3c5b..5ef2cde 100644
--- a/SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj
+++ b/SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj
@@ -1,7 +1,7 @@
 <Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
   <PropertyGroup>
     <OutputType>WinExe</OutputType>
-    <TargetFrameworks>netcoreapp3.0;net462</TargetFrameworks>
+    <TargetFrameworks>netcoreapp3.0;net472</TargetFrameworks>
     <UseWPF>true</UseWPF>
     <Authors>Hybrid Application Team</Authors>
     <Company>Microsoft</Company>
@@ -40,7 +40,7 @@
     </Content>
   </ItemGroup>
   <ItemGroup>
-    <PackageReference Include="Microsoft.Web.WebView2" Version="1.0.1414-prerelease" />
+    <PackageReference Include="Microsoft.Web.WebView2" Version="1.0.1466-prerelease" />
        <PackageReference Include="System.ValueTuple" Version="4.5.0" />
   </ItemGroup>
 </Project>
\ No newline at end of file

I would expect a popup message with this alert is within the frame show up, and using the JS Console in the browser Dev Tools, I would expect window.debugFrameProp to exist within every <iframe>, but in this case, the only thing that exists is window.debugTopLevelProp, at the top level context.

NOTE: I also tried adding an args.Frame.NavigationStarting event handler, but CoreWebView2Frame.ExecuteScriptAsync() also didn't seem to do anything in there.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.