WindowSharedStorage: worklet 属性

实验性: 这是一项实验性技术
在生产中使用此技术之前,请仔细检查浏览器兼容性表格

WindowSharedStorage 接口的只读属性 worklet 包含一个 SharedStorageWorklet 实例,该实例代表当前源的共享存储 worklet。

SharedStorageWorklet 包含 addModule() 方法,该方法用于向共享存储 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();

有关此示例的演练以及指向其他示例的链接,请参阅 Shared Storage API 登陆页面。

规范

此特性似乎未在任何规范中定义。

浏览器兼容性

另见