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- : A string that contains the ID of the context to which the realm belongs.
This field is included only when the
typefield value is"window".
- : A string that contains the ID of the context to which the realm belongs.
This field is included only when the
origin- : A string with the origin of the realm.
owners- : A single-element array that contains the ID of the realm that owns the worker.
This field is included only when the
typefield value is"dedicated-worker".
- : A single-element array that contains the ID of the realm that owns the worker.
This field is included only when the
realm- : A string that contains the ID of the realm.
Pass this value as the
realmfield of thetargetparameter of commands such asscript.evaluate.
- : A string that contains the ID of the realm.
Pass this value as the
sandbox- : A string that contains the name of the sandbox realm.
This field is included only for a sandbox realm, which is of type
"window".
- : A string that contains the name of the sandbox realm.
This field is included only for a sandbox realm, which is of type
type- : A string that indicates the type of realm.
It has one of the following values:
"window": A realm whose global object is a Window. This includes sandbox realms."worker": A realm whose global object is a WorkerGlobalScope, but not one of the more specific dedicated, shared, or service worker global scopes."dedicated-worker": A realm whose global object is a DedicatedWorkerGlobalScope."shared-worker": A realm whose global object is a SharedWorkerGlobalScope."service-worker": A realm whose global object is a ServiceWorkerGlobalScope."worklet": A realm whose global object is a WorkletGlobalScope, but not one of the more specific audio or paint worklet global scopes."audio-worklet": A realm whose global object is an AudioWorkletGlobalScope."paint-worklet": A realm whose global object is a PaintWorkletGlobalScope.
- : 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:
{
"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:
{
"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
Browser compatibility
See also
script.callFunctioncommandscript.evaluatecommandscript.getRealmscommandscript.realmDestroyedeventsession.subscribecommand
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
