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 登陸頁面。

規範

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

瀏覽器相容性

另見