SharedStorageWorklet
SharedStorageWorklet 介面是 共享儲存 API 的一部分,代表當前來源的共享儲存工作執行緒。
SharedStorageWorklet 沒有自己的屬性或方法。相反,它繼承了 Worklet 介面的 addModule() 方法。此方法用於新增模組。
與常規 Worklet 不同
- 如果呼叫方未將共享儲存 API 包含在 隱私沙盒註冊流程 中,則對
sharedStorageWorklet.addModule()的呼叫將被拒絕。 - 出於隱私原因,
SharedStorageWorklet僅允許新增一個模組。即使成功註冊,對同一共享儲存工作執行緒重複呼叫addModule()也會被拒絕。
透過 WindowSharedStorage.worklet 訪問 SharedStorageWorklet。
示例
js
// Randomly assigns a user to a group 0 or 1
function getExperimentGroup() {
return Math.round(Math.random());
}
async function injectContent() {
// Add the module to the shared storage worklet
await window.sharedStorage.worklet.addModule("ab-testing-worklet.js");
// Assign user to a random group (0 or 1) and store it in shared storage
window.sharedStorage.set("ab-testing-group", getExperimentGroup(), {
ignoreIfPresent: true,
});
// Run the URL selection operation
const fencedFrameConfig = await window.sharedStorage.selectURL(
"ab-testing",
[
{ url: `https://your-server.example/content/default-content.html` },
{ url: `https://your-server.example/content/experiment-content-a.html` },
],
{
resolveToConfig: true,
},
);
// Render the chosen URL into a fenced frame
document.getElementById("content-slot").config = fencedFrameConfig;
}
injectContent();
有關此示例的詳細介紹以及其他示例的連結,請參閱 共享儲存 API 登入頁。
規範
| 規範 |
|---|
| 共享儲存 API # sharedstorageworklet |
瀏覽器相容性
載入中…