Search code examples
javascriptgoogle-chrome-extensiontabs

Delete group tab with javascript


I want to delete a group tab using JavaScript.

I have the current tab id, and with that i get the current group tab id (if the current tab is not in a group tab it returns -1).

Now I wonder if there is something like this :

chrome.tabs.remove(tabId)

but for the group tab, so more like :

chrome.tabGroups.remove(groupId).

Here's my code if it helps :

chrome.tabs.query({ active: true, currentWindow: true }, async function (tabs) {
    console.log(tabs[0].id, tabs[0].groupId);
    let currentTab = tabs[0].id;
    let currentGroupId = tabs[0].groupId;

    //get all the infos about the group
    let infos = await chrome.tabGroups.get(tabs[0].groupId);
  }
);

Solution

  • Use chrome.tabs.query to find the tabs with the same groupId and then call chrome.tabs.remove with an array of these tabs' ids:

    async function closeGroup() {
      const [tab] = await chrome.tabs.query({active: true, currentWindow: true});
      const groupTabs = await chrome.tabs.query({groupId: tab.groupId});
      await chrome.tabs.remove(groupTabs.map(t => t.id));
    }