tabs.group()

将一个或多个标签页添加到组中,或者,如果未指定组,则将标签页添加到新组中。标签页组中的所有标签页都必须是相邻的,并且如果需要,标签页会被移动。任何固定的标签页在分组之前都会被取消固定。

如果一个调用将标签页移出标签页组,并且其中任何一个标签页组变为空,则会删除空的标签页组。

注意: tabs.group() 方法不是对标签页进行分组的唯一方法。当 tabs.move 将标签页放置在属于标签页组的标签页之间时,标签页也会加入一个标签页组。

有关标签页组的更多信息,请参阅 tabGroups

语法

js
let grouping = browser.tabs.group(
  options    // object
)

参数

options

一个包含有关标签页分组详细信息的对象。

createProperties 可选

object。新组的配置详细信息。如果指定了 groupId,则不能使用此选项。

windowId 可选

integer。新组的窗口。默认为 当前窗口

groupId 可选

integer。要将标签页添加到的组的 ID。如果未指定,则会创建一个组。

tabIds

integerintegerarray。要添加到组的标签页 ID 或标签页 ID 列表。必须至少包含一个标签页 ID。

返回值

一个 Promise,它会以一个整数(包含标签页被添加到的标签页组的 groupId)来fulfilled。如果找不到 groupId,任何 tabIds 无效,windowId 无效,或者发生其他错误,则 promise 会以错误消息被rejected。当发生验证错误时,标签页不会被修改。

示例

创建两个标签页并将它们放入新组,然后创建另一个标签页并将其添加到该组。

js
// Create two tabs and put them in a new group.
const tab1 = await browser.tabs.create({});
const tab2 = await browser.tabs.create({});
const groupId = await browser.tabs.group({
  tabIds: [tab1.id, tab2.id],
});

// Create another tab and add it to the group.
const tab3 = await browser.tabs.create({});
await browser.tabs.group({
  tabIds: tab3.id,
  groupId,
});

创建一个标签页并将其分组与当前标签页匹配。

js
let [oldTab] = await browser.tabs.query({
  active: true,
  lastFocusedWindow: true,
});

let newTab = await browser.tabs.create({
  url: "https://example.com/",
  index: oldTab.index + 1,
});
// Feature detection: tab grouping is a relatively new feature.
// All tabs are ungrouped if the API does not exist.
if (browser.tabs.group) {
  if (oldTab.groupId !== -1) {
    // oldTab is in a group, add newTab to the same group
    await browser.tabs.group({ groupId: oldTab.groupId, tabIds: [newTab.id] });
  } else {
    // oldTab isn't in a group
    // Although a new tab positioned next to an ungrouped tab is
    // already ungrouped, we call ungroup() in case this example is
    // adopted for use with tabs that aren't adjacent. When oldTab
    // is not in a tab group, the only way to ensure that newTab isn't
    // in a tab group is by using ungroup().
    await browser.tabs.ungroup(newTab.id);
  }
}

浏览器兼容性