-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
189 lines (174 loc) · 4.95 KB
/
Copy pathbackground.js
File metadata and controls
189 lines (174 loc) · 4.95 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// background.js
let activeTabsByDomain = {};
const domainStyles = {
'google.com': { tag: '❗️' },
'youtube.com': { tag: '📍' },
'github.com': { tag: '🟠' },
'chatgpt.com': { tag: '❗' },
'default': { tag: '🏷' }
};
function getStyleForDomain(domain) {
return domainStyles[domain] || domainStyles['default'];
}
function updateActiveDomainTab(tab) {
const domain = new URL(tab.url).hostname;
if (activeTabsByDomain[domain]) {
activeTabsByDomain[domain] = {
...activeTabsByDomain[domain],
previousTabId: activeTabsByDomain[domain].tabId,
tabId: tab.id,
windowId: tab.windowId,
url: tab.url,
title: tab.title,
favicon: tab.favIconUrl || '',
lastAccessed: new Date().toISOString()
};
} else {
activeTabsByDomain[domain] = {
domain,
tabId: tab.id,
previousTabId: null,
windowId: tab.windowId,
url: tab.url,
title: tab.title,
favicon: tab.favIconUrl || '',
lastAccessed: new Date().toISOString()
};
}
chrome.storage.local.set({ activeTabsByDomain });
applyHighlight(domain);
}
function applyHighlight(domain) {
if (!activeTabsByDomain[domain]) return;
const domainInfo = activeTabsByDomain[domain];
const style = getStyleForDomain(domain);
chrome.tabs.query({}, (tabs) => {
const domainTabs = tabs.filter(tab => {
if (!tab.url) return false;
try {
return new URL(tab.url).hostname === domain;
} catch (_) {
return false;
}
});
domainTabs.forEach(tab => {
const isActive = tab.id === domainInfo.tabId;
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: (tag, isActiveTab) => {
const currentTitle = document.title;
// 动态构建正则:匹配当前域名对应的标签符号
const regex = new RegExp(`^${tag}\\s*`, 'u');
// 清理历史符号(无论是否当前活动)
const stripped = document.title.replace(regex, '');
document.title = isActiveTab ? tag + ' ' + stripped : stripped;
},
args: [style.tag, isActive]
});
});
});
}
function removeTabRecord(tabId) {
for (const domain in activeTabsByDomain) {
if (activeTabsByDomain[domain].tabId === tabId) {
const previousId = activeTabsByDomain[domain].previousTabId;
if (previousId) {
chrome.tabs.get(previousId, (prevTab) => {
if (chrome.runtime.lastError) {
delete activeTabsByDomain[domain];
} else {
activeTabsByDomain[domain] = {
...activeTabsByDomain[domain],
tabId: prevTab.id,
previousTabId: null,
url: prevTab.url,
title: prevTab.title,
favicon: prevTab.favIconUrl || '',
lastAccessed: new Date().toISOString()
};
applyHighlight(domain);
}
chrome.storage.local.set({ activeTabsByDomain });
});
} else {
delete activeTabsByDomain[domain];
chrome.storage.local.set({ activeTabsByDomain });
}
break;
}
}
}
chrome.tabs.onActivated.addListener(async (activeInfo) => {
try {
const tab = await chrome.tabs.get(activeInfo.tabId);
if (tab.url?.startsWith('http')) {
updateActiveDomainTab(tab);
}
} catch (error) {
console.error("激活事件错误:", error);
}
});
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tab.url?.startsWith('http')) {
try {
const domain = new URL(tab.url).hostname;
let isActive = false;
for (const d in activeTabsByDomain) {
if (activeTabsByDomain[d].tabId === tabId) {
isActive = true;
if (d !== domain) {
delete activeTabsByDomain[d];
updateActiveDomainTab(tab);
} else {
activeTabsByDomain[d] = {
...activeTabsByDomain[d],
url: tab.url,
title: tab.title,
favicon: tab.favIconUrl || '',
lastAccessed: new Date().toISOString()
};
chrome.storage.local.set({ activeTabsByDomain });
applyHighlight(d);
}
break;
}
}
if (!isActive && tab.active) updateActiveDomainTab(tab);
} catch (err) {
console.error("更新标签页出错:", err);
}
}
});
chrome.tabs.onRemoved.addListener(tabId => removeTabRecord(tabId));
chrome.runtime.onStartup.addListener(async () => {
try {
const { activeTabsByDomain: saved } = await chrome.storage.local.get('activeTabsByDomain');
if (saved) {
activeTabsByDomain = saved;
for (const domain in activeTabsByDomain) {
const info = activeTabsByDomain[domain];
chrome.tabs.get(info.tabId, tab => {
if (chrome.runtime.lastError) {
delete activeTabsByDomain[domain];
} else {
applyHighlight(domain);
}
});
}
chrome.storage.local.set({ activeTabsByDomain });
}
} catch (err) {
console.error("启动加载出错:", err);
}
});
chrome.runtime.onInstalled.addListener(async () => {
try {
await chrome.storage.local.set({ activeTabsByDomain: {} });
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
if (tabs[0]?.url?.startsWith('http')) {
updateActiveDomainTab(tabs[0]);
}
} catch (err) {
console.error("扩展初始化错误:", err);
}
});