interface ICoreWebView2
Note
This reference is no longer being maintained. For the latest API reference, see WebView2 API Reference.
interface ICoreWebView2
: public IUnknown
WebView2 enables you to host web content using the latest Edge web browser technology.
Summary
Members | Descriptions |
---|---|
get_Settings | The ICoreWebView2Settings object contains various modifiable settings for the running WebView. |
get_Source | The URI of the current top level document. |
Navigate | Cause a navigation of the top level document to the specified URI. |
NavigateToString | Initiates a navigation to htmlContent as source HTML of a new document. |
add_NavigationStarting | Add an event handler for the NavigationStarting event. |
remove_NavigationStarting | Remove an event handler previously added with add_NavigationStarting. |
add_ContentLoading | Add an event handler for the ContentLoading event. |
remove_ContentLoading | Remove an event handler previously added with add_ContentLoading. |
add_SourceChanged | SourceChanged fires when the Source property changes. |
remove_SourceChanged | Remove an event handler previously added with add_SourceChanged. |
add_HistoryChanged | HistoryChange listen to the change of navigation history for the top level document. |
remove_HistoryChanged | Remove an event handler previously added with add_HistoryChanged. |
add_NavigationCompleted | Add an event handler for the NavigationCompleted event. |
remove_NavigationCompleted | Remove an event handler previously added with add_NavigationCompleted. |
add_FrameNavigationStarting | Add an event handler for the FrameNavigationStarting event. |
remove_FrameNavigationStarting | Remove an event handler previously added with add_FrameNavigationStarting. |
add_ScriptDialogOpening | Add an event handler for the ScriptDialogOpening event. |
remove_ScriptDialogOpening | Remove an event handler previously added with add_ScriptDialogOpening. |
add_PermissionRequested | Add an event handler for the PermissionRequested event. |
remove_PermissionRequested | Remove an event handler previously added with add_PermissionRequested. |
add_ProcessFailed | Add an event handler for the ProcessFailed event. |
remove_ProcessFailed | Remove an event handler previously added with add_ProcessFailed. |
AddScriptToExecuteOnDocumentCreated | Add the provided JavaScript to a list of scripts that should be executed 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 executed. |
RemoveScriptToExecuteOnDocumentCreated | Remove the corresponding JavaScript added via AddScriptToExecuteOnDocumentCreated. |
ExecuteScript | Execute JavaScript code from the javascript parameter in the current top level document rendered in the WebView. |
CapturePreview | Capture an image of what WebView is displaying. |
Reload | Reload the current page. |
PostWebMessageAsJson | Post the specified webMessage to the top level document in this WebView. |
PostWebMessageAsString | This is a helper for posting a message that is a simple string rather than a JSON string representation of a JavaScript object. |
add_WebMessageReceived | This event fires when the IsWebMessageEnabled setting is set and the top level document of the webview calls window.chrome.webview.postMessage . |
remove_WebMessageReceived | Remove an event handler previously added with add_WebMessageReceived. |
CallDevToolsProtocolMethod | Call an asynchronous DevToolsProtocol method. |
get_BrowserProcessId | The process id of the browser process that hosts the WebView. |
get_CanGoBack | Returns true if the webview can navigate to a previous page in the navigation history. |
get_CanGoForward | Returns true if the webview can navigate to a next page in the navigation history. |
GoBack | Navigates the WebView to the previous page in the navigation history. |
GoForward | Navigates the WebView to the next page in the navigation history. |
GetDevToolsProtocolEventReceiver | Get a DevTools Protocol event receiver that allows you to subscribe to a DevTools Protocol event. |
Stop | Stop all navigations and pending resource fetches. |
add_NewWindowRequested | Add an event handler for the NewWindowRequested event. |
remove_NewWindowRequested | Remove an event handler previously added with add_NewWindowRequested. |
add_DocumentTitleChanged | Add an event handler for the DocumentTitleChanged event. |
remove_DocumentTitleChanged | Remove an event handler previously added with add_DocumentTitleChanged. |
get_DocumentTitle | The title for the current top level document. |
AddRemoteObject | Add the provided host object to script running in the WebView with the specified name. |
RemoveRemoteObject | Remove the host object specified by the name so that it is no longer accessible from JavaScript code in the WebView. |
OpenDevToolsWindow | Opens the DevTools window for the current document in the WebView. |
add_ContainsFullScreenElementChanged | Notifies when the ContainsFullScreenElement property changes. |
remove_ContainsFullScreenElementChanged | Remove an event handler previously added with the corresponding add_ event method. |
get_ContainsFullScreenElement | Indicates if the WebView contains a fullscreen HTML element. |
add_WebResourceRequested | Add an event handler for the WebResourceRequested event. |
remove_WebResourceRequested | Remove an event handler previously added with add_WebResourceRequested. |
AddWebResourceRequestedFilter | Adds a URI and resource context filter to the WebResourceRequested event. |
RemoveWebResourceRequestedFilter | Removes a matching WebResource filter that was previously added for the WebResourceRequested event. |
add_WindowCloseRequested | Add an event handler for the WindowCloseRequested event. |
remove_WindowCloseRequested | Remove an event handler previously added with add_WindowCloseRequested. |
CORE_WEBVIEW2_CAPTURE_PREVIEW_IMAGE_FORMAT | Image format used by the ICoreWebView2::CapturePreview method. |
CORE_WEBVIEW2_SCRIPT_DIALOG_KIND | Kind of JavaScript dialog used in the ICoreWebView2ScriptDialogOpeningEventHandler interface. |
CORE_WEBVIEW2_PROCESS_FAILED_KIND | Kind of process failure used in the ICoreWebView2ProcessFailedEventHandler interface. |
CORE_WEBVIEW2_PERMISSION_KIND | The type of a permission request. |
CORE_WEBVIEW2_PERMISSION_STATE | Response to a permission request. |
CORE_WEBVIEW2_WEB_ERROR_STATUS | Error status values for web navigations. |
CORE_WEBVIEW2_WEB_RESOURCE_CONTEXT | Enum for web resource request contexts. |
Members
get_Settings
The ICoreWebView2Settings object contains various modifiable settings for the running WebView.
public HRESULT get_Settings(ICoreWebView2Settings ** settings)
get_Source
The URI of the current top level document.
public HRESULT get_Source(LPWSTR * uri)
This value potentially changes as a part of the SourceChanged event firing for some cases such as navigating to a different site or fragment navigations. It will remain the same for other types of navigations such as page reloads or history.pushState with the same URL as the current page.
// Register a handler for the SourceChanged event.
// This handler will read the webview's source URI and update
// the app's address bar.
CHECK_FAILURE(m_webView->add_SourceChanged(
Callback<ICoreWebView2SourceChangedEventHandler>(
[this](ICoreWebView2* sender, ICoreWebView2SourceChangedEventArgs* args)
-> HRESULT {
wil::unique_cotaskmem_string uri;
sender->get_Source(&uri);
if (wcscmp(uri.get(), L"about:blank") == 0)
{
uri = wil::make_cotaskmem_string(L"");
}
SetWindowText(m_toolbar->addressBarWindow, uri.get());
return S_OK;
})
.Get(),
&m_sourceChangedToken));
Navigate
Cause a navigation of the top level document to the specified URI.
public HRESULT Navigate(LPCWSTR uri)
See the navigation events for more information. Note that this starts a navigation and the corresponding NavigationStarting event will fire sometime after this Navigate call completes.
void ControlComponent::NavigateToAddressBar()
{
int length = GetWindowTextLength(m_toolbar->addressBarWindow);
std::wstring uri(length, 0);
PWSTR buffer = const_cast<PWSTR>(uri.data());
GetWindowText(m_toolbar->addressBarWindow, buffer, length + 1);
HRESULT hr = m_webView->Navigate(uri.c_str());
if (hr == E_INVALIDARG)
{
// An invalid URI was provided.
if (uri.find(L' ') == std::wstring::npos
&& uri.find(L'.') != std::wstring::npos)
{
// If it contains a dot and no spaces, try tacking http:// on the front.
hr = m_webView->Navigate((L"http://" + uri).c_str());
}
else
{
// Otherwise treat it as a web search. We aren't bothering to escape
// URL syntax characters such as & and #
hr = m_webView->Navigate((L"https://bing.com/search?q=" + uri).c_str());
}
}
if (hr != E_INVALIDARG) {
CHECK_FAILURE(hr);
}
}
NavigateToString
Initiates a navigation to htmlContent as source HTML of a new document.
public HRESULT NavigateToString(LPCWSTR htmlContent)
The htmlContent parameter may not be larger than 2 MB of characters. The origin of the new page will be about:blank.
static const PCWSTR htmlContent =
L"<h1>Domain Blocked</h1>"
L"<p>You've attempted to navigate to a domain in the blocked "
L"sites list. Press back to return to the previous page.</p>";
CHECK_FAILURE(sender->NavigateToString(htmlContent));
add_NavigationStarting
Add an event handler for the NavigationStarting event.
public HRESULT add_NavigationStarting(ICoreWebView2NavigationStartingEventHandler * eventHandler,EventRegistrationToken * token)
NavigationStarting fires when the WebView main frame is requesting permission to navigate to a different URI. This will fire for redirects as well.
// Register a handler for the NavigationStarting event.
// This handler will check the domain being navigated to, and if the domain
// matches a list of blocked sites, it will cancel the navigation and
// possibly display a warning page. It will also disable JavaScript on
// selected websites.
CHECK_FAILURE(m_webView->add_NavigationStarting(
Callback<ICoreWebView2NavigationStartingEventHandler>(
[this](ICoreWebView2* sender,
ICoreWebView2NavigationStartingEventArgs* args) -> HRESULT
{
wil::unique_cotaskmem_string uri;
CHECK_FAILURE(args->get_Uri(&uri));
if (ShouldBlockUri(uri.get()))
{
CHECK_FAILURE(args->put_Cancel(true));
// If the user clicked a link to navigate, show a warning page.
BOOL userInitiated;
CHECK_FAILURE(args->get_IsUserInitiated(&userInitiated));
static const PCWSTR htmlContent =
L"<h1>Domain Blocked</h1>"
L"<p>You've attempted to navigate to a domain in the blocked "
L"sites list. Press back to return to the previous page.</p>";
CHECK_FAILURE(sender->NavigateToString(htmlContent));
}
// Changes to settings will apply at the next navigation, which includes the
// navigation after a NavigationStarting event. We can use this to change
// settings according to what site we're visiting.
if (ShouldBlockScriptForUri(uri.get()))
{
m_settings->put_IsScriptEnabled(FALSE);
}
else
{
m_settings->put_IsScriptEnabled(m_isScriptEnabled);
}
return S_OK;
}).Get(), &m_navigationStartingToken));
remove_NavigationStarting
Remove an event handler previously added with add_NavigationStarting.
public HRESULT remove_NavigationStarting(EventRegistrationToken token)
add_ContentLoading
Add an event handler for the ContentLoading event.
public HRESULT add_ContentLoading(ICoreWebView2ContentLoadingEventHandler * eventHandler,EventRegistrationToken * token)
ContentLoading fires before any content is loaded, including scripts added with AddScriptToExecuteOnDocumentCreated ContentLoading will not fire if a same page navigation occurs (such as through fragment navigations or history.pushState navigations). This follows the NavigationStarting and SourceChanged events and precedes the HistoryChanged and NavigationCompleted events.
remove_ContentLoading
Remove an event handler previously added with add_ContentLoading.
public HRESULT remove_ContentLoading(EventRegistrationToken token)
add_SourceChanged
SourceChanged fires when the Source property changes.
public HRESULT add_SourceChanged(ICoreWebView2SourceChangedEventHandler * eventHandler,EventRegistrationToken * token)
SourceChanged fires for navigating to a different site or fragment navigations. It will not fires for other types of navigations such as page reloads or history.pushState with the same URL as the current page. SourceChanged fires before ContentLoading for navigation to a new document. Add an event handler for the SourceChanged event.
// Register a handler for the SourceChanged event.
// This handler will read the webview's source URI and update
// the app's address bar.
CHECK_FAILURE(m_webView->add_SourceChanged(
Callback<ICoreWebView2SourceChangedEventHandler>(
[this](ICoreWebView2* sender, ICoreWebView2SourceChangedEventArgs* args)
-> HRESULT {
wil::unique_cotaskmem_string uri;
sender->get_Source(&uri);
if (wcscmp(uri.get(), L"about:blank") == 0)
{
uri = wil::make_cotaskmem_string(L"");
}
SetWindowText(m_toolbar->addressBarWindow, uri.get());
return S_OK;
})
.Get(),
&m_sourceChangedToken));
remove_SourceChanged
Remove an event handler previously added with add_SourceChanged.
public HRESULT remove_SourceChanged(EventRegistrationToken token)
add_HistoryChanged
HistoryChange listen to the change of navigation history for the top level document.
public HRESULT add_HistoryChanged(ICoreWebView2HistoryChangedEventHandler * eventHandler,EventRegistrationToken * token)
Use HistoryChange to check if get_CanGoBack/get_CanGoForward value has changed. HistoryChanged also fires for using GoBack/GoForward. HistoryChanged fires after SourceChanged and ContentLoading. Add an event handler for the HistoryChanged event.
// Register a handler for the HistoryChanged event.
// Update the Back, Forward buttons.
CHECK_FAILURE(m_webView->add_HistoryChanged(
Callback<ICoreWebView2HistoryChangedEventHandler>(
[this](ICoreWebView2* sender, IUnknown* args) -> HRESULT {
BOOL canGoBack;
BOOL canGoForward;
sender->get_CanGoBack(&canGoBack);
sender->get_CanGoForward(&canGoForward);
EnableWindow(m_toolbar->backWindow, canGoBack);
EnableWindow(m_toolbar->forwardWindow, canGoForward);
return S_OK;
})
.Get(),
&m_historyChangedToken));
remove_HistoryChanged
Remove an event handler previously added with add_HistoryChanged.
public HRESULT remove_HistoryChanged(EventRegistrationToken token)
add_NavigationCompleted
Add an event handler for the NavigationCompleted event.
public HRESULT add_NavigationCompleted(ICoreWebView2NavigationCompletedEventHandler * eventHandler,EventRegistrationToken * token)
NavigationCompleted event fires when the WebView has completely loaded (body.onload has fired) or loading stopped with error.
// Register a handler for the NavigationCompleted event.
// Check whether the navigation succeeded, and if not, do something.
// Also update the Cancel buttons.
CHECK_FAILURE(m_webView->add_NavigationCompleted(
Callback<ICoreWebView2NavigationCompletedEventHandler>(
[this](ICoreWebView2* sender, ICoreWebView2NavigationCompletedEventArgs* args)
-> HRESULT {
BOOL success;
CHECK_FAILURE(args->get_IsSuccess(&success));
if (!success)
{
CORE_WEBVIEW2_WEB_ERROR_STATUS webErrorStatus;
CHECK_FAILURE(args->get_WebErrorStatus(&webErrorStatus));
if (webErrorStatus == CORE_WEBVIEW2_WEB_ERROR_STATUS_DISCONNECTED)
{
// Do something here if you want to handle a specific error case.
// In most cases this isn't necessary, because the WebView will
// display its own error page automatically.
}
}
EnableWindow(m_toolbar->cancelWindow, FALSE);
return S_OK;
})
.Get(),
&m_navigationCompletedToken));
remove_NavigationCompleted
Remove an event handler previously added with add_NavigationCompleted.
public HRESULT remove_NavigationCompleted(EventRegistrationToken token)
add_FrameNavigationStarting
Add an event handler for the FrameNavigationStarting event.
public HRESULT add_FrameNavigationStarting(ICoreWebView2NavigationStartingEventHandler * eventHandler,EventRegistrationToken * token)
FrameNavigationStarting fires when a child frame in the WebView requesting permission to navigate to a different URI. This will fire for redirects as well.
// Register a handler for the FrameNavigationStarting event.
// This handler will prevent a frame from navigating to a blocked domain.
CHECK_FAILURE(m_webView->add_FrameNavigationStarting(
Callback<ICoreWebView2NavigationStartingEventHandler>(
[this](ICoreWebView2* sender,
ICoreWebView2NavigationStartingEventArgs* args) -> HRESULT
{
wil::unique_cotaskmem_string uri;
CHECK_FAILURE(args->get_Uri(&uri));
if (ShouldBlockUri(uri.get()))
{
CHECK_FAILURE(args->put_Cancel(true));
}
return S_OK;
}).Get(), &m_frameNavigationStartingToken));
remove_FrameNavigationStarting
Remove an event handler previously added with add_FrameNavigationStarting.
public HRESULT remove_FrameNavigationStarting(EventRegistrationToken token)
add_ScriptDialogOpening
Add an event handler for the ScriptDialogOpening event.
public HRESULT add_ScriptDialogOpening(ICoreWebView2ScriptDialogOpeningEventHandler * eventHandler,EventRegistrationToken * token)
The event fires when a JavaScript dialog (alert, confirm, or prompt) will show for the webview. This event only fires if the ICoreWebView2Settings::AreDefaultScriptDialogsEnabled property is set to false. The ScriptDialogOpening event can be used to suppress dialogs or replace default dialogs with custom dialogs.
// Register a handler for the ScriptDialogOpening event.
// This handler will set up a custom prompt dialog for the user,
// and may defer the event if the setting to defer dialogs is enabled.
CHECK_FAILURE(m_webView->add_ScriptDialogOpening(
Callback<ICoreWebView2ScriptDialogOpeningEventHandler>(
[this](
ICoreWebView2* sender,
ICoreWebView2ScriptDialogOpeningEventArgs* args) -> HRESULT
{
wil::com_ptr<ICoreWebView2ScriptDialogOpeningEventArgs> eventArgs = args;
auto showDialog = [this, eventArgs]
{
wil::unique_cotaskmem_string uri;
CORE_WEBVIEW2_SCRIPT_DIALOG_KIND type;
wil::unique_cotaskmem_string message;
wil::unique_cotaskmem_string defaultText;
CHECK_FAILURE(eventArgs->get_Uri(&uri));
CHECK_FAILURE(eventArgs->get_Kind(&type));
CHECK_FAILURE(eventArgs->get_Message(&message));
CHECK_FAILURE(eventArgs->get_DefaultText(&defaultText));
std::wstring promptString = std::wstring(L"The page at '")
+ uri.get() + L"' says:";
TextInputDialog dialog(
m_appWindow->GetMainWindow(),
L"Script Dialog",
promptString.c_str(),
message.get(),
defaultText.get(),
/* readonly */ type != CORE_WEBVIEW2_SCRIPT_DIALOG_KIND_PROMPT);
if (dialog.confirmed)
{
CHECK_FAILURE(eventArgs->put_ResultText(dialog.input.c_str()));
CHECK_FAILURE(eventArgs->Accept());
}
};
if (m_deferScriptDialogs)
{
wil::com_ptr<ICoreWebView2Deferral> deferral;
CHECK_FAILURE(args->GetDeferral(&deferral));
m_completeDeferredDialog = [showDialog, deferral]
{
showDialog();
CHECK_FAILURE(deferral->Complete());
};
}
else
{
showDialog();
}
return S_OK;
}).Get(), &m_scriptDialogOpeningToken));
remove_ScriptDialogOpening
Remove an event handler previously added with add_ScriptDialogOpening.
public HRESULT remove_ScriptDialogOpening(EventRegistrationToken token)
add_PermissionRequested
Add an event handler for the PermissionRequested event.
public HRESULT add_PermissionRequested(ICoreWebView2PermissionRequestedEventHandler * eventHandler,EventRegistrationToken * token)
Fires when content in a WebView requests permission to access some privileged resources.
// Register a handler for the PermissionRequested event.
// This handler prompts the user to allow or deny the request.
CHECK_FAILURE(m_webView->add_PermissionRequested(
Callback<ICoreWebView2PermissionRequestedEventHandler>(
[this](
ICoreWebView2* sender,
ICoreWebView2PermissionRequestedEventArgs* args) -> HRESULT
{
wil::unique_cotaskmem_string uri;
CORE_WEBVIEW2_PERMISSION_KIND kind = CORE_WEBVIEW2_PERMISSION_KIND_UNKNOWN_PERMISSION;
BOOL userInitiated = FALSE;
CHECK_FAILURE(args->get_Uri(&uri));
CHECK_FAILURE(args->get_PermissionKind(&kind));
CHECK_FAILURE(args->get_IsUserInitiated(&userInitiated));
std::wstring message = L"Do you want to grant permission for ";
message += NameOfPermissionKind(kind);
message += L" to the website at ";
message += uri.get();
message += L"?\n\n";
message += (userInitiated
? L"This request came from a user gesture."
: L"This request did not come from a user gesture.");
int response = MessageBox(nullptr, message.c_str(), L"Permission Request",
MB_YESNOCANCEL | MB_ICONWARNING);
CORE_WEBVIEW2_PERMISSION_STATE state =
response == IDYES ? CORE_WEBVIEW2_PERMISSION_STATE_ALLOW
: response == IDNO ? CORE_WEBVIEW2_PERMISSION_STATE_DENY
: CORE_WEBVIEW2_PERMISSION_STATE_DEFAULT;
CHECK_FAILURE(args->put_State(state));
return S_OK;
}).Get(), &m_permissionRequestedToken));
remove_PermissionRequested
Remove an event handler previously added with add_PermissionRequested.
public HRESULT remove_PermissionRequested(EventRegistrationToken token)
add_ProcessFailed
Add an event handler for the ProcessFailed event.
public HRESULT add_ProcessFailed(ICoreWebView2ProcessFailedEventHandler * eventHandler,EventRegistrationToken * token)
Fires when a WebView process terminated unexpectedly or become unresponsive.
// Register a handler for the ProcessFailed event.
// This handler checks if the browser process failed, and asks the user if
// they want to recreate the webview.
CHECK_FAILURE(m_webView->add_ProcessFailed(
Callback<ICoreWebView2ProcessFailedEventHandler>(
[this](ICoreWebView2* sender,
ICoreWebView2ProcessFailedEventArgs* args) -> HRESULT
{
CORE_WEBVIEW2_PROCESS_FAILED_KIND failureType;
CHECK_FAILURE(args->get_ProcessFailedKind(&failureType));
if (failureType == CORE_WEBVIEW2_PROCESS_FAILED_KIND_BROWSER_PROCESS_EXITED)
{
int button = MessageBox(
m_appWindow->GetMainWindow(),
L"Browser process exited unexpectedly. Recreate webview?",
L"Browser process exited",
MB_YESNO);
if (button == IDYES)
{
m_appWindow->ReinitializeWebView();
}
}
else if (failureType == CORE_WEBVIEW2_PROCESS_FAILED_KIND_RENDER_PROCESS_UNRESPONSIVE)
{
int button = MessageBox(
m_appWindow->GetMainWindow(),
L"Browser render process has stopped responding. Recreate webview?",
L"Web page unresponsive", MB_YESNO);
if (button == IDYES)
{
m_appWindow->ReinitializeWebView();
}
}
return S_OK;
}).Get(), &m_processFailedToken));
remove_ProcessFailed
Remove an event handler previously added with add_ProcessFailed.
public HRESULT remove_ProcessFailed(EventRegistrationToken token)
AddScriptToExecuteOnDocumentCreated
Add the provided JavaScript to a list of scripts that should be executed 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 executed.
public HRESULT AddScriptToExecuteOnDocumentCreated(LPCWSTR javaScript,ICoreWebView2AddScriptToExecuteOnDocumentCreatedCompletedHandler * handler)
The injected script will apply to all future top level document and child frame navigations until removed with RemoveScriptToExecuteOnDocumentCreated. This is applied asynchronously and you must wait for the completion handler to run before you can be sure that the script is ready to execute on future navigations.
Note that if an HTML document has sandboxing of some kind via sandbox properties or the Content-Security-Policy HTTP header this will affect the script run here. So, for example, if the 'allow-modals' keyword is not set then calls to the alert
function will be ignored.
// Prompt the user for some script and register it to execute whenever a new page loads.
void ScriptComponent::AddInitializeScript()
{
TextInputDialog dialog(
m_appWindow->GetMainWindow(),
L"Add Initialize Script",
L"Initialization Script:",
L"Enter the JavaScript code to run as the initialization script that "
L"runs before any script in the HTML document.",
// This example script stops child frames from opening new windows. Because
// the initialization script runs before any script in the HTML document, we
// can trust the results of our checks on window.parent and window.top.
L"if (window.parent !== window.top) {\r\n"
L" delete window.open;\r\n"
L"}");
if (dialog.confirmed)
{
m_webView->AddScriptToExecuteOnDocumentCreated(
dialog.input.c_str(),
Callback<ICoreWebView2AddScriptToExecuteOnDocumentCreatedCompletedHandler>(
[this](HRESULT error, PCWSTR id) -> HRESULT
{
m_lastInitializeScriptId = id;
MessageBox(nullptr, id, L"AddScriptToExecuteOnDocumentCreated Id", MB_OK);
return S_OK;
}).Get());
}
}
RemoveScriptToExecuteOnDocumentCreated
Remove the corresponding JavaScript added via AddScriptToExecuteOnDocumentCreated.
public HRESULT RemoveScriptToExecuteOnDocumentCreated(LPCWSTR id)
ExecuteScript
Execute JavaScript code from the javascript parameter in the current top level document rendered in the WebView.
public HRESULT ExecuteScript(LPCWSTR javaScript,ICoreWebView2ExecuteScriptCompletedHandler * handler)
This will execute asynchronously and when complete, if a handler is provided in the ExecuteScriptCompletedHandler parameter, its Invoke method will be called with the result of evaluating the provided JavaScript. The result value is a JSON encoded string. If the result is undefined, contains a reference cycle, or otherwise cannot be encoded into JSON, the JSON null value will be returned as the string 'null'. Note that a function that has no explicit return value returns undefined. If the executed script throws an unhandled exception, then the result is also 'null'. This method is applied asynchronously. If the call is made while the webview is on one document, and a navigation occurs after the call is made but before the JavaScript is executed, then the script will not be executed and the handler will be called with E_FAIL for its errorCode parameter. ExecuteScript will work even if IsScriptEnabled is set to FALSE.
// Prompt the user for some script and then execute it.
void ScriptComponent::InjectScript()
{
TextInputDialog dialog(
m_appWindow->GetMainWindow(),
L"Inject Script",
L"Enter script code:",
L"Enter the JavaScript code to run in the webview.",
L"window.getComputedStyle(document.body).backgroundColor");
if (dialog.confirmed)
{
m_webView->ExecuteScript(dialog.input.c_str(),
Callback<ICoreWebView2ExecuteScriptCompletedHandler>(
[](HRESULT error, PCWSTR result) -> HRESULT
{
if (error != S_OK) {
ShowFailure(error, L"ExecuteScript failed");
}
MessageBox(nullptr, result, L"ExecuteScript Result", MB_OK);
return S_OK;
}).Get());
}
}
CapturePreview
Capture an image of what WebView is displaying.
public HRESULT CapturePreview(CORE_WEBVIEW2_CAPTURE_PREVIEW_IMAGE_FORMAT imageFormat,IStream * imageStream,ICoreWebView2CapturePreviewCompletedHandler * handler)
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 called.
// Show the user a file selection dialog, then save a screenshot of the WebView
// to the selected file.
void FileComponent::SaveScreenshot()
{
OPENFILENAME openFileName = {};
openFileName.lStructSize = sizeof(openFileName);
openFileName.hwndOwner = nullptr;
openFileName.hInstance = nullptr;
WCHAR fileName[MAX_PATH] = L"WebView2_Screenshot.png";
openFileName.lpstrFile = fileName;
openFileName.lpstrFilter = L"PNG File\0*.png\0";
openFileName.nMaxFile = ARRAYSIZE(fileName);
openFileName.Flags = OFN_OVERWRITEPROMPT;
if (GetSaveFileName(&openFileName))
{
wil::com_ptr<IStream> stream;
CHECK_FAILURE(SHCreateStreamOnFileEx(
fileName, STGM_READWRITE | STGM_CREATE, FILE_ATTRIBUTE_NORMAL, TRUE, nullptr,
&stream));
HWND mainWindow = m_appWindow->GetMainWindow();
CHECK_FAILURE(m_webView->CapturePreview(
CORE_WEBVIEW2_CAPTURE_PREVIEW_IMAGE_FORMAT_PNG, stream.get(),
Callback<ICoreWebView2CapturePreviewCompletedHandler>(
[mainWindow](HRESULT error_code) -> HRESULT {
CHECK_FAILURE(error_code);
MessageBox(mainWindow, L"Preview Captured", L"Preview Captured", MB_OK);
return S_OK;
})
.Get()));
}
}
Reload
Reload the current page.
public HRESULT Reload()
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/forward history will not be modified.
PostWebMessageAsJson
Post the specified webMessage to the top level document in this WebView.
public HRESULT PostWebMessageAsJson(LPCWSTR webMessageAsJson)
The top level document's window.chrome.webview's message event fires. JavaScript in that document may subscribe and unsubscribe to the event via the following:
window.chrome.webview.addEventListener('message', handler)
window.chrome.webview.removeEventListener('message', handler)
The event args is an instance of MessageEvent
. The ICoreWebView2Settings::IsWebMessageEnabled setting must be true or this method will fail with E_INVALIDARG. The event arg's data property is the webMessage string parameter parsed as a JSON string into a JavaScript object. The event arg's source property is a reference to the window.chrome.webview
object. See SetWebMessageReceivedEventHandler for information on sending messages from the HTML document in the webview to the host. This message is sent asynchronously. If a navigation occurs before the message is posted to the page, then the message will not be sent.
// Setup the web message received event handler before navigating to
// ensure we don't miss any messages.
CHECK_FAILURE(m_webView->add_WebMessageReceived(
Microsoft::WRL::Callback<ICoreWebView2WebMessageReceivedEventHandler>(
[this](ICoreWebView2* sender, ICoreWebView2WebMessageReceivedEventArgs* args)
{
wil::unique_cotaskmem_string uri;
CHECK_FAILURE(args->get_Source(&uri));
// Always validate that the origin of the message is what you expect.
if (uri.get() != m_sampleUri)
{
return S_OK;
}
wil::unique_cotaskmem_string messageRaw;
CHECK_FAILURE(args->TryGetWebMessageAsString(&messageRaw));
std::wstring message = messageRaw.get();
if (message.compare(0, 13, L"SetTitleText ") == 0)
{
m_appWindow->SetTitleText(message.substr(13).c_str());
}
else if (message.compare(L"GetWindowBounds") == 0)
{
RECT bounds = m_appWindow->GetWindowBounds();
std::wstring reply =
L"{\"WindowBounds\":\"Left:" + std::to_wstring(bounds.left)
+ L"\\nTop:" + std::to_wstring(bounds.top)
+ L"\\nRight:" + std::to_wstring(bounds.right)
+ L"\\nBottom:" + std::to_wstring(bounds.bottom)
+ L"\"}";
CHECK_FAILURE(sender->PostWebMessageAsJson(reply.c_str()));
}
return S_OK;
}).Get(), &m_webMessageReceivedToken));
PostWebMessageAsString
This is a helper for posting a message that is a simple string rather than a JSON string representation of a JavaScript object.
public HRESULT PostWebMessageAsString(LPCWSTR webMessageAsString)
This behaves in exactly the same manner as PostWebMessageAsJson but the window.chrome.webview
message event arg's data property will be a string with the same value as webMessageAsString. Use this instead of PostWebMessageAsJson if you want to communicate via simple strings rather than JSON objects.
add_WebMessageReceived
This event fires when the IsWebMessageEnabled setting is set and the top level document of the webview calls window.chrome.webview.postMessage
.
public HRESULT add_WebMessageReceived(ICoreWebView2WebMessageReceivedEventHandler * handler,EventRegistrationToken * token)
The postMessage function is void postMessage(object)
where object is any object supported by JSON conversion.
window.chrome.webview.addEventListener('message', arg => {
if ("SetColor" in arg.data) {
document.getElementById("colorable").style.color = arg.data.SetColor;
}
if ("WindowBounds" in arg.data) {
document.getElementById("window-bounds").value = arg.data.WindowBounds;
}
});
function SetTitleText() {
let titleText = document.getElementById("title-text");
window.chrome.webview.postMessage(`SetTitleText ${titleText.value}`);
}
function GetWindowBounds() {
window.chrome.webview.postMessage("GetWindowBounds");
}
When postMessage is called, the ICoreWebView2WebMessageReceivedEventHandler set via this SetWebMessageReceivedEventHandler method will be invoked with the postMessage's object parameter converted to a JSON string.
// Setup the web message received event handler before navigating to
// ensure we don't miss any messages.
CHECK_FAILURE(m_webView->add_WebMessageReceived(
Microsoft::WRL::Callback<ICoreWebView2WebMessageReceivedEventHandler>(
[this](ICoreWebView2* sender, ICoreWebView2WebMessageReceivedEventArgs* args)
{
wil::unique_cotaskmem_string uri;
CHECK_FAILURE(args->get_Source(&uri));
// Always validate that the origin of the message is what you expect.
if (uri.get() != m_sampleUri)
{
return S_OK;
}
wil::unique_cotaskmem_string messageRaw;
CHECK_FAILURE(args->TryGetWebMessageAsString(&messageRaw));
std::wstring message = messageRaw.get();
if (message.compare(0, 13, L"SetTitleText ") == 0)
{
m_appWindow->SetTitleText(message.substr(13).c_str());
}
else if (message.compare(L"GetWindowBounds") == 0)
{
RECT bounds = m_appWindow->GetWindowBounds();
std::wstring reply =
L"{\"WindowBounds\":\"Left:" + std::to_wstring(bounds.left)
+ L"\\nTop:" + std::to_wstring(bounds.top)
+ L"\\nRight:" + std::to_wstring(bounds.right)
+ L"\\nBottom:" + std::to_wstring(bounds.bottom)
+ L"\"}";
CHECK_FAILURE(sender->PostWebMessageAsJson(reply.c_str()));
}
return S_OK;
}).Get(), &m_webMessageReceivedToken));
remove_WebMessageReceived
Remove an event handler previously added with add_WebMessageReceived.
public HRESULT remove_WebMessageReceived(EventRegistrationToken token)
CallDevToolsProtocolMethod
Call an asynchronous DevToolsProtocol method.
public HRESULT CallDevToolsProtocolMethod(LPCWSTR methodName,LPCWSTR parametersAsJson,ICoreWebView2CallDevToolsProtocolMethodCompletedHandler * handler)
See the DevTools Protocol Viewer for a list and description of available methods. The methodName parameter is the full name of the method in the format {domain}.{method}
. The parametersAsJson parameter is a JSON formatted string containing the parameters for the corresponding method. The handler's Invoke method will be called when the method asynchronously completes. Invoke will be called with the method's return object as a JSON string.
// Prompt the user for the name and parameters of a CDP method, then call it.
void ScriptComponent::CallCdpMethod()
{
TextInputDialog dialog(
m_appWindow->GetMainWindow(),
L"Call CDP Method",
L"CDP method name:",
L"Enter the CDP method name to call, followed by a space,\r\n"
L"followed by the parameters in JSON format.",
L"Runtime.evaluate {\"expression\":\"alert(\\\"test\\\")\"}");
if (dialog.confirmed)
{
size_t delimiterPos = dialog.input.find(L' ');
std::wstring methodName = dialog.input.substr(0, delimiterPos);
std::wstring methodParams =
(delimiterPos < dialog.input.size()
? dialog.input.substr(delimiterPos + 1)
: L"{}");
m_webView->CallDevToolsProtocolMethod(
methodName.c_str(),
methodParams.c_str(),
Callback<ICoreWebView2CallDevToolsProtocolMethodCompletedHandler>(
[](HRESULT error, PCWSTR resultJson) -> HRESULT
{
MessageBox(nullptr, resultJson, L"CDP Method Result", MB_OK);
return S_OK;
}).Get());
}
}
get_BrowserProcessId
The process id of the browser process that hosts the WebView.
public HRESULT get_BrowserProcessId(UINT32 * value)
get_CanGoBack
Returns true if the webview can navigate to a previous page in the navigation history.
public HRESULT get_CanGoBack(BOOL * canGoBack)
The HistoryChanged event will fire if get_CanGoBack changes value.
get_CanGoForward
Returns true if the webview can navigate to a next page in the navigation history.
public HRESULT get_CanGoForward(BOOL * canGoForward)
The HistoryChanged event will fire if get_CanGoForward changes value.
GoBack
Navigates the WebView to the previous page in the navigation history.
public HRESULT GoBack()
GoForward
Navigates the WebView to the next page in the navigation history.
public HRESULT GoForward()
GetDevToolsProtocolEventReceiver
Get a DevTools Protocol event receiver that allows you to subscribe to a DevTools Protocol event.
public HRESULT GetDevToolsProtocolEventReceiver(LPCWSTR eventName,ICoreWebView2DevToolsProtocolEventReceiver ** receiver)
The eventName parameter is the full name of the event in the format {domain}.{event}
. See the DevTools Protocol Viewer for a list of DevTools Protocol events description, and event args.
// Prompt the user to name a CDP event, and then subscribe to that event.
void ScriptComponent::SubscribeToCdpEvent()
{
TextInputDialog dialog(
m_appWindow->GetMainWindow(),
L"Subscribe to CDP Event",
L"CDP event name:",
L"Enter the name of the CDP event to subscribe to.\r\n"
L"You may also have to call the \"enable\" method of the\r\n"
L"event's domain to receive events (for example \"Log.enable\").\r\n",
L"Log.entryAdded");
if (dialog.confirmed)
{
std::wstring eventName = dialog.input;
wil::com_ptr<ICoreWebView2DevToolsProtocolEventReceiver> receiver;
CHECK_FAILURE(
m_webView->GetDevToolsProtocolEventReceiver(eventName.c_str(), &receiver));
// If we are already subscribed to this event, unsubscribe first.
auto preexistingToken = m_devToolsProtocolEventReceivedTokenMap.find(eventName);
if (preexistingToken != m_devToolsProtocolEventReceivedTokenMap.end())
{
CHECK_FAILURE(receiver->remove_DevToolsProtocolEventReceived(
preexistingToken->second));
}
CHECK_FAILURE(receiver->add_DevToolsProtocolEventReceived(
Callback<ICoreWebView2DevToolsProtocolEventReceivedEventHandler>(
[eventName](
ICoreWebView2* sender,
ICoreWebView2DevToolsProtocolEventReceivedEventArgs* args) -> HRESULT {
wil::unique_cotaskmem_string parameterObjectAsJson;
CHECK_FAILURE(args->get_ParameterObjectAsJson(¶meterObjectAsJson));
MessageBox(
nullptr, parameterObjectAsJson.get(),
(L"CDP Event Fired: " + eventName).c_str(), MB_OK);
return S_OK;
})
.Get(),
&m_devToolsProtocolEventReceivedTokenMap[eventName]));
}
}
Stop
Stop all navigations and pending resource fetches.
public HRESULT Stop()
Does not stop scripts.
add_NewWindowRequested
Add an event handler for the NewWindowRequested event.
public HRESULT add_NewWindowRequested(ICoreWebView2NewWindowRequestedEventHandler * eventHandler,EventRegistrationToken * token)
Fires when content inside the WebView requested to open a new window, such as through window.open. The app can pass a target webview that will be considered the opened window.
// Register a handler for the NewWindowRequested event.
// This handler will defer the event, create a new app window, and then once the
// new window is ready, it'll provide that new window's WebView as the response to
// the request.
CHECK_FAILURE(m_webView->add_NewWindowRequested(
Callback<ICoreWebView2NewWindowRequestedEventHandler>(
[this](
ICoreWebView2* sender,
ICoreWebView2NewWindowRequestedEventArgs* args) {
wil::com_ptr<ICoreWebView2Deferral> deferral;
CHECK_FAILURE(args->GetDeferral(&deferral));
auto newAppWindow = new AppWindow(L"");
newAppWindow->m_isPopupWindow = true;
newAppWindow->m_onWebViewFirstInitialized = [args, deferral, newAppWindow]() {
CHECK_FAILURE(args->put_NewWindow(newAppWindow->m_webView.get()));
CHECK_FAILURE(args->put_Handled(TRUE));
CHECK_FAILURE(deferral->Complete());
};
return S_OK;
})
.Get(),
nullptr));
remove_NewWindowRequested
Remove an event handler previously added with add_NewWindowRequested.
public HRESULT remove_NewWindowRequested(EventRegistrationToken token)
add_DocumentTitleChanged
Add an event handler for the DocumentTitleChanged event.
public HRESULT add_DocumentTitleChanged(ICoreWebView2DocumentTitleChangedEventHandler * eventHandler,EventRegistrationToken * token)
The event fires when the DocumentTitle property of the WebView changes and may fire before or after the NavigationCompleted event.
// Register a handler for the DocumentTitleChanged event.
// This handler just announces the new title on the window's title bar.
CHECK_FAILURE(m_webView->add_DocumentTitleChanged(
Callback<ICoreWebView2DocumentTitleChangedEventHandler>(
[this](ICoreWebView2* sender, IUnknown* args) -> HRESULT {
wil::unique_cotaskmem_string title;
CHECK_FAILURE(sender->get_DocumentTitle(&title));
SetWindowText(m_appWindow->GetMainWindow(), title.get());
return S_OK;
})
.Get(),
&m_documentTitleChangedToken));
remove_DocumentTitleChanged
Remove an event handler previously added with add_DocumentTitleChanged.
public HRESULT remove_DocumentTitleChanged(EventRegistrationToken token)
get_DocumentTitle
The title for the current top level document.
public HRESULT get_DocumentTitle(LPWSTR * title)
If the document has no explicit title or is otherwise empty, a default that may or may not match the URI of the document will be used.
AddRemoteObject
Add the provided host object to script running in the WebView with the specified name.
public HRESULT AddRemoteObject(LPCWSTR name,VARIANT * object)
Host objects are exposed as remote object proxies via window.chrome.webview.remoteObjects.<name>
. Remote object proxies are promises and will resolve 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 will resolve to the value returned from the host for the property or method, or rejected in case of error such as there is no such property or method on the object or parameters are invalid. For example, when the application code does the following:
VARIANT object;
object.vt = VT_DISPATCH;
object.pdispVal = appObject;
webview->AddRemoteObject(L"host_object", &host);
JavaScript code in the WebView will be able to access appObject as following and then access attributes and methods of appObject:
let app_object = await window.chrome.webview.remoteObjects.host_object;
let attr1 = await app_object.attr1;
let result = await app_object.method1(parameters);
Note that while simple types, IDispatch and array are supported, generic IUnknown, VT_DECIMAL, or VT_RECORD variant is not supported. Remote JavaScript objects like callback functions are represented as an VT_DISPATCH VARIANT with the object implementing IDispatch. The JavaScript callback method may be invoked using DISPID_VALUE for the DISPID. Nested arrays are supported up to a depth of 3. Arrays of by reference types are not supported. VT_EMPTY and VT_NULL are mapped into JavaScript as null. In JavaScript null and undefined are mapped to VT_EMPTY.
Additionally, all remote objects are exposed as window.chrome.webview.remoteObjects.sync.<name>
. Here the host objects are exposed as synchronous remote object proxies. These are not promises and calls to functions or property access synchronously block running script waiting to communicate cross process for the host code to run. Accordingly this can result in reliability issues and it is recommended that you use the promise based asynchronous window.chrome.webview.remoteObjects.<name>
API described above.
Synchronous remote object proxies and asynchronous remote object proxies can both proxy the same remote object. Remote changes made by one proxy will be reflected in any other proxy of that same remote object whether the other proxies and synchronous or asynchronous.
While JavaScript is blocked on a synchronous call to native code, that native code is unable to call back to JavaScript. Attempts to do so will fail with HRESULT_FROM_WIN32(ERROR_POSSIBLE_DEADLOCK).
Remote object proxies are JavaScript Proxy objects that intercept all property get, property set, and method invocations. Properties or methods that are a part of the Function or Object prototype are run locally. Additionally any property or method in the array chrome.webview.remoteObjects.options.forceLocalProperties
will also be run locally. This defaults to including optional methods that have meaning in JavaScript like toJSON
and Symbol.toPrimitive
. You can add more to this array as required.
There's a method chrome.webview.remoteObjects.cleanupSome
that will best effort garbage collect remote object proxies.
Remote object proxies additionally have the following methods which run locally:
applyRemote, getRemote, setRemote: Perform a method invocation, property get, or property set on the remote object. You can use these to explicitly force a method or property to run remotely if there is a conflicting local method or property. For instance,
proxy.toString()
will run the local toString method on the proxy object. Butproxy.applyRemote('toString')
runstoString
on the remote proxied object instead.getLocal, setLocal: Perform property get, or property set locally. You can use these methods to force getting or setting a property on the remote object proxy itself rather than on the remote object it represents. For instance,
proxy.unknownProperty
will get the property namedunknownProperty
from the remote proxied object. Butproxy.getLocal('unknownProperty')
will get the value of the propertyunknownProperty
on the proxy object itself.sync: Asynchronous remote object proxies expose a sync method which returns a promise for a synchronous remote object proxy for the same remote object. For example,
chrome.webview.remoteObjects.sample.methodCall()
returns an asynchronous remote object proxy. You can use thesync
method to obtain a synchronous remote object proxy instead:const syncProxy = await chrome.webview.remoteObjects.sample.methodCall().sync()
async: Synchronous remote object proxies expose an async method which blocks and returns an asynchronous remote object proxy for the same remote object. For example,
chrome.webview.remoteObjects.sync.sample.methodCall()
returns a synchronous remote object proxy. Calling theasync
method on this blocks and then returns an asynchronous remote object proxy for the same remote object:const asyncProxy = chrome.webview.remoteObjects.sync.sample.methodCall().async()
then: Asynchronous remote object proxies have a then method. This allows them to be awaitable.
then
will return a promise that resolves with a representation of the remote object. If the proxy represents a JavaScript literal then a copy of that is returned locally. If the proxy represents a function then a non-awaitable proxy is returned. If the proxy represents a JavaScript object with a mix of literal properties and function properties, then the a copy of the object is returned with some properties as remote object proxies.
All other property and method invocations (other than the above Remote object proxy methods, forceLocalProperties list, and properties on Function and Object prototypes) are run remotely. Asynchronous remote object proxies return a promise representing asynchronous completion of remotely invoking the method, or getting the property. The promise resolves after the remote operations complete and the promises resolve to the resulting value of the operation. Synchronous remote object proxies work similarly but block JavaScript execution and wait for the remote operation to complete.
Setting a property on an asynchronous remote object proxy works slightly differently. The set returns immediately and the return value is the value that will be set. This is a requirement of the JavaScript Proxy object. If you need to asynchronously wait for the property set to complete, use the setRemote method which returns a promise as described above. Synchronous object property set property synchronously blocks until the property is set.
For example, suppose you have a COM object with the following interface
[uuid(3a14c9c0-bc3e-453f-a314-4ce4a0ec81d8), object, local]
interface IRemoteObjectSample : IUnknown
{
// Demonstrate basic method call with some parameters and a return value.
HRESULT MethodWithParametersAndReturnValue([in] BSTR stringParameter, [in] INT integerParameter, [out, retval] BSTR* stringResult);
// Demonstrate getting and setting a property.
[propget] HRESULT Property([out, retval] BSTR* stringResult);
[propput] HRESULT Property([in] BSTR stringValue);
// Demonstrate native calling back into JavaScript.
HRESULT CallCallbackAsynchronously([in] IDispatch* callbackParameter);
};
We can add an instance of this interface into our JavaScript with AddRemoteObject
. In this case we name it sample
:
VARIANT remoteObjectAsVariant = {};
m_remoteObject.query_to<IDispatch>(&remoteObjectAsVariant.pdispVal);
remoteObjectAsVariant.vt = VT_DISPATCH;
// We can call AddRemoteObject multiple times in a row without
// calling RemoveRemoteObject first. This will replace the previous object
// with the new object. In our case this is the same object and everything
// is fine.
CHECK_FAILURE(m_webView->AddRemoteObject(L"sample", &remoteObjectAsVariant));
remoteObjectAsVariant.pdispVal->Release();
Then in the HTML document we can use this COM object via chrome.webview.remoteObjects.sample
:
document.getElementById("getPropertyAsyncButton").addEventListener("click", async () => {
const propertyValue = await chrome.webview.remoteObjects.sample.property;
document.getElementById("getPropertyAsyncOutput").textContent = propertyValue;
});
document.getElementById("getPropertySyncButton").addEventListener("click", () => {
const propertyValue = chrome.webview.remoteObjects.sync.sample.property;
document.getElementById("getPropertySyncOutput").textContent = propertyValue;
});
document.getElementById("setPropertyAsyncButton").addEventListener("click", async () => {
const propertyValue = document.getElementById("setPropertyAsyncInput").value;
// The following line will work but it will return immediately before the property value has actually been set.
// If you need to set the property and wait for the property to change value, use the setRemote function.
chrome.webview.remoteObjects.sample.property = propertyValue;
document.getElementById("setPropertyAsyncOutput").textContent = "Set";
});
document.getElementById("setPropertyExplicitAsyncButton").addEventListener("click", async () => {
const propertyValue = document.getElementById("setPropertyExplicitAsyncInput").value;
// If you care about waiting until the property has actually changed value use the setRemote function.
await chrome.webview.remoteObjects.sample.setRemote("property", propertyValue);
document.getElementById("setPropertyExplicitAsyncOutput").textContent = "Set";
});
document.getElementById("setPropertySyncButton").addEventListener("click", () => {
const propertyValue = document.getElementById("setPropertySyncInput").value;
chrome.webview.remoteObjects.sync.sample.property = propertyValue;
document.getElementById("setPropertySyncOutput").textContent = "Set";
});
document.getElementById("invokeMethodAsyncButton").addEventListener("click", async () => {
const paramValue1 = document.getElementById("invokeMethodAsyncParam1").value;
const paramValue2 = parseInt(document.getElementById("invokeMethodAsyncParam2").value);
const resultValue = await chrome.webview.remoteObjects.sample.MethodWithParametersAndReturnValue(paramValue1, paramValue2);
document.getElementById("invokeMethodAsyncOutput").textContent = resultValue;
});
document.getElementById("invokeMethodSyncButton").addEventListener("click", () => {
const paramValue1 = document.getElementById("invokeMethodSyncParam1").value;
const paramValue2 = parseInt(document.getElementById("invokeMethodSyncParam2").value);
const resultValue = chrome.webview.remoteObjects.sync.sample.MethodWithParametersAndReturnValue(paramValue1, paramValue2);
document.getElementById("invokeMethodSyncOutput").textContent = resultValue;
});
let callbackCount = 0;
document.getElementById("invokeCallbackButton").addEventListener("click", async () => {
chrome.webview.remoteObjects.sample.CallCallbackAsynchronously(() => {
document.getElementById("invokeCallbackOutput").textContent = "Native object called the callback " + (++callbackCount) + " time(s).";
});
});
RemoveRemoteObject
Remove the host object specified by the name so that it is no longer accessible from JavaScript code in the WebView.
public HRESULT RemoveRemoteObject(LPCWSTR name)
While new access attempts will be denied, if the object is already obtained by JavaScript code in the WebView, the JavaScript code will continue to have access to that object. Calling this method for a name that is already removed or never added will fail.
OpenDevToolsWindow
Opens the DevTools window for the current document in the WebView.
public HRESULT OpenDevToolsWindow()
Does nothing if called when the DevTools window is already open
add_ContainsFullScreenElementChanged
Notifies when the ContainsFullScreenElement property changes.
public HRESULT add_ContainsFullScreenElementChanged(ICoreWebView2ContainsFullScreenElementChangedEventHandler * eventHandler,EventRegistrationToken * token)
This means that an HTML element inside the WebView is entering fullscreen to the size of the WebView or leaving fullscreen. This event is useful when, for example, a video element requests to go fullscreen. The listener of ContainsFullScreenElementChanged can then resize the WebView in response.
// Register a handler for the ContainsFullScreenChanged event.
CHECK_FAILURE(m_webView->add_ContainsFullScreenElementChanged(
Callback<ICoreWebView2ContainsFullScreenElementChangedEventHandler>(
[this](ICoreWebView2* sender, IUnknown* args) -> HRESULT {
if (m_fullScreenAllowed)
{
CHECK_FAILURE(
sender->get_ContainsFullScreenElement(&m_containsFullscreenElement));
if (m_containsFullscreenElement)
{
EnterFullScreen();
}
else
{
ExitFullScreen();
}
}
return S_OK;
})
.Get(),
nullptr));
remove_ContainsFullScreenElementChanged
Remove an event handler previously added with the corresponding add_ event method.
public HRESULT remove_ContainsFullScreenElementChanged(EventRegistrationToken token)
get_ContainsFullScreenElement
Indicates if the WebView contains a fullscreen HTML element.
public HRESULT get_ContainsFullScreenElement(BOOL * containsFullScreenElement)
add_WebResourceRequested
Add an event handler for the WebResourceRequested event.
public HRESULT add_WebResourceRequested(ICoreWebView2WebResourceRequestedEventHandler * eventHandler,EventRegistrationToken * token)
Fires when the WebView is performing an HTTP 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 fire.
if (m_blockImages)
{
m_webView->AddWebResourceRequestedFilter(L"*", CORE_WEBVIEW2_WEB_RESOURCE_CONTEXT_IMAGE);
CHECK_FAILURE(m_webView->add_WebResourceRequested(
Callback<ICoreWebView2WebResourceRequestedEventHandler>(
[this](
ICoreWebView2* sender,
ICoreWebView2WebResourceRequestedEventArgs* args) {
CORE_WEBVIEW2_WEB_RESOURCE_CONTEXT resourceContext;
CHECK_FAILURE(
args->get_ResourceContext(&resourceContext));
// Ensure that the type is image
if (resourceContext != CORE_WEBVIEW2_WEB_RESOURCE_CONTEXT_IMAGE)
{
return E_INVALIDARG;
}
// Override the response with an empty one to block the image.
// If put_Response is not called, the request will continue as normal.
wil::com_ptr<ICoreWebView2WebResourceResponse> response;
CHECK_FAILURE(m_webViewEnvironment->CreateWebResourceResponse(
nullptr, 403 /*NoContent*/, L"Blocked", L"", &response));
CHECK_FAILURE(args->put_Response(response.get()));
return S_OK;
})
.Get(),
&m_webResourceRequestedTokenForImageBlocking));
}
else
{
CHECK_FAILURE(m_webView->remove_WebResourceRequested(
m_webResourceRequestedTokenForImageBlocking));
}
remove_WebResourceRequested
Remove an event handler previously added with add_WebResourceRequested.
public HRESULT remove_WebResourceRequested(EventRegistrationToken token)
AddWebResourceRequestedFilter
Adds a URI and resource context filter to the WebResourceRequested event.
public HRESULT AddWebResourceRequestedFilter(LPCWSTR const uri,CORE_WEBVIEW2_WEB_RESOURCE_CONTEXT const resourceContext)
URI parameter can be a wildcard string ('': zero or more, '?': exactly one). nullptr is equivalent to L"". See CORE_WEBVIEW2_WEB_RESOURCE_CONTEXT enum for description of resource context filters.
RemoveWebResourceRequestedFilter
Removes a matching WebResource filter that was previously added for the WebResourceRequested event.
public HRESULT RemoveWebResourceRequestedFilter(LPCWSTR const uri,CORE_WEBVIEW2_WEB_RESOURCE_CONTEXT const resourceContext)
If the same filter was added multiple times, then it will need to 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.
add_WindowCloseRequested
Add an event handler for the WindowCloseRequested event.
public HRESULT add_WindowCloseRequested(ICoreWebView2WindowCloseRequestedEventHandler * eventHandler,EventRegistrationToken * token)
Fires when content inside the WebView requested to close the window, such as after window.close is called. The app should close the WebView and related app window if that makes sense to the app.
// Register a handler for the WindowCloseRequested event.
// This handler will close the app window if it is not the main window.
CHECK_FAILURE(m_webView->add_WindowCloseRequested(
Callback<ICoreWebView2WindowCloseRequestedEventHandler>(
[this](ICoreWebView2* sender, IUnknown* args) {
if (m_isPopupWindow)
{
CloseAppWindow();
}
return S_OK;
})
.Get(),
nullptr));
remove_WindowCloseRequested
Remove an event handler previously added with add_WindowCloseRequested.
public HRESULT remove_WindowCloseRequested(EventRegistrationToken token)
CORE_WEBVIEW2_CAPTURE_PREVIEW_IMAGE_FORMAT
Image format used by the ICoreWebView2::CapturePreview method.
Values | Descriptions |
---|---|
CORE_WEBVIEW2_CAPTURE_PREVIEW_IMAGE_FORMAT_PNG | PNG image format. |
CORE_WEBVIEW2_CAPTURE_PREVIEW_IMAGE_FORMAT_JPEG | JPEG image format. |
CORE_WEBVIEW2_SCRIPT_DIALOG_KIND
Kind of JavaScript dialog used in the ICoreWebView2ScriptDialogOpeningEventHandler interface.
Values | Descriptions |
---|---|
CORE_WEBVIEW2_SCRIPT_DIALOG_KIND_ALERT | A dialog invoked via the window.alert JavaScript function. |
CORE_WEBVIEW2_SCRIPT_DIALOG_KIND_CONFIRM | A dialog invoked via the window.confirm JavaScript function. |
CORE_WEBVIEW2_SCRIPT_DIALOG_KIND_PROMPT | A dialog invoked via the window.prompt JavaScript function. |
CORE_WEBVIEW2_SCRIPT_DIALOG_KIND_BEFOREUNLOAD | A dialog invoked via the beforeunload JavaScript event. |
CORE_WEBVIEW2_PROCESS_FAILED_KIND
Kind of process failure used in the ICoreWebView2ProcessFailedEventHandler interface.
Values | Descriptions |
---|---|
CORE_WEBVIEW2_PROCESS_FAILED_KIND_BROWSER_PROCESS_EXITED | Indicates the browser process terminated unexpectedly. |
CORE_WEBVIEW2_PROCESS_FAILED_KIND_RENDER_PROCESS_EXITED | Indicates the render process terminated unexpectedly. |
CORE_WEBVIEW2_PROCESS_FAILED_KIND_RENDER_PROCESS_UNRESPONSIVE | Indicates the render process becomes unresponsive. |
CORE_WEBVIEW2_PERMISSION_KIND
The type of a permission request.
Values | Descriptions |
---|---|
CORE_WEBVIEW2_PERMISSION_KIND_UNKNOWN_PERMISSION | Unknown permission. |
CORE_WEBVIEW2_PERMISSION_KIND_MICROPHONE | Permission to capture audio. |
CORE_WEBVIEW2_PERMISSION_KIND_CAMERA | Permission to capture video. |
CORE_WEBVIEW2_PERMISSION_KIND_GEOLOCATION | Permission to access geolocation. |
CORE_WEBVIEW2_PERMISSION_KIND_NOTIFICATIONS | Permission to send web notifications. |
CORE_WEBVIEW2_PERMISSION_KIND_OTHER_SENSORS | Permission to access generic sensor. |
CORE_WEBVIEW2_PERMISSION_KIND_CLIPBOARD_READ | Permission to read system clipboard without a user gesture. |
CORE_WEBVIEW2_PERMISSION_STATE
Response to a permission request.
Values | Descriptions |
---|---|
CORE_WEBVIEW2_PERMISSION_STATE_DEFAULT | Use default browser behavior, which normally prompt users for decision. |
CORE_WEBVIEW2_PERMISSION_STATE_ALLOW | Grant the permission request. |
CORE_WEBVIEW2_PERMISSION_STATE_DENY | Deny the permission request. |
CORE_WEBVIEW2_WEB_ERROR_STATUS
Error status values for web navigations.
Values | Descriptions |
---|---|
CORE_WEBVIEW2_WEB_ERROR_STATUS_UNKNOWN | An unknown error occurred. |
CORE_WEBVIEW2_WEB_ERROR_STATUS_CERTIFICATE_COMMON_NAME_IS_INCORRECT | The SSL certificate common name does not match the web address. |
CORE_WEBVIEW2_WEB_ERROR_STATUS_CERTIFICATE_EXPIRED | The SSL certificate has expired. |
CORE_WEBVIEW2_WEB_ERROR_STATUS_CLIENT_CERTIFICATE_CONTAINS_ERRORS | The SSL client certificate contains errors. |
CORE_WEBVIEW2_WEB_ERROR_STATUS_CERTIFICATE_REVOKED | The SSL certificate has been revoked. |
CORE_WEBVIEW2_WEB_ERROR_STATUS_CERTIFICATE_IS_INVALID | The SSL certificate is invalid – this could mean the certificate did not match the public key pins for the host name, the certificate is signed by an untrusted authority or using a weak sign algorithm, the certificate claimed DNS names violate name constraints, the certificate contains a weak key, the certificate's validity period is too long, lack of revocation information or revocation mechanism, non-unique host name, lack of certificate transparency information, or the certificate is chained to a legacy Symantec root. |
CORE_WEBVIEW2_WEB_ERROR_STATUS_SERVER_UNREACHABLE | The host is unreachable. |
CORE_WEBVIEW2_WEB_ERROR_STATUS_TIMEOUT | The connection has timed out. |
CORE_WEBVIEW2_WEB_ERROR_STATUS_ERROR_HTTP_INVALID_SERVER_RESPONSE | The server returned an invalid or unrecognized response. |
CORE_WEBVIEW2_WEB_ERROR_STATUS_CONNECTION_ABORTED | The connection was aborted. |
CORE_WEBVIEW2_WEB_ERROR_STATUS_CONNECTION_RESET | The connection was reset. |
CORE_WEBVIEW2_WEB_ERROR_STATUS_DISCONNECTED | The Internet connection has been lost. |
CORE_WEBVIEW2_WEB_ERROR_STATUS_CANNOT_CONNECT | Cannot connect to destination. |
CORE_WEBVIEW2_WEB_ERROR_STATUS_HOST_NAME_NOT_RESOLVED | Could not resolve provided host name. |
CORE_WEBVIEW2_WEB_ERROR_STATUS_OPERATION_CANCELED | The operation was canceled. |
CORE_WEBVIEW2_WEB_ERROR_STATUS_REDIRECT_FAILED | The request redirect failed. |
CORE_WEBVIEW2_WEB_ERROR_STATUS_UNEXPECTED_ERROR | An unexpected error occurred. |
CORE_WEBVIEW2_WEB_RESOURCE_CONTEXT
Enum for web resource request contexts.
Values | Descriptions |
---|---|
CORE_WEBVIEW2_WEB_RESOURCE_CONTEXT_ALL | All resources. |
CORE_WEBVIEW2_WEB_RESOURCE_CONTEXT_DOCUMENT | Document resources. |
CORE_WEBVIEW2_WEB_RESOURCE_CONTEXT_STYLESHEET | CSS resources. |
CORE_WEBVIEW2_WEB_RESOURCE_CONTEXT_IMAGE | Image resources. |
CORE_WEBVIEW2_WEB_RESOURCE_CONTEXT_MEDIA | Other media resources such as videos. |
CORE_WEBVIEW2_WEB_RESOURCE_CONTEXT_FONT | Font resources. |
CORE_WEBVIEW2_WEB_RESOURCE_CONTEXT_SCRIPT | Script resources. |
CORE_WEBVIEW2_WEB_RESOURCE_CONTEXT_XML_HTTP_REQUEST | XML HTTP requests. |
CORE_WEBVIEW2_WEB_RESOURCE_CONTEXT_FETCH | Fetch API communication. |
CORE_WEBVIEW2_WEB_RESOURCE_CONTEXT_TEXT_TRACK | TextTrack resources. |
CORE_WEBVIEW2_WEB_RESOURCE_CONTEXT_EVENT_SOURCE | EventSource API communication. |
CORE_WEBVIEW2_WEB_RESOURCE_CONTEXT_WEBSOCKET | WebSocket API communication. |
CORE_WEBVIEW2_WEB_RESOURCE_CONTEXT_MANIFEST | Web App Manifests. |
CORE_WEBVIEW2_WEB_RESOURCE_CONTEXT_SIGNED_EXCHANGE | Signed HTTP Exchanges. |
CORE_WEBVIEW2_WEB_RESOURCE_CONTEXT_PING | Ping requests. |
CORE_WEBVIEW2_WEB_RESOURCE_CONTEXT_CSP_VIOLATION_REPORT | CSP Violation Reports. |
CORE_WEBVIEW2_WEB_RESOURCE_CONTEXT_OTHER | Other resources. |