This interface 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.
Summary
| Members | Description |
|---|---|
| ScriptUri | A string representing the Uri of the script that the worker is executing. |
| PostWebMessageAsJson | Posts the specified webMessageAsJson to this worker. |
| PostWebMessageAsString | Posts a message that is a simple string rather than a JSON string representation of a JavaScript object. |
| Destroying | Add an event handler for the CoreWebView2ServiceWorker.Destroying event that is raised when the worker object is Destroying. A worker object is Destroying when the worker script is terminated or when the CoreWebView2ServiceWorker object is Destroying. |
| WebMessageReceived | It is raised when the CoreWebView2Settings.IsWebMessageEnabled setting is set and the service worker script runs window.chrome.webview.postMessage. |
Properties
ScriptUri
readonly string 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 Worker 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 section 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
PostWebMessageAsJson
void PostWebMessageAsJson(string webMessageAsJson)
Posts the specified webMessageAsJson to this worker.
The event args is an instance of MessageEvent. The CoreWebView2Settings.IsWebMessageEnabled setting must be true or the message will not be sent. The event arg's data property of the event arg is the webMessageAsJson string parameter parsed as a JSON string into a JavaScript object. The event arg's source property of the event arg is a reference to the self.chrome.webview object. For information about sending messages from the worker to the host, navigate to CoreWebView2ServiceWorker.WebMessageReceived. The message is sent asynchronously. If the worker is terminated or destroyed before the message is posted, the message is discarded.
Runs the message event of the self.chrome.webview of this worker. Worker Javascript may subscribe and unsubscribe to the event using the following code:
self.chrome.webview.addEventListener('message', handler)
self.chrome.webview.removeEventListener('message', handler)
PostWebMessageAsString
void PostWebMessageAsString(string 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 CoreWebView2ServiceWorker.PostWebMessageAsJson, 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 CoreWebView2ServiceWorker.PostWebMessageAsJson if you want to communicate using simple strings rather than JSON objects.
Events
Destroying
Add an event handler for the CoreWebView2ServiceWorker.Destroying event that is raised when the worker object is Destroying. A worker object is Destroying when the worker script is terminated or when the CoreWebView2ServiceWorker object is Destroying.
If the worker has already been destroyed before the event handler is registered, the handler will never be called.
Type: TypedEventHandler<CoreWebView2ServiceWorker, Object>
WebMessageReceived
It is raised when the CoreWebView2Settings.IsWebMessageEnabled setting is set and the service worker script runs window.chrome.webview.postMessage.
Type: TypedEventHandler<CoreWebView2ServiceWorker, CoreWebView2WebMessageReceivedEventArgs>