forked from abevol/AutoGroupTabs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
175 lines (158 loc) · 4.11 KB
/
background.js
File metadata and controls
175 lines (158 loc) · 4.11 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
chrome.runtime.onInstalled.addListener(function(details)
{
details && "install" == details.reason && groupAllTabs();
});
chrome.tabs.onCreated.addListener(function(tab)
{
groupTab(tab);
});
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab)
{
if (changeInfo.url != undefined)
{
groupTab(tab);
}
});
// Listen for the installation event
chrome.runtime.onInstalled.addListener(function(details) {
// Check if the extension is being installed (as opposed to updated, etc.)
if (details.reason === 'install') {
// Set the default rules
chrome.storage.sync.set({
rules: ['*.bilibili.com', '*.openai.com']
});
}
});
chrome.storage.onChanged.addListener(function(changes, namespace) {
for (var key in changes) {
if (key === 'rules') {
loadRules();
}
}
});
const colors = ["grey", "blue", "yellow", "red", "green", "pink", "purple", "cyan"]
let rules = [];
// Load the rules from storage
function loadRules() {
chrome.storage.sync.get('rules', function(data) {
if (data.rules) {
rules = data.rules;
}
});
}
loadRules();
function genGroupName(url)
{
url = new URL(url);
let hostname = url.hostname;
// Check if the hostname matches any of the user rules
for (var i = 0; i < rules.length; i++) {
var rule = rules[i];
if (rule.startsWith('*.')) {
// Remove the '*.' from the rule and check if the hostname ends with it
var ruleHost = rule.slice(2);
if (hostname.endsWith(ruleHost)) {
return rule;
}
} else if (hostname === rule) {
return rule;
}
}
if (url.protocol != "http:" && url.protocol != "https:")
{
return url.protocol.substr(0, url.protocol.length - 1);
}
let hostName = url.hostname;
let groupName = hostName.startsWith("www.") ? hostName.substr(4) : hostName;
return groupName;
}
let tabIdx = 0;
let allTabs = [];
function onGroupTabComplete()
{
tabIdx++;
if (tabIdx < allTabs.length)
{
let tab = allTabs[tabIdx];
groupTab(tab, onGroupTabComplete);
}
}
function groupAllTabs()
{
console.debug("groupAllTabs");
chrome.tabs.query(
{
currentWindow: true
}, function(tabs)
{
tabIdx = 0;
allTabs = tabs;
groupTab(allTabs[tabIdx], onGroupTabComplete);
});
}
function groupTab(tab, complete)
{
if (tab.url == "" || tab.pinned)
{
complete && complete();
return;
}
chrome.windows.getCurrent(function(currentWindow)
{
chrome.tabGroups.query(
{
windowId: currentWindow.id
}, function(groups)
{
groupTabIntl(tab, groups, currentWindow, complete);
})
});
}
function groupTabIntl(tab, groups, currentWindow, complete)
{
try
{
let groupName = genGroupName(tab.url);
const existedGroup = groups.find(a => a.title == groupName);
if (existedGroup == undefined)
{
chrome.tabs.group(
{
createProperties:
{
windowId: currentWindow.id,
},
tabIds: tab.id
}, function(groupId)
{
console.debug("add group", groupName);
chrome.tabGroups.update(groupId,
{
color: colors[parseInt(Math.random() * 10)],
title: groupName,
}, function(group)
{
console.debug("group added", group.title);
complete && complete();
});
})
}
else
{
console.debug("update group", groupName);
chrome.tabs.group(
{
groupId: existedGroup.id,
tabIds: tab.id
}, function(groupId)
{
console.debug("group updated", groupName);
complete && complete();
})
}
}
catch (e)
{
console.error(e)
}
}