WindowSharedStorage

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

WindowSharedStorage 接口是 共享存储 API 的一部分,代表了标准浏览上下文中的特定源的共享存储。

WindowSharedStorage 可通过 Window.sharedStorage 访问。

实例属性

worklet 实验性

包含代表当前源的共享存储 worklet 的 SharedStorageWorklet 实例。SharedStorageWorklet 包含 addModule() 方法,该方法用于向共享存储 worklet 添加模块。

实例方法

WindowSharedStorage 继承其父接口 SharedStorage 的属性。

run() 实验性

执行已在当前源的 SharedStorageWorklet 中添加的模块中注册的 Run 输出门 操作。

selectURL() 实验性

执行已在当前源的 SharedStorageWorklet 中添加的模块中注册的 URL 选择输出门 操作。

示例

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 登录页。

规范

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

浏览器兼容性

另见