Reproduction of timing issue when setting CoreWebView2NewWindowRequestedEventArgs.NewWindow
vs ensuring scripts are loading prior to navigation (i.e. CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync
).
The project loads an embedded web page with a button and a link. When either are clicked a new window should appear containing a WebView2 instance that:
- Has scripts added (via a call to
CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync
) that cause an alert to be shown on document load. - Is assigned to the
NewWindow
property ofCoreWebView2NewWindowRequestedEventArgs
- Loads to destination url (https://ian.bebbs.co.uk)
- Fires the JS alert twice (once for core document load, once for an embedded frame).
Behavior is indeterminate as it seems there is a race condition. Either of the following are common outcomes:
- A blank web page is displayed with scripts added but no alerts as no document is loaded.
- The destination url is displayed but no alerts are displayed as the scripts have not been added.
This project requires:
- .NET6 SDK
- An evergreen deployment of WebView2 - at time of writing this is runtime version '107.0.1418.42'.
The project contains numerous modifiers at the top of the main window. These include:
- Set New Window - Determines whether the new WebView2 instance should be assigned to the
NewWindow
property ofCoreWebView2NewWindowRequestedEventArgs
- Add Scripts - Determins if and when to call
CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync
:- After - will be called after assigning
CoreWebView2NewWindowRequestedEventArgs.NewWindow
(per documentation here) - Before - will be called before assigning
CoreWebView2NewWindowRequestedEventArgs.NewWindow
(per documentation here) - None -
CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync
will not be called.
- After - will be called after assigning
- Delay - Adds an optional
await Task.Delay(<milliseconds>))
after assigningCoreWebView2NewWindowRequestedEventArgs.NewWindow
as this has been found to change behavior - Set Source - Specifically assigns the
CoreWebView2NewWindowRequestedEventArgs.Uri
to the new WebView'sSource
property. - Schedule New Window - Allows the
CoreWebView2NewWindowRequested
event to complete by scheduling a new action onto the dispatcher (aka UI) thread which performs the initialization of the new Window/WebView
(See ![Expected Behavior](Recordings/Expected Behavior.gif | width=100))
- Start the app.
- Modify controls to the following:
- "Set New Window" unchecked
- "Add Scipts" set to "After"
- "Delay" set to "None"
- "Set Source" checked
- "Schedule New Window" unchecked
- Click the 'Open Window From Target="_blank"' link.
- A new window will appear and two JS alerts shown as the content loads.
(See ![Actual Behavior](Recordings/Actual Behavior.gif | width=100) & ![Actual Behavior with Delay](Recordings/Actual Behavior with Delay.gif | width=100))
- Start the app.
- Modify controls to the following:
- "Set New Window" checked
- "Add Scipts" set to "After"
- "Delay" set to "None"
- "Set Source" unchecked
- "Schedule New Window" unchecked
- Click the 'Open Window From Target="_blank"' link.
- A new window will appear which either:
- Is blank
- Navigates to the destination URL but no JS alerts are shown; i.e. scripts have not been added prior to first document load.
- Navigates to the destination URL but only a single JS alerts is shown; i.e. scripts were not added prior to first document load.
- Try various settings for "Delay" and observe different outcomes in the previous step