-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
112 lines (92 loc) · 3.39 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// chrome.action.onClicked.addListener(() => {
// keepAlive(groupTabs);
// });
async function keepAlive(callback) {
try {
await callback();
} catch (error) {
console.error("Error keeping service worker alive:", error);
}
}
async function groupTabs(groupName, regex) {
try {
// Get all open tabs
const tabs = await chrome.tabs.query({});
// Find or create the target tab group by name
let groupId = null;
for (const group of await chrome.tabGroups.query({})) {
const groupInfo = await chrome.tabGroups.get(group.id);
if (groupInfo.title === groupName) {
groupId = group.id;
break;
}
}
// If group not found, create a new one
if (!groupId) {
const firstTab = tabs[0]; // Pick an initial tab to create the group
groupId = await chrome.tabs.group({ tabIds: firstTab.id });
await chrome.tabGroups.update(groupId, { title: groupName });
}
// Add matching tabs to the group
for (const tab of tabs) {
if (regex.test(tab.url)) {
await chrome.tabs.group({ groupId, tabIds: tab.id });
}
}
console.log("Tabs matched and grouped successfully.");
} catch (error) {
console.error("Error grouping tabs:", error);
}
}
chrome.runtime.onMessage.addListener((request) => {
if (request.action === "groupMatchingTabs") {
const { groupName, regexInput } = request;
// Validate regex input
let regex;
try {
regex = new RegExp(regexInput);
} catch (e) {
console.error("Invalid regular expression:", e);
return;
}
// Keep the service worker alive while grouping tabs
keepAlive(() => groupTabs(groupName, regex));
}
});
// async function groupTabs() {
// const regex = /github\.com/i; // Replace with your regular expression
// const groupName = "GH"; // Desired name of the tab group
// try {
// // Get all open tabs
// const tabs = await chrome.tabs.query({});
// // Find or create the target tab group
// let groupId = null;
// for (const group of await chrome.tabGroups.query({})) {
// const groupInfo = await chrome.tabGroups.get(group.id);
// if (groupInfo.title === groupName) {
// groupId = group.id;
// break;
// }
// }
// // If group not found, create a new one
// if (!groupId) {
// const firstTab = tabs[0]; // Pick an initial tab to create the group
// groupId = await chrome.tabs.group({ tabIds: firstTab.id });
// await chrome.tabGroups.update(groupId, { title: groupName });
// }
// // Add matching tabs to the group
// for (const tab of tabs) {
// if (regex.test(tab.url)) {
// await chrome.tabs.group({ groupId, tabIds: tab.id });
// }
// }
// console.log("Tabs matched and grouped successfully.");
// } catch (error) {
// console.error("Error grouping tabs:", error);
// }
// }
// chrome.runtime.onMessage.addListener((request) => {
// if (request.action === "groupMatchingTabs") {
// keepAlive(groupTabs); // Call the main function to group tabs
// }
// });