WindowSharedStorage

Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

Giao diện WindowSharedStorage của Shared Storage API đại diện cho bộ nhớ dùng chung cho một nguồn gốc cụ thể trong ngữ cảnh duyệt web tiêu chuẩn.

WindowSharedStorage được truy cập thông qua Window.sharedStorage.

Thuộc tính phiên bản

worklet Deprecated

Chứa phiên bản SharedStorageWorklet đại diện cho worklet bộ nhớ dùng chung cho nguồn gốc hiện tại. SharedStorageWorklet bao gồm phương thức addModule(), được sử dụng để thêm module vào worklet bộ nhớ dùng chung.

Phương thức phiên bản

WindowSharedStorage kế thừa các thuộc tính từ giao diện cha SharedStorage.

run() Deprecated

Thực thi thao tác Run output gate đã được đăng ký trong module được thêm vào SharedStorageWorklet của nguồn gốc hiện tại.

selectURL() Deprecated

Thực thi thao tác URL Selection output gate đã được đăng ký trong module được thêm vào SharedStorageWorklet của nguồn gốc hiện tại.

Ví dụ

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();

Xem trang đích Shared Storage API để được hướng dẫn về ví dụ này và các liên kết đến các ví dụ khác.

Thông số kỹ thuật

This feature does not appear to be defined in any specification.

Tương thích trình duyệt

Xem thêm