pub struct WebView(/* private fields */);Expand description
A WebView2 browser. Navigate to URLs and run JavaScript against the hosted page.
Implementations§
Source§impl WebView
impl WebView
Navigates the browser to the given URI.
Navigates the browser to the given HTML content as the document.
Navigates the browser using a NavigationRequest, allowing a custom
HTTP method, request headers, or body - for example a POST or an
Authorization header that a plain navigate cannot
supply.
Sourcepub fn open_dev_tools_window(&self) -> Result<()>
pub fn open_dev_tools_window(&self) -> Result<()>
Opens the DevTools window for the page, the same view shown by the browser’s “Inspect” command.
Sourcepub fn go_back(&self) -> Result<()>
pub fn go_back(&self) -> Result<()>
Navigates back to the previous page in the navigation history.
Sourcepub fn go_forward(&self) -> Result<()>
pub fn go_forward(&self) -> Result<()>
Navigates forward to the next page in the navigation history.
Sourcepub fn document_title(&self) -> String
pub fn document_title(&self) -> String
Returns the title of the current top-level document.
Sourcepub fn set_virtual_host_name_to_folder_mapping(
&self,
host_name: &str,
folder_path: &str,
access_kind: HostResourceAccessKind,
) -> Result<()>
pub fn set_virtual_host_name_to_folder_mapping( &self, host_name: &str, folder_path: &str, access_kind: HostResourceAccessKind, ) -> Result<()>
Maps a virtual host name to a local folder so the page can load its files
over a normal URL such as https://app.example/index.html. access_kind
controls cross-origin access to the mapped files.
Sourcepub fn clear_virtual_host_name_to_folder_mapping(
&self,
host_name: &str,
) -> Result<()>
pub fn clear_virtual_host_name_to_folder_mapping( &self, host_name: &str, ) -> Result<()>
Removes a mapping previously created with
set_virtual_host_name_to_folder_mapping.
Returns the CookieManager for reading, writing, and deleting the
browser’s cookies.
Sourcepub fn profile(&self) -> Result<Profile>
pub fn profile(&self) -> Result<Profile>
Returns the Profile this browser belongs to, exposing its color
scheme, download folder, and browsing-data controls.
Sourcepub fn contains_fullscreen_element(&self) -> bool
pub fn contains_fullscreen_element(&self) -> bool
Returns true if the page currently has an element displayed full screen
(for example a video using the HTML Fullscreen API).
Sourcepub fn memory_usage_target_level(&self) -> Result<MemoryUsageTargetLevel>
pub fn memory_usage_target_level(&self) -> Result<MemoryUsageTargetLevel>
Returns the memory-usage level WebView2 is targeting.
Sourcepub fn set_memory_usage_target_level(
&self,
level: MemoryUsageTargetLevel,
) -> Result<()>
pub fn set_memory_usage_target_level( &self, level: MemoryUsageTargetLevel, ) -> Result<()>
Hints the memory-usage level WebView2 should target. Set
MemoryUsageTargetLevel::Low when the WebView is hidden so the
browser can trim memory, and back to Normal when it is shown again.
Sourcepub fn settings(&self) -> Result<Settings>
pub fn settings(&self) -> Result<Settings>
Returns the Settings controlling features such as JavaScript, the dev
tools, and context menus.
Sourcepub fn execute_script<F: FnOnce(Result<String>) + 'static>(
&self,
javascript: &str,
handler: F,
) -> Result<()>
pub fn execute_script<F: FnOnce(Result<String>) + 'static>( &self, javascript: &str, handler: F, ) -> Result<()>
Asynchronously runs JavaScript in the context of the current page. The
handler closure receives the JSON-encoded result on the UI thread.
Sourcepub fn call_dev_tools_protocol_method<F: FnOnce(Result<String>) + 'static>(
&self,
method: &str,
params_json: &str,
handler: F,
) -> Result<()>
pub fn call_dev_tools_protocol_method<F: FnOnce(Result<String>) + 'static>( &self, method: &str, params_json: &str, handler: F, ) -> Result<()>
Asynchronously calls a Chrome DevTools Protocol method.
params_json is the method’s JSON object argument, or "{}" for none.
Sourcepub fn on_dev_tools_protocol_event<F>(
&self,
event_name: &str,
handler: F,
) -> Result<EventRegistration>where
F: FnMut(DevToolsProtocolEventReceivedArgs) + 'static,
pub fn on_dev_tools_protocol_event<F>(
&self,
event_name: &str,
handler: F,
) -> Result<EventRegistration>where
F: FnMut(DevToolsProtocolEventReceivedArgs) + 'static,
Subscribes to a Chrome DevTools Protocol event by name.
Most CDP events require enabling their domain before they fire.
Sourcepub fn add_script_to_execute_on_document_created(
&self,
javascript: &str,
) -> Result<ScriptId>
pub fn add_script_to_execute_on_document_created( &self, javascript: &str, ) -> Result<ScriptId>
Registers JavaScript to run before any other script in each new document.
Pumps the calling thread’s message loop until registration completes, so call it during setup before handing control to your own message loop.
Sourcepub fn remove_script_to_execute_on_document_created(
&self,
id: &ScriptId,
) -> Result<()>
pub fn remove_script_to_execute_on_document_created( &self, id: &ScriptId, ) -> Result<()>
Removes a script previously registered on document creation.
Subscribes to the navigation-starting event, raised before each
navigation. The handler may inspect the target and cancel it via
NavigationStartingArgs::set_cancel.
Subscribes to the navigation-completed event.
Sourcepub fn on_process_failed<F: FnMut(ProcessFailedArgs) + 'static>(
&self,
handler: F,
) -> Result<EventRegistration>
pub fn on_process_failed<F: FnMut(ProcessFailedArgs) + 'static>( &self, handler: F, ) -> Result<EventRegistration>
Subscribes to the process-failed event.
A renderer crash can be reloaded; a browser-process exit requires a new WebView.
Sourcepub fn on_contains_fullscreen_element_changed<F: FnMut(bool) + 'static>(
&self,
handler: F,
) -> Result<EventRegistration>
pub fn on_contains_fullscreen_element_changed<F: FnMut(bool) + 'static>( &self, handler: F, ) -> Result<EventRegistration>
Subscribes to HTML fullscreen state changes.
Sourcepub fn post_web_message_as_json(&self, json: &str) -> Result<()>
pub fn post_web_message_as_json(&self, json: &str) -> Result<()>
Posts a message to the hosted page as a JSON value. The page receives it
via the window.chrome.webview.addEventListener("message", ...) event,
with event.data set to the parsed JSON.
Sourcepub fn post_web_message_as_string(&self, message: &str) -> Result<()>
pub fn post_web_message_as_string(&self, message: &str) -> Result<()>
Posts a message to the hosted page as a string. The page receives it via
the window.chrome.webview.addEventListener("message", ...) event, with
event.data set to the string.
Sourcepub fn on_web_message_received<F: FnMut(WebMessageReceivedArgs) + 'static>(
&self,
handler: F,
) -> Result<EventRegistration>
pub fn on_web_message_received<F: FnMut(WebMessageReceivedArgs) + 'static>( &self, handler: F, ) -> Result<EventRegistration>
Subscribes to the web-message-received event, raised when the hosted
page calls window.chrome.webview.postMessage.
Sourcepub fn on_content_loading<F: FnMut(ContentLoadingArgs) + 'static>(
&self,
handler: F,
) -> Result<EventRegistration>
pub fn on_content_loading<F: FnMut(ContentLoadingArgs) + 'static>( &self, handler: F, ) -> Result<EventRegistration>
Subscribes to the content-loading event, raised when the browser starts loading content for a new document.
Sourcepub fn on_document_title_changed<F: FnMut(String) + 'static>(
&self,
handler: F,
) -> Result<EventRegistration>
pub fn on_document_title_changed<F: FnMut(String) + 'static>( &self, handler: F, ) -> Result<EventRegistration>
Subscribes to the document-title-changed event. The handler receives
the new document_title.
Sourcepub fn on_window_close_requested<F: FnMut() + 'static>(
&self,
handler: F,
) -> Result<EventRegistration>
pub fn on_window_close_requested<F: FnMut() + 'static>( &self, handler: F, ) -> Result<EventRegistration>
Subscribes to the window-close-requested event, raised when the hosted
page calls window.close(). The host typically responds by closing its
window.
Sourcepub fn on_new_window_requested<F: FnMut(NewWindowRequestedArgs) + 'static>(
&self,
handler: F,
) -> Result<EventRegistration>
pub fn on_new_window_requested<F: FnMut(NewWindowRequestedArgs) + 'static>( &self, handler: F, ) -> Result<EventRegistration>
Subscribes to the new-window-requested event, raised when the page
tries to open a new window (for example via window.open). The
handler may suppress, redirect, or
defer the request.
Sourcepub fn on_permission_requested<F: FnMut(PermissionRequestedArgs) + 'static>(
&self,
handler: F,
) -> Result<EventRegistration>
pub fn on_permission_requested<F: FnMut(PermissionRequestedArgs) + 'static>( &self, handler: F, ) -> Result<EventRegistration>
Subscribes to the permission-requested event, raised when the page
requests access to a capability such as the camera or geolocation.
The handler decides the outcome via
PermissionRequestedArgs::set_state and may
defer the decision.
Sourcepub fn on_download_starting<F: FnMut(DownloadStartingArgs) + 'static>(
&self,
handler: F,
) -> Result<EventRegistration>
pub fn on_download_starting<F: FnMut(DownloadStartingArgs) + 'static>( &self, handler: F, ) -> Result<EventRegistration>
Subscribes to the download-starting event, raised when a download begins.
The handler receives a DownloadStartingArgs to inspect or control the
DownloadOperation, change its destination, or cancel it.
Sourcepub fn on_web_resource_requested<F>(
&self,
uri_filter: &str,
handler: F,
) -> Result<EventRegistration>
pub fn on_web_resource_requested<F>( &self, uri_filter: &str, handler: F, ) -> Result<EventRegistration>
Subscribes to matching resource requests and optionally fulfills them from memory.