Idioma

CoreWebView2ServiceWorker Class

Definition

Represents a service worker in WebView2 and provides methods and properties for interacting with it, such as getting the script URI, posting messages to it, etc.

public class CoreWebView2ServiceWorker
type CoreWebView2ServiceWorker = class
Public Class CoreWebView2ServiceWorker
Inheritance
CoreWebView2ServiceWorker

Properties

Name Description
ScriptUri

A string representing the Uri of the script that the worker is executing.

The scriptUri is a fully qualified URI, including the scheme, host, and path. In contrast, the scriptURL property of the ServiceWorker object in the DOM returns the relative URL of the script being executed by the worker. For more details on DOM API, see the DOM API documentation.

Refer to the Host Name Canonicalization for details on how normalization is performed. The same process applies to the scriptURL when a worker is registered from DOM API. The scriptUri property reflects this normalization, ensuring that the URL is standardized. For example, HTTPS://EXAMPLE.COM/worker.js is canonicalized to https://example.com/worker.js; https://bücher.de/worker.js is canonicalized to https://xn--bcher-kva.de/worker.js.

Methods

Name Description
PostWebMessageAsJson(String)

Posts the specified webMessageAsJson to this worker.

PostWebMessageAsString(String)

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(String), but the data property of the event arg of the self.chrome.webview message is a string with the same value as webMessageAsString. Use this instead of PostWebMessageAsJson(String) if you want to communicate using simple strings rather than JSON objects.

Events

Name Description
Destroying

Raised when the worker object is being destroyed.

A worker object is being destroyed when the worker script is terminated or when the CoreWebView2ServiceWorker object is being destroyed.

If the worker has already been destroyed before the event handler is registered, the handler will never be called.

WebMessageReceived

WebMessageReceived is fired when the IsWebMessageEnabled setting is set to true and the worker runs self.chrome.webview.postMessage. The postMessage function is void postMessage(object) where object is any object supported by JSON conversion.

If the worker calls postMessage multiple times, the corresponding WebMessageReceived events are guaranteed to be fired in the same order.

Applies to