语法
js
let moveContainers = browser.contextualIdentities.move(
cookieStoreIds, // string or array of string
position // integer
)
参数
-
string或string的array。要移动的上下文身份 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);
浏览器兼容性
加载中…