Skip to content

Commit

Permalink
fix: group name is not set after reordering a tab (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienfontaine authored Jun 22, 2024
1 parent e9d094e commit ed330f1
Showing 1 changed file with 61 additions and 7 deletions.
68 changes: 61 additions & 7 deletions src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Group, Rule, TabModifierSettings } from './common/types.ts';
import {
_getDefaultRule,
_getDefaultTabModifierSettings,
_getRuleFromUrl,
_getStorageAsync,
_setStorage,
} from './common/storage.ts';
Expand Down Expand Up @@ -56,6 +57,20 @@ async function handleSetGroup(rule: Rule, tab: chrome.tabs.Tab) {
if (tabModifier) await applyGroupRuleToTab(rule, tab, tabModifier);
}

chrome.tabs.onMoved.addListener(async (tabId) => {
const tab = await chrome.tabs.get(tabId);
if (!tab) return;
if (!tab.url) return;

const tabModifier = await _getStorageAsync();
if (!tabModifier) return;

const rule = await _getRuleFromUrl(tab.url);
if (!rule) return;

await applyGroupRuleToTab(rule, tab, tabModifier);
});

chrome.runtime.onMessage.addListener(async (message, sender) => {
if (!sender.tab) return;

Expand Down Expand Up @@ -221,6 +236,7 @@ async function applyGroupRuleToTab(
// }
// }

let handleTabGroupsMaxRetries = 600;
async function handleTabGroups(
groups: chrome.tabGroups.TabGroup[],
tab: chrome.tabs.Tab,
Expand All @@ -233,26 +249,64 @@ async function handleTabGroups(
} else if (groups.length === 1) {
const group = groups[0];

chrome.tabs.group({ groupId: group.id, tabIds: [tab.id] }, (groupId: number) => {
updateTabGroup(groupId, tmGroup);
});
const execute = () => {
if (!tab.id) return;

chrome.tabs.group({ groupId: group.id, tabIds: [tab.id] }, (groupId: number) => {
if (chrome.runtime.lastError && handleTabGroupsMaxRetries > 0) {
setTimeout(() => execute(), 100);
handleTabGroupsMaxRetries--;
return;
} else {
handleTabGroupsMaxRetries = 600;
updateTabGroup(groupId, tmGroup);
}
});
};

execute();
}
}

let createAndSetupGroupMaxRetries = 600;
async function createAndSetupGroup(tabIds: number[], tmGroup: Group) {
chrome.tabs.group({ tabIds: tabIds }, (groupId: number) => {
updateTabGroup(groupId, tmGroup);
});
const execute = () => {
chrome.tabs.group({ tabIds: tabIds }, (groupId: number) => {
if (chrome.runtime.lastError && createAndSetupGroupMaxRetries > 0) {
setTimeout(() => execute(), 100);
createAndSetupGroupMaxRetries--;
return;
} else {
createAndSetupGroupMaxRetries = 600;
updateTabGroup(groupId, tmGroup);
}
});
};

execute();
}

let updateTabGroupMaxRetries = 600;
async function updateTabGroup(groupId: number, tmGroup: Group) {
if (!groupId) return;

const updateProperties = {
title: tmGroup.title,
color: tmGroup.color,
collapsed: tmGroup.collapsed,
} as chrome.tabGroups.UpdateProperties;

await chrome.tabGroups.update(groupId, updateProperties);
const execute = () => {
chrome.tabGroups.update(groupId, updateProperties, () => {
if (chrome.runtime.lastError && updateTabGroupMaxRetries > 0) {
setTimeout(() => execute(), 100);
updateTabGroupMaxRetries--;
return;
}
});
};

execute();
}

export {};

0 comments on commit ed330f1

Please sign in to comment.