FencedFrameConfig:setSharedStorageContext() 方法
FencedFrameConfig 介面的 setSharedStorageContext() 方法將嵌入式文件的上下文資料傳遞到 <fencedframe> 的 共享儲存。
語法
js
setSharedStorageContext(context)
引數
context-
一個代表要傳遞到共享儲存的上下文資料的字串。設定後,它將被儲存在
FencedFrameConfig例項的內部配置中。
返回值
無(Undefined)。
示例
透過 setSharedStorageContext() 傳遞上下文資料
您可以使用 Private Aggregation API 來建立報告,這些報告將隱私沙盒(fenced frame)內的事件級資料與嵌入文件的上下文資料相結合。setSharedStorageContext() 可用於將來自嵌入程式的上下文資料傳遞給由 Protected Audience API 啟動的共享儲存工作組。
在下面的示例中,我們將嵌入式頁面和圍欄框架的資料儲存在 共享儲存中。
在嵌入式頁面中,我們將使用 setSharedStorageContext() 設定一個模擬事件 ID 作為共享儲存上下文。
js
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() 新增 worklet 模組,然後使用 window.sharedStorage.run() 將事件級別的資料傳送到共享儲存 worklet(這與來自嵌入式文件的上下文資料無關)。
js
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 worklet 中,我們從 sharedStorage.context 讀取嵌入式文件的事件 ID,從資料物件中讀取框架的事件級別資料,然後透過 Private Aggregation 進行報告。
js
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);
規範
| 規範 |
|---|
| 圍欄框架 # dom-fencedframeconfig-setsharedstoragecontext |
瀏覽器相容性
載入中…
另見
- privacysandbox.google.com 上的Fenced frames
- 隱私沙盒(位於 privacysandbox.google.com)