大网数据广告活动
Web 标准与前端开发

`script.realmCreated` event

· 52CE 我爱测

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

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 type field value is "window".
  • 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 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
    • : 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:
      • "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.

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

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。