WorkletSharedStorage
WorkletSharedStorage 接口是 Shared Storage API 的一部分,用于表示工作组(worklet)上下文中特定源的共享存储。
可以通过 SharedStorageWorkletGlobalScope.sharedStorage 访问 WorkletSharedStorage。
实例属性
contextExperimental Non-standard-
包含通过
FencedFrameConfig.setSharedStorageContext()方法从关联的浏览上下文传递到共享存储工作组的上下文数据。
实例方法
WorkletSharedStorage 继承了其父接口 SharedStorage 的属性。
get()Experimental-
从共享存储中检索值。
length()Experimental-
返回当前为当前源存储在共享存储中的条目数。
remainingBudget()Experimental-
返回当前源的剩余导航预算。
由于 WorkletSharedStorage 定义了 异步迭代器,因此它还包含以下方法:
示例
通过 setSharedStorageContext() 传递上下文数据
您可以使用 Private Aggregation API 来创建报告,这些报告将隐私沙盒(fenced frame)内的事件级数据与嵌入文档的上下文数据相结合。setSharedStorageContext() 可用于将来自嵌入程序的上下文数据传递给由 Protected Audience API 启动的共享存储工作组。
在此示例中,我们使用 共享存储 来存储来自嵌入页面和隐私沙盒的数据。
在嵌入页面上,我们使用 setSharedStorageContext() 将模拟事件 ID 设置为共享存储上下文。
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;
在隐私沙盒中,使用 window.sharedStorage.worklet.addModule() 添加工作组模块后,我们使用 window.sharedStorage.run() 将事件级数据发送到共享存储工作组模块(这与来自嵌入文档的上下文数据无关)。
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,
},
});
在 reporting-worklet.js 工作组中,我们从 sharedStorage.context 读取嵌入文档的事件 ID,并从数据对象读取框架的事件级数据。然后,我们通过 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);
规范
此特性似乎未在任何规范中定义。浏览器兼容性
加载中…