contextualIdentities.move()

將一個或多個上下文身份移動到上下文身份列表中的新位置。

這是一個非同步函式,返回一個 Promise

語法

js
let moveContainers = browser.contextualIdentities.move(
  cookieStoreIds,                  // string or array of string
  position                         // integer
)

引數

cookieStoreIds

stringstringarray。要移動的上下文身份 cookie 儲存 ID 的有序列表。

position

integer。將 cookieStoreIds 移動到上下文身份列表中位置的整數。從零開始;0 表示第一個位置。-1 表示將專案移動到列表末尾。

返回值

一個 Promise,當上下文身份重新排序時,該 Promise 被 fulfilled。如果請求無效移動或上下文身份功能未啟用,則 Promise 被 rejected。

示例

此示例將第一個身份移動到末尾,然後再移動回開頭。

js
let identities = await browser.contextualIdentities.query({});
let firstId = identities[0].cookieStoreId;

// Moves first identity to the end.
await browser.contextualIdentities.move(firstId, -1);

// Move identity to the start again.
await browser.contextualIdentities.move(firstId, 0);

將第一個身份移動到末尾的另一種方法是將所有其他身份移動到開頭。

js
let identities = await browser.contextualIdentities.query({});
let ids = identities.map((identity) => identity.cookieStoreId);
// Create an array without the first item:
let otherIds = ids.slice(1);

// Move other identities to the start,
// effectively putting the first identity at the end.
await browser.contextualIdentities.move(otherIds, 0);

此示例將“個人”身份移動到“工作”之前。該示例假定存在具有這些名稱的容器。在自定義或本地化(非英語)的 Firefox 例項中可能並非如此。

js
let identities = await browser.contextualIdentities.query({});

// Find the index and ID of the container with the name "Personal".
let personalIndex = identities.findIndex((ci) => ci.name === "Personal");
if (personalIndex === -1) {
  throw new Error("Personal container not found");
}
let personalId = identities[personalIndex].cookieStoreId;

// Find the index of the container with the name "Work".
let workIndex = identities.findIndex((identity) => identity.name === "Work");
if (workIndex === -1) {
  throw new Error("Work container not found!");
}

if (personalIndex < workIndex) {
  // When the Personal identity moves, all following
  // identities shift to the left by one. To place
  // the Personal identity before the Work identity,
  // we should therefore subtract one.
  workIndex--;
}
await browser.contextualIdentities.move(personalId, workIndex);

瀏覽器相容性