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 登入頁。

規範

此特性似乎未在任何規範中定義。

瀏覽器相容性

另見