{"id":23864496,"url":"https://github.com/ibebbs/scriptvsnewwindow","last_synced_at":"2025-10-07T13:21:38.496Z","repository":{"id":148899794,"uuid":"566308521","full_name":"ibebbs/ScriptVsNewWindow","owner":"ibebbs","description":"Reproduction of timing issue","archived":false,"fork":false,"pushed_at":"2024-07-26T10:11:39.000Z","size":1168,"stargazers_count":0,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-03T08:31:46.087Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ibebbs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-11-15T12:02:10.000Z","updated_at":"2022-11-15T12:02:25.000Z","dependencies_parsed_at":"2023-07-17T00:16:19.070Z","dependency_job_id":null,"html_url":"https://github.com/ibebbs/ScriptVsNewWindow","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ibebbs%2FScriptVsNewWindow","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ibebbs%2FScriptVsNewWindow/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ibebbs%2FScriptVsNewWindow/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ibebbs%2FScriptVsNewWindow/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ibebbs","download_url":"https://codeload.github.com/ibebbs/ScriptVsNewWindow/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240177573,"owners_count":19760392,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-01-03T08:26:29.511Z","updated_at":"2025-10-07T13:21:33.459Z","avatar_url":"https://github.com/ibebbs.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ScriptVsNewWindow\n\nReproduction of timing issue when setting `CoreWebView2NewWindowRequestedEventArgs.NewWindow` vs ensuring scripts are loading prior to navigation (i.e. `CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync`).\n\n# Expected Behavior\nThe 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:\n  1. Has scripts added (via a call to `CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync`) that cause an alert to be shown on document load.\n  2. Is assigned to the `NewWindow` property of `CoreWebView2NewWindowRequestedEventArgs`\n  3. Loads to destination url (https://ian.bebbs.co.uk)\n  4. Fires the JS alert twice (once for core document load, once for an embedded frame).\n\n# Actual Behavior\nBehavior is indeterminate as it seems there is a race condition. Either of the following are common outcomes:\n  1. A blank web page is displayed with scripts added but no alerts as no document is loaded.\n  2. The destination url is displayed but no alerts are displayed as the scripts have not been added.\n\n# Requirements\nThis project requires:\n  1. .NET6 SDK\n  2. An evergreen deployment of WebView2 - at time of writing this is runtime version '107.0.1418.42'.\n\n# Setup\nThe project contains numerous modifiers at the top of the main window. These include:\n  1. Set New Window - Determines whether the new WebView2 instance should be assigned to the `NewWindow` property of `CoreWebView2NewWindowRequestedEventArgs`\n  2. Add Scripts - Determins if and when to call `CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync`:\n      * After - will be called after assigning `CoreWebView2NewWindowRequestedEventArgs.NewWindow` (per documentation [here](https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/win32/icorewebview2newwindowrequestedeventargs?view=webview2-1.0.1418.22#put_newwindow))\n      * Before - will be called before assigning `CoreWebView2NewWindowRequestedEventArgs.NewWindow` (per documentation [here](https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2newwindowrequestedeventargs.newwindow?view=webview2-dotnet-1.0.1418.22#microsoft-web-webview2-core-corewebview2newwindowrequestedeventargs-newwindow))\n      * None - `CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync` will not be called.\n  3. Delay - Adds an optional `await Task.Delay(\u003cmilliseconds\u003e))` after assigning `CoreWebView2NewWindowRequestedEventArgs.NewWindow` as this has been found to change behavior\n  4. Set Source - Specifically assigns the `CoreWebView2NewWindowRequestedEventArgs.Uri` to the new WebView's `Source` property.\n  5. 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\n\n# Reproduction Steps\n## Expected behavior by not assigning `CoreWebView2NewWindowRequestedEventArgs.NewWindow`\n(See ![Expected Behavior](Recordings/Expected Behavior.gif | width=100))\n\n1. Start the app.\n2. Modify controls to the following:\n    * \"Set New Window\" __unchecked__\n    * \"Add Scipts\" set to \"After\"\n    * \"Delay\" set to \"None\"\n    * \"Set Source\" __checked__\n    * \"Schedule New Window\" unchecked\n3. Click the 'Open Window From Target=\"_blank\"' link.\n4. A new window will appear and two JS alerts shown as the content loads.\n\n## Actual behavior by assigning `CoreWebView2NewWindowRequestedEventArgs.NewWindow`\n(See ![Actual Behavior](Recordings/Actual Behavior.gif | width=100) \u0026 ![Actual Behavior with Delay](Recordings/Actual Behavior with Delay.gif | width=100))\n1. Start the app.\n2. Modify controls to the following:\n    * \"Set New Window\" __checked__\n    * \"Add Scipts\" set to \"After\"\n    * \"Delay\" set to \"None\"\n    * \"Set Source\" __unchecked__\n    * \"Schedule New Window\" unchecked\n2. Click the 'Open Window From Target=\"_blank\"' link.\n3. A new window will appear which either:\n    1. Is blank\n    2. Navigates to the destination URL but no JS alerts are shown; i.e. scripts have not been added prior to first document load.\n    3. Navigates to the destination URL but only a single JS alerts is shown; i.e. scripts were not added prior to first document load.\n4. Try various settings for \"Delay\" and observe different outcomes in the previous step","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fibebbs%2Fscriptvsnewwindow","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fibebbs%2Fscriptvsnewwindow","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fibebbs%2Fscriptvsnewwindow/lists"}