contextualIdentities.move()

将一个或多个上下文标识移动到上下文标识列表中的新位置。

这是一个异步函数,它返回一个 Promise

语法

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

参数

cookieStoreIds

字符串字符串 数组。要移动的上下文标识 cookie 存储 ID 的有序列表。

position

整数。在上下文标识列表中将 cookieStoreIds 移动到的位置。从 0 开始;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);

浏览器兼容性

BCD 表格仅在启用 JavaScript 的浏览器中加载。