ICoreWebView2

Undocumented in source.
version(Windows && inline_webview2_bindings && all)
interface ICoreWebView2 : IUnknown {
static const
GUID iid;
}

Members

Functions

AddHostObjectToScript
HRESULT AddHostObjectToScript(LPCWSTR name, VARIANT* object)

Add the provided host object to script running in the WebView with the specified name. Host objects are exposed as host object proxies using window.chrome.webview.hostObjects.{name}. Host object proxies are promises and resolves to an object representing the host object. The promise is rejected if the app has not added an object with the name. When JavaScript code access a property or method of the object, a promise is return, which resolves to the value returned from the host for the property or method, or rejected in case of error, for example, no property or method on the object or parameters are not valid.

AddScriptToExecuteOnDocumentCreated
HRESULT AddScriptToExecuteOnDocumentCreated(LPCWSTR javaScript, ICoreWebView2AddScriptToExecuteOnDocumentCreatedCompletedHandler handler)

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.

AddWebResourceRequestedFilter
HRESULT AddWebResourceRequestedFilter(LPCWSTR uri, COREWEBVIEW2_WEB_RESOURCE_CONTEXT resourceContext)

Adds a URI and resource context filter for the WebResourceRequested event. A web resource request with a resource context that matches this filter's resource context and a URI that matches this filter's URI wildcard string will be raised via the WebResourceRequested event.

CallDevToolsProtocolMethod
HRESULT CallDevToolsProtocolMethod(LPCWSTR methodName, LPCWSTR parametersAsJson, ICoreWebView2CallDevToolsProtocolMethodCompletedHandler handler)

Runs an asynchronous DevToolsProtocol method. For more information about available methods, navigate to DevTools Protocol Viewer

. The methodName parameter is the full name of the method in the {domain}.{method} format. The parametersAsJson parameter is a JSON formatted string containing the parameters for the corresponding method. The Invoke method of the handler is run when the method asynchronously completes. Invoke is run with the return object of the method as a JSON string. This function returns E_INVALIDARG if the methodName is unknown or the parametersAsJson has an error. In the case of such an error, the returnObjectAsJson parameter of the handler will include information about the error. Note even though WebView2 dispatches the CDP messages in the order called, CDP method calls may be processed out of order. If you require CDP methods to run in a particular order, you should wait for the previous method's completed handler to run before calling the next method.

CapturePreview
HRESULT CapturePreview(COREWEBVIEW2_CAPTURE_PREVIEW_IMAGE_FORMAT imageFormat, IStream* imageStream, ICoreWebView2CapturePreviewCompletedHandler handler)

Capture an image of what WebView is displaying. Specify the format of the image with the imageFormat parameter. The resulting image binary data is written to the provided imageStream parameter. When CapturePreview finishes writing to the stream, the Invoke method on the provided handler parameter is run. This method fails if called before the first ContentLoading event. For example if this is called in the NavigationStarting event for the first navigation it will fail. For subsequent navigations, the method may not fail, but will not capture an image of a given webpage until the ContentLoading event has been fired for it. Any call to this method prior to that will result in a capture of the page being navigated away from.

ExecuteScript
HRESULT ExecuteScript(LPCWSTR javaScript, ICoreWebView2ExecuteScriptCompletedHandler handler)

Run JavaScript code from the javascript parameter in the current top-level document rendered in the WebView. The result of evaluating the provided JavaScript is used in this parameter. The result value is a JSON encoded string. If the result is undefined, contains a reference cycle, or otherwise is not able to be encoded into JSON, then the result is considered to be null, which is encoded in JSON as the string "null".

GetDevToolsProtocolEventReceiver
HRESULT GetDevToolsProtocolEventReceiver(LPCWSTR eventName, ICoreWebView2DevToolsProtocolEventReceiver* receiver)

Get a DevTools Protocol event receiver that allows you to subscribe to a DevTools Protocol event. The eventName parameter is the full name of the event in the format {domain}.{event}. For more information about DevTools Protocol events description and event args, navigate to DevTools Protocol Viewer.

GoBack
HRESULT GoBack()

Navigates the WebView to the previous page in the navigation history.

GoForward
HRESULT GoForward()

Navigates the WebView to the next page in the navigation history.

Navigate
HRESULT Navigate(LPCWSTR uri)

Cause a navigation of the top-level document to run to the specified URI. For more information, navigate to [Navigation events](/microsoft-edge/webview2/concepts/navigation-events).

NavigateToString
HRESULT NavigateToString(LPCWSTR htmlContent)

Initiates a navigation to htmlContent as source HTML of a new document. The htmlContent parameter may not be larger than 2 MB (2 * 1024 * 1024 bytes) in total size. The origin of the new page is about:blank.

OpenDevToolsWindow
HRESULT OpenDevToolsWindow()

Opens the DevTools window for the current document in the WebView. Does nothing if run when the DevTools window is already open.

PostWebMessageAsJson
HRESULT PostWebMessageAsJson(LPCWSTR webMessageAsJson)

Post the specified webMessage to the top level document in this WebView. The main page receives the message by subscribing to the message event of the window.chrome.webview of the page document.

PostWebMessageAsString
HRESULT PostWebMessageAsString(LPCWSTR webMessageAsString)

Posts a message that is a simple string rather than a JSON string representation of a JavaScript object. This behaves in exactly the same manner as PostWebMessageAsJson, but the data property of the event arg of the window.chrome.webview message is a string with the same value as webMessageAsString. Use this instead of PostWebMessageAsJson if you want to communicate using simple strings rather than JSON objects.

Reload
HRESULT Reload()

Reload the current page. This is similar to navigating to the URI of current top level document including all navigation events firing and respecting any entries in the HTTP cache. But, the back or forward history are not modified.

RemoveHostObjectFromScript
HRESULT RemoveHostObjectFromScript(LPCWSTR name)

Remove the host object specified by the name so that it is no longer accessible from JavaScript code in the WebView. While new access attempts are denied, if the object is already obtained by JavaScript code in the WebView, the JavaScript code continues to have access to that object. Run this method for a name that is already removed or never added fails.

RemoveScriptToExecuteOnDocumentCreated
HRESULT RemoveScriptToExecuteOnDocumentCreated(LPCWSTR id)

Remove the corresponding JavaScript added using AddScriptToExecuteOnDocumentCreated with the specified script ID. The script ID should be the one returned by the AddScriptToExecuteOnDocumentCreated. Both use AddScriptToExecuteOnDocumentCreated and this method in NewWindowRequested event handler at the same time sometimes causes trouble. Since invalid scripts will be ignored, the script IDs you got may not be valid anymore.

RemoveWebResourceRequestedFilter
HRESULT RemoveWebResourceRequestedFilter(LPCWSTR uri, COREWEBVIEW2_WEB_RESOURCE_CONTEXT resourceContext)

Removes a matching WebResource filter that was previously added for the WebResourceRequested event. If the same filter was added multiple times, then it must be removed as many times as it was added for the removal to be effective. Returns E_INVALIDARG for a filter that was never added.

Stop
HRESULT Stop()

Stop all navigations and pending resource fetches. Does not stop scripts.

add_ContainsFullScreenElementChanged
HRESULT add_ContainsFullScreenElementChanged(ICoreWebView2ContainsFullScreenElementChangedEventHandler eventHandler, EventRegistrationToken* token)

Add an event handler for the ContainsFullScreenElementChanged event. ContainsFullScreenElementChanged triggers when the ContainsFullScreenElement property changes. An HTML element inside the WebView may enter fullscreen to the size of the WebView or leave fullscreen. This event is useful when, for example, a video element requests to go fullscreen. The listener of ContainsFullScreenElementChanged may resize the WebView in response.

add_ContentLoading
HRESULT add_ContentLoading(ICoreWebView2ContentLoadingEventHandler eventHandler, EventRegistrationToken* token)

Add an event handler for the ContentLoading event. ContentLoading triggers before any content is loaded, including scripts added with AddScriptToExecuteOnDocumentCreated. ContentLoading does not trigger if a same page navigation occurs (such as through fragment navigations or history.pushState navigations). This operation follows the NavigationStarting and SourceChanged events and precedes the HistoryChanged and NavigationCompleted events.

add_DocumentTitleChanged
HRESULT add_DocumentTitleChanged(ICoreWebView2DocumentTitleChangedEventHandler eventHandler, EventRegistrationToken* token)

Add an event handler for the DocumentTitleChanged event. DocumentTitleChanged runs when the DocumentTitle property of the WebView changes and may run before or after the NavigationCompleted event.

add_FrameNavigationCompleted
HRESULT add_FrameNavigationCompleted(ICoreWebView2NavigationCompletedEventHandler eventHandler, EventRegistrationToken* token)

Add an event handler for the FrameNavigationCompleted event. FrameNavigationCompleted triggers when a child frame has completely loaded (concurrently when body.onload has triggered) or loading stopped with error.

add_FrameNavigationStarting
HRESULT add_FrameNavigationStarting(ICoreWebView2NavigationStartingEventHandler eventHandler, EventRegistrationToken* token)

Add an event handler for the FrameNavigationStarting event. FrameNavigationStarting triggers when a child frame in the WebView requests permission to navigate to a different URI. Redirects trigger this operation as well, and the navigation id is the same as the original one.

add_HistoryChanged
HRESULT add_HistoryChanged(ICoreWebView2HistoryChangedEventHandler eventHandler, EventRegistrationToken* token)

Add an event handler for the HistoryChanged event. HistoryChanged is raised for changes to joint session history, which consists of top-level and manual frame navigations. Use HistoryChanged to verify that the CanGoBack or CanGoForward value has changed. HistoryChanged also runs for using GoBack or GoForward. HistoryChanged runs after SourceChanged and ContentLoading. CanGoBack is false for navigations initiated through ICoreWebView2Frame APIs if there has not yet been a user gesture.

add_NavigationCompleted
HRESULT add_NavigationCompleted(ICoreWebView2NavigationCompletedEventHandler eventHandler, EventRegistrationToken* token)

Add an event handler for the NavigationCompleted event. NavigationCompleted runs when the WebView has completely loaded (concurrently when body.onload runs) or loading stopped with error.

add_NavigationStarting
HRESULT add_NavigationStarting(ICoreWebView2NavigationStartingEventHandler eventHandler, EventRegistrationToken* token)

Add an event handler for the NavigationStarting event. NavigationStarting runs when the WebView main frame is requesting permission to navigate to a different URI. Redirects trigger this operation as well, and the navigation id is the same as the original one.

add_NewWindowRequested
HRESULT add_NewWindowRequested(ICoreWebView2NewWindowRequestedEventHandler eventHandler, EventRegistrationToken* token)

Add an event handler for the NewWindowRequested event. NewWindowRequested runs when content inside the WebView requests to open a new window, such as through window.open. The app can pass a target WebView that is considered the opened window or mark the event as Handled, in which case WebView2 does not open a window. If either Handled or NewWindow properties are not set, the target content will be opened on a popup window.

add_PermissionRequested
HRESULT add_PermissionRequested(ICoreWebView2PermissionRequestedEventHandler eventHandler, EventRegistrationToken* token)

Add an event handler for the PermissionRequested event. PermissionRequested runs when content in a WebView requests permission to access some privileged resources.

add_ProcessFailed
HRESULT add_ProcessFailed(ICoreWebView2ProcessFailedEventHandler eventHandler, EventRegistrationToken* token)

Add an event handler for the ProcessFailed event. ProcessFailed runs when any of the processes in the WebView2 Process Group

encounters one of the following conditions:

add_ScriptDialogOpening
HRESULT add_ScriptDialogOpening(ICoreWebView2ScriptDialogOpeningEventHandler eventHandler, EventRegistrationToken* token)

Add an event handler for the ScriptDialogOpening event. ScriptDialogOpening runs when a JavaScript dialog (alert, confirm, prompt, or beforeunload) displays for the webview. This event only triggers if the ICoreWebView2Settings::AreDefaultScriptDialogsEnabled property is set to FALSE. The ScriptDialogOpening event suppresses dialogs or replaces default dialogs with custom dialogs.

add_SourceChanged
HRESULT add_SourceChanged(ICoreWebView2SourceChangedEventHandler eventHandler, EventRegistrationToken* token)

Add an event handler for the SourceChanged event. SourceChanged triggers when the Source property changes. SourceChanged runs when navigating to a different site or fragment navigations. It does not trigger for other types of navigations such as page refreshes or history.pushState with the same URL as the current page. SourceChanged runs before ContentLoading for navigation to a new document.

add_WebMessageReceived
HRESULT add_WebMessageReceived(ICoreWebView2WebMessageReceivedEventHandler handler, EventRegistrationToken* token)

Add an event handler for the WebMessageReceived event. WebMessageReceived runs when the ICoreWebView2Settings::IsWebMessageEnabled setting is set and the top-level document of the WebView runs window.chrome.webview.postMessage. The postMessage function is void postMessage(object) where object is any object supported by JSON conversion.

add_WebResourceRequested
HRESULT add_WebResourceRequested(ICoreWebView2WebResourceRequestedEventHandler eventHandler, EventRegistrationToken* token)

Add an event handler for the WebResourceRequested event. WebResourceRequested runs when the WebView is performing a URL request to a matching URL and resource context filter that was added with AddWebResourceRequestedFilter. At least one filter must be added for the event to run.

add_WindowCloseRequested
HRESULT add_WindowCloseRequested(ICoreWebView2WindowCloseRequestedEventHandler eventHandler, EventRegistrationToken* token)

Add an event handler for the WindowCloseRequested event. WindowCloseRequested triggers when content inside the WebView requested to close the window, such as after window.close is run. The app should close the WebView and related app window if that makes sense to the app.

get_BrowserProcessId
HRESULT get_BrowserProcessId(UINT32* value)

The process ID of the browser process that hosts the WebView.

get_CanGoBack
HRESULT get_CanGoBack(BOOL* canGoBack)

TRUE if the WebView is able to navigate to a previous page in the navigation history. If CanGoBack changes value, the HistoryChanged event runs.

get_CanGoForward
HRESULT get_CanGoForward(BOOL* canGoForward)

TRUE if the WebView is able to navigate to a next page in the navigation history. If CanGoForward changes value, the HistoryChanged event runs.

get_ContainsFullScreenElement
HRESULT get_ContainsFullScreenElement(BOOL* containsFullScreenElement)

Indicates if the WebView contains a fullscreen HTML element.

get_DocumentTitle
HRESULT get_DocumentTitle(LPWSTR* title)

The title for the current top-level document. If the document has no explicit title or is otherwise empty, a default that may or may not match the URI of the document is used.

get_Settings
HRESULT get_Settings(ICoreWebView2Settings* settings)

The ICoreWebView2Settings object contains various modifiable settings for the running WebView.

get_Source
HRESULT get_Source(LPWSTR* uri)

The URI of the current top level document. This value potentially changes as a part of the SourceChanged event that runs for some cases such as navigating to a different site or fragment navigations. It remains the same for other types of navigations such as page refreshes or history.pushState with the same URL as the current page.

remove_ContainsFullScreenElementChanged
HRESULT remove_ContainsFullScreenElementChanged(EventRegistrationToken token)

Remove an event handler previously added with add_ContainsFullScreenElementChanged.

remove_ContentLoading
HRESULT remove_ContentLoading(EventRegistrationToken token)

Remove an event handler previously added with add_ContentLoading.

remove_DocumentTitleChanged
HRESULT remove_DocumentTitleChanged(EventRegistrationToken token)

Remove an event handler previously added with add_DocumentTitleChanged.

remove_FrameNavigationCompleted
HRESULT remove_FrameNavigationCompleted(EventRegistrationToken token)

Remove an event handler previously added with add_FrameNavigationCompleted.

remove_FrameNavigationStarting
HRESULT remove_FrameNavigationStarting(EventRegistrationToken token)

Remove an event handler previously added with add_FrameNavigationStarting.

remove_HistoryChanged
HRESULT remove_HistoryChanged(EventRegistrationToken token)

Remove an event handler previously added with add_HistoryChanged.

remove_NavigationCompleted
HRESULT remove_NavigationCompleted(EventRegistrationToken token)

Remove an event handler previously added with add_NavigationCompleted.

remove_NavigationStarting
HRESULT remove_NavigationStarting(EventRegistrationToken token)

Remove an event handler previously added with add_NavigationStarting.

remove_NewWindowRequested
HRESULT remove_NewWindowRequested(EventRegistrationToken token)

Remove an event handler previously added with add_NewWindowRequested.

remove_PermissionRequested
HRESULT remove_PermissionRequested(EventRegistrationToken token)

Remove an event handler previously added with add_PermissionRequested.

remove_ProcessFailed
HRESULT remove_ProcessFailed(EventRegistrationToken token)

Remove an event handler previously added with add_ProcessFailed.

remove_ScriptDialogOpening
HRESULT remove_ScriptDialogOpening(EventRegistrationToken token)

Remove an event handler previously added with add_ScriptDialogOpening.

remove_SourceChanged
HRESULT remove_SourceChanged(EventRegistrationToken token)

Remove an event handler previously added with add_SourceChanged.

remove_WebMessageReceived
HRESULT remove_WebMessageReceived(EventRegistrationToken token)

Remove an event handler previously added with add_WebMessageReceived.

remove_WebResourceRequested
HRESULT remove_WebResourceRequested(EventRegistrationToken token)

Remove an event handler previously added with add_WebResourceRequested.

remove_WindowCloseRequested
HRESULT remove_WindowCloseRequested(EventRegistrationToken token)

Remove an event handler previously added with add_WindowCloseRequested.

Meta