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);
  }
}

瀏覽器相容性