script.realmCreated event

The script.realmCreated event of the script module fires when a new realm is created for a document, a worker, or a worklet.

Event data

The params field in the event notification is an object that can contain the following fields, depending on the value of the type field:

context Optional

A string that contains the ID of the context to which the realm belongs. This field is included only when the type field value is "window".

origin

A string with the origin of the realm.

owners Optional

A single-element array that contains the ID of the realm that owns the worker. This field is included only when the type field value is "dedicated-worker".

realm

A string that contains the ID of the realm. Pass this value as the realm field of the target parameter of commands such as script.evaluate.

sandbox Optional

A string that contains the name of the sandbox realm. This field is included only for a sandbox realm, which is of type "window".

type

A string that indicates the type of realm. It has one of the following values:

Description

Together with script.realmDestroyed, use this event to monitor the lifetime of JavaScript realms.

When you subscribe to this event, the browser first sends a script.realmCreated event for each realm that already exists and is ready to run scripts, and then sends further events as new realms are created. This means you don't need to call script.getRealms to discover the realms that existed before you subscribed.

A cross-document navigation creates a new realm for the document, so you receive a new event with a new realm ID. The realm ID from before the navigation is no longer valid.

Examples

Receiving an event when a document is loaded

Assume you have a WebDriver BiDi connection and an active session with a subscription to script.realmCreated.

If any realms already exist when you subscribe, you receive an event for each of them first. Then, when a tab loads a document at https://example.com, the browser sends the following notification:

json
{
  "type": "event",
  "method": "script.realmCreated",
  "params": {
    "context": "93ee5bd6-d256-4608-a002-9a8995cc0e5f",
    "origin": "https://example.com",
    "realm": "7c37f4c0-abcd-1234-ef56-789012345678",
    "type": "window"
  }
}

Receiving an event when a worker starts

Using the same connection and session as in the previous example, suppose the page starts a dedicated worker.

The browser sends the following notification, which has no context field because a worker realm doesn't belong to a context. Instead, owners contains the ID of the realm that owns the worker:

json
{
  "type": "event",
  "method": "script.realmCreated",
  "params": {
    "origin": "https://example.com",
    "owners": ["7c37f4c0-abcd-1234-ef56-789012345678"],
    "realm": "a1b2c3d4-e5f6-4708-9a1b-2c3d4e5f6071",
    "type": "dedicated-worker"
  }
}

Specifications

Specification
WebDriver BiDi
# event-script-realmCreated

Browser compatibility

See also