ICoreWebView2.AddScriptToExecuteOnDocumentCreated

Add the provided JavaScript to a list of scripts that should be run after the global object has been created, but before the HTML document has been parsed and before any other script included by the HTML document is run. This method injects a script that runs on all top-level document and child frame page navigations. This method runs asynchronously, and you must wait for the completion handler to finish before the injected script is ready to run. When this method completes, the Invoke method of the handler is run with the id of the injected script. id is a string. To remove the injected script, use RemoveScriptToExecuteOnDocumentCreated.

If the method is run in add_NewWindowRequested handler it should be called before the new window is set. If called after setting the NewWindow property, the initial script may or may not apply to the initial navigation and may only apply to the subsequent navigation. For more details see ICoreWebView2NewWindowRequestedEventArgs::put_NewWindow.

\> [!NOTE]\n\> If an HTML document is running in a sandbox of some kind using sandbox

properties or the Content-Security-Policy

HTTP header affects the script that runs. For example, if the allow-modals keyword is not set then requests to run the alert function are ignored.

\snippet ScriptComponent.cpp AddScriptToExecuteOnDocumentCreated

interface ICoreWebView2
HRESULT
AddScriptToExecuteOnDocumentCreated

Meta