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 |
浏览器兼容性
加载中…