cookies.CookieStore

cookies API 的 CookieStore 类型代表浏览器中的一个 cookie 存储区。

不同浏览模式下的窗口可能使用不同的 cookie 存储区。例如,隐私浏览(或无痕)模式窗口使用独立的 cookie 存储区,与非隐私窗口不同。此外,在 Firefox 中使用 容器标签页时,一个窗口可能有多个 cookie 存储区。

有关 cookie 存储区的更多信息,请参阅 使用 Cookies API

类型

此类型的值是可能包含以下属性的对象

id

一个 string,包含 cookie 存储区的唯一标识符。

incognito 可选

一个布尔值,指示这是否为无痕 cookie 存储区。Chrome 或 Safari 不支持此属性。但是,您可以通过其 id 始终为 "1" 来识别 Chrome 中的无痕 cookie 存储区。

tabIds

一个 integers 数组,用于标识共享此 cookie 存储区的所有浏览器标签页。

示例

在此代码片段中,cookies.getAllCookieStores() 方法用于检索浏览器中所有可用的 cookie 存储区。然后,它会打印出每个 cookie 存储区的 ID 以及共享每个 cookie 存储区的标签页。

js
function logStores(cookieStores) {
  for (const store of cookieStores) {
    console.log(`Cookie store: ${store.id}\n Tab IDs: ${store.tabIds}`);
  }
}

browser.cookies.getAllCookieStores().then(logStores);

此代码片段获取所有 cookie 存储区,然后记录存储区的总数以及无痕存储区的数量。

js
browser.cookies.getAllCookieStores().then((stores) => {
  const incognitoStores = stores.map((store) => store.incognito);
  console.log(
    `Of ${stores.length} cookie stores, ${incognitoStores.length} are incognito.`,
  );
});

浏览器兼容性

注意:此 API 基于 Chromium 的 chrome.cookies API。本文档来源于 Chromium 代码中的 cookies.json