So recently, I began learning how to make extensions in chrome. I was trying to modify one example given in the chrome dev docs here.
I want to modify it so that I can also ungroup tabs which are already grouped if I click on the button again.
I studied the documentation of both tabs and tabGroups to see how this could be done. I decided to use the chrome API tabs.ungroup
for ungrouping grouped tabs.
async function groupTabs(tabIds) { const group = await chrome.tabs.group({ tabIds }); await chrome.tabGroups.update(group, { title: "Docs", collapsed: true, color: "cyan" });}async function groupUngroupTabs(tabs) { const tabIds = tabs.map(({ id }) => id); if (!tabIds.length) { return; } const groupIds = tabs.map(({ groupId }) => groupId); // if there is only one tab and that is not grouped, then group it if (groupIds.length === 1 && groupIds[0] === chrome.tabGroups.TAB_GROUP_ID_NONE) { await groupTabs(tabIds); return; } for (let i = 0; i < groupIds.length-1; i++) { // if any of the tabs is not in a group, or two tabs are in different groups, then group // them together if (groupIds[i] === chrome.tabGroups.TAB_GROUP_ID_NONE || groupIds[i] !== groupIds[i+1]) { await groupTabs(tabIds); console.log(tabs.map(({ groupId }) => groupId)); return; } } // otherwise, ungroup the tabs await chrome.tabs.ungroup(tabIds);}
The problem is that this works only once. If the tabs are ungrouped, then they will get grouped fine, but clicking again does nothing. Now if I close the extension popup and reopen it and click, the tabs will get ungrouped, but then clicking again won't group them back.
Since the tab group information is stored in tab.groupId
, I began console logging the groupId
of the tabs after and before grouping/ungrouping them.
I noticed that grouping/ungrouping them doesn't change the groupId
of the tabs. I read documentation and was hoping to find something in tab.update()
but I couldn't find anything.
Interestingly, the groupId
of the tabs does update, but only after I close the extension. That is why closing and opening the extension works as expected, but obviously I would like to have the functionality without doing that everytime.
I would be grateful if anyone can help me, or point me in the right direction.