Skip to main content

WebView

Struct WebView 

Source
pub struct WebView(/* private fields */);
Expand description

A WebView2 browser. Navigate to URLs and run JavaScript against the hosted page.

Implementations§

Source§

impl WebView

Source

pub fn navigate(&self, uri: &str) -> Result<()>

Navigates the browser to the given URI.

Source

pub fn navigate_to_string(&self, html: &str) -> Result<()>

Navigates the browser to the given HTML content as the document.

Source

pub fn navigate_with_request(&self, request: &NavigationRequest) -> Result<()>

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.

Source

pub fn reload(&self) -> Result<()>

Reloads the current page.

Source

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.

Source

pub fn stop(&self) -> Result<()>

Stops any in-progress navigation or download.

Source

pub fn go_back(&self) -> Result<()>

Navigates back to the previous page in the navigation history.

Source

pub fn go_forward(&self) -> Result<()>

Navigates forward to the next page in the navigation history.

Source

pub fn source(&self) -> String

Returns the URI of the current top-level document.

Source

pub fn document_title(&self) -> String

Returns the title of the current top-level document.

Source

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.

Source

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.

Source

pub fn cookie_manager(&self) -> Result<CookieManager>

Returns the CookieManager for reading, writing, and deleting the browser’s cookies.

Source

pub fn profile(&self) -> Result<Profile>

Returns the Profile this browser belongs to, exposing its color scheme, download folder, and browsing-data controls.

Source

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).

Source

pub fn memory_usage_target_level(&self) -> Result<MemoryUsageTargetLevel>

Returns the memory-usage level WebView2 is targeting.

Source

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.

Source

pub fn settings(&self) -> Result<Settings>

Returns the Settings controlling features such as JavaScript, the dev tools, and context menus.

Source

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.

Source

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.

Source

pub fn on_dev_tools_protocol_event<F>( &self, event_name: &str, handler: F, ) -> Result<EventRegistration>

Subscribes to a Chrome DevTools Protocol event by name.

Most CDP events require enabling their domain before they fire.

Source

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.

Source

pub fn remove_script_to_execute_on_document_created( &self, id: &ScriptId, ) -> Result<()>

Removes a script previously registered on document creation.

Source

pub fn on_navigation_starting<F: FnMut(NavigationStartingArgs) + 'static>( &self, handler: F, ) -> Result<EventRegistration>

Subscribes to the navigation-starting event, raised before each navigation. The handler may inspect the target and cancel it via NavigationStartingArgs::set_cancel.

Source

pub fn on_navigation_completed<F: FnMut(NavigationCompletedArgs) + 'static>( &self, handler: F, ) -> Result<EventRegistration>

Subscribes to the navigation-completed event.

Source

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.

Source

pub fn on_contains_fullscreen_element_changed<F: FnMut(bool) + 'static>( &self, handler: F, ) -> Result<EventRegistration>

Subscribes to HTML fullscreen state changes.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Clone for WebView

Source§

fn clone(&self) -> WebView

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.