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

WorkletSharedStorage

· 52CE 我爱测

The **`WorkletSharedStorage`** interface of the Shared Storage API represents the shared storage for a particular origin within a worklet context.

Shared Storage API

The WorkletSharedStorage interface of the Shared Storage API represents the shared storage for a particular origin within a worklet context.

WorkletSharedStorage is accessed via SharedStorageWorkletGlobalScope.sharedStorage.

Instance properties

  • WorkletSharedStorage.context
    • : Contains contextual data passed into the shared storage worklet from the associated browsing context via the FencedFrameConfig.setSharedStorageContext() method.

Instance methods

WorkletSharedStorage inherits properties from its parent interface, SharedStorage.

  • WorkletSharedStorage.get
    • : Retrieves a value from the shared storage.
  • WorkletSharedStorage.length
    • : Returns the number of entries currently stored in the shared storage for the current origin.
  • WorkletSharedStorage.remainingBudget
    • : Returns the remaining navigation budget for the current origin.

WorkletSharedStorage also includes the following methods because it has an async iterator defined on it:

  • WorkletSharedStorage.entries
    • : Returns a new async iterator for the key-value pairs of a WorkletSharedStorage object instance's enumerable properties.
  • WorkletSharedStorage.keys
    • : Returns a new async iterator containing the keys for each item in a WorkletSharedStorage object instance.
  • WorkletSharedStorage[Symbol.asyncIterator]()
    • : Returns the WorkletSharedStorage.entries function by default.

Examples

Passing contextual data via setSharedStorageContext()

You can use the Private Aggregation API to create reports that combine event-level data inside fenced frames with contextual data from the embedding document. setSharedStorageContext() can be used to pass contextual data from the embedder to shared storage worklets initiated by the Protected Audience API.

In this example, we store data from both the embedding page and the fenced frame using shared storage.

On the embedding page, we set a mock event ID as the shared storage context using setSharedStorageContext():

const frameConfig = await navigator.runAdAuction({ resolveToConfig: true });

// Data from the embedder that you want to pass to the shared storage worklet
frameConfig.setSharedStorageContext("some-event-id");

const frame = document.createElement("fencedframe");
frame.config = frameConfig;

Within the fenced frame, after adding the worklet module with Worklet.addModule, we send the event-level data into the shared storage worklet module using WindowSharedStorage.run (this is unrelated to the contextual data from the embedding document):

const frameData = {
  // Data available only inside the fenced frame
};

await window.sharedStorage.worklet.addModule("reporting-worklet.js");

await window.sharedStorage.run("send-report", {
  data: {
    frameData,
  },
});

In the reporting-worklet.js worklet, we read the embedding document's event ID from sharedStorage.context and the frame's event-level data from the data object. We then report them through Private Aggregation:

class ReportingOperation {
  convertEventIdToBucket(eventId) {
    // …
  }
  convertEventPayloadToValue(info) {
    // …
  }

  async run(data) {
    // Data from the embedder
    const eventId = sharedStorage.context;

    // Data from the fenced frame
    const eventPayload = data.frameData;

    privateAggregation.sendHistogramReport({
      bucket: convertEventIdToBucket(eventId),
      value: convertEventPayloadToValue(eventPayload),
    });
  }
}

register("send-report", ReportingOperation);

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

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

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