Share via


CoreWebView2DedicatedWorker Class

Definition

public class CoreWebView2DedicatedWorker
type CoreWebView2DedicatedWorker = class
Public Class CoreWebView2DedicatedWorker
Inheritance
CoreWebView2DedicatedWorker

Properties

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](https://developer.mozilla.org/docs/Web/API/Worker/scriptURL).

        Refer to the Host Name Canonicalization for
        details on how normalization is performed.
        The same process applies to the `scriptURL` when a worker is created 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(String)

Posts the specified webMessageAsJson to this worker. The worker receives the message by subscribing to the message event of the self.chrome.webview of the worker.

        ```cpp
        self.chrome.webview.addEventListener('message', handler)
        self.chrome.webview.removeEventListener('message', handler)
        ```

        The event args is an instance of `MessageEvent`. The
        `ICoreWebView2Settings::IsWebMessageEnabled` setting must be `TRUE` or
        the web message will not be sent. The `data` property of the event arg 
        is the `webMessageAsJson` string parameter parsed as a JSON string into a 
        JS object. The `source` property of the event arg is the path
        to the worker script. The message is delivered asynchronously. 
        If the worker is terminated or destroyed before the message is posted,
        the message is discarded.
        Worker Javascript may subscribe and unsubscribe to the event 
        using the following code:
        ```javascript
        self.chrome.webview.addEventListener('message', handler)
        self.chrome.webview.removeEventListener('message', handler)
        ```
        See also the equivalent methods: `ICoreWebView2::PostWebMessageAsJson`
        and `ICoreWebView2Frame::PostWebMessageAsJson`
PostWebMessageAsString(String)

Posts a message that is a simple string rather than a JSON string representation of a JavaScript object. This behaves exactly the same manner as 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 PostWebMessageAsJson if you want to communicate using simple strings rather than JSON objects. Please see PostWebMessageAsJson for additional information. See also the equivalent methods: ICoreWebView2::PostWebMessageAsString and ICoreWebView2Frame::PostWebMessageAsString

Events

DedicatedWorkerCreated

Subscribe to this event that gets raised when a new dedicated worker is created from a dedicated worker.

        A Dedicated Worker is a type of web worker that allow you to run Javascript
        code in the background without blocking the main thread, making them useful
        for tasks like heavy computations, data processing, and parallel execution.
        It is "dedicated" because it is linked to a single parent document and cannot
        be shared with other scripts.

        This event is raised when a dedicated creates a dedicated worker using the
        `new Worker("/worker.js")` method. See the
        [Worker](https://developer.mozilla.org/docs/Web/API/Worker/Worker)
        for more information.
Destroying

Add an event handler for the 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 `CoreWebView2DedicatedWorker` object is Destroying.

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

Add an event handler for the WebMessageReceived event. WebMessageReceived is fired, when the ICoreWebView2Settings::IsWebMessageEnabled setting is set 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