-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
408 lines (350 loc) · 14.9 KB
/
popup.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
document.addEventListener('DOMContentLoaded', function () {
const tabGroups = document.getElementById('tab-groups');
const searchInput = document.getElementById('search');
const sortSelect = document.getElementById('sort-select');
const groupButton = document.getElementById('group-tabs');
const saveSessionButton = document.getElementById('save-session');
const loadSessionButton = document.getElementById('load-session');
const closeSelectedButton = document.getElementById('close-selected');
const statsDiv = document.getElementById('stats');
let allTabs = [];
// searchInput 自动聚焦
searchInput.focus();
function getTabs() {
return new Promise((resolve) => {
chrome.tabs.query({}, resolve);
});
}
function groupTabs(tabs) {
const groups = {};
tabs.forEach(tab => {
const domain = new URL(tab.url).hostname;
if (!groups[domain]) groups[domain] = [];
groups[domain].push(tab);
});
return groups;
}
function displayTabs(groups) {
tabGroups.innerHTML = '';
let tabIndex = 1;
const tabElements = [];
for (const domain in groups) {
const groupElement = document.createElement('div');
groupElement.className = 'group';
const groupHeader = document.createElement('div');
groupHeader.className = 'group-header';
groupHeader.textContent = domain;
groupElement.appendChild(groupHeader);
groups[domain].forEach(tab => {
const tabElement = document.createElement('div');
tabElement.className = `tab-item${tab.pinned ? ' pinned' : ''}`;
tabElement.dataset.tabId = tab.id;
if (tabIndex <= 9) {
const shortcutSpan = document.createElement('span');
shortcutSpan.className = 'tab-shortcut';
shortcutSpan.textContent = tabIndex;
tabElement.appendChild(shortcutSpan);
}
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.className = 'tab-checkbox';
checkbox.addEventListener('click', (e) => {
e.stopPropagation();
});
tabElement.appendChild(checkbox);
const favicon = document.createElement('img');
favicon.className = 'tab-favicon';
favicon.src = tab.favIconUrl || 'default-favicon.png';
tabElement.appendChild(favicon);
const tabTitle = document.createElement('span');
tabTitle.textContent = tab.title;
tabElement.appendChild(tabTitle);
const closeButton = document.createElement('span');
closeButton.className = 'tab-close';
closeButton.textContent = '✕';
closeButton.addEventListener('click', (e) => {
e.stopPropagation();
chrome.tabs.remove(tab.id);
tabElement.remove();
updateStats();
});
tabElement.appendChild(closeButton);
tabElement.addEventListener('click', async function (e) {
if (e.target !== checkbox) {
await updatePinnedStatus(tab.id, true);
updateTabElementPinnedStatus(tabElement, true);
chrome.tabs.update(tab.id, { active: true });
window.close();
}
});
groupElement.appendChild(tabElement);
tabElements.push(tabElement);
tabIndex++;
});
tabGroups.appendChild(groupElement);
}
updateStats();
return tabElements;
}
function getDisplayWithFilter(searchTerm) {
const filteredTabs = filterTabs(allTabs, searchTerm);
const groups = groupTabs(filteredTabs);
const tabElements = displayTabs(groups);
return { count: filteredTabs.length, elements: tabElements };
}
function updateStats() {
const totalTabs = allTabs.length;
const totalDomains = Object.keys(groupTabs(allTabs)).length;
statsDiv.textContent = `总标签数: ${totalTabs} | 总域名数: ${totalDomains}`;
}
function sortTabs(tabs, method) {
switch (method) {
case 'title':
return tabs.sort((a, b) => a.title.localeCompare(b.title));
case 'domain':
return tabs.sort((a, b) => {
const domainA = new URL(a.url).hostname;
const domainB = new URL(b.url).hostname;
return domainA.localeCompare(domainB);
});
default:
return tabs;
}
}
function filterTabs(tabs, searchTerm) {
const searchLower = searchTerm.toLowerCase();
return tabs.filter(tab => {
const title = tab.title.toLowerCase();
const url = new URL(tab.url);
const domain = url.hostname.toLowerCase();
const fullUrl = tab.url.toLowerCase();
// 检查标题
if (title.includes(searchLower)) {
return true;
}
// 检查完整URL
if (fullUrl.includes(searchLower)) {
return true;
}
// 检查域名及其部分
const domainParts = domain.split('.');
return domainParts.some(part => part.includes(searchLower));
});
}
function updateDisplayWithFilter(searchTerm) {
const filteredTabs = filterTabs(allTabs, searchTerm);
const groups = groupTabs(filteredTabs);
displayTabs(groups);
return filteredTabs.length;
}
async function updateDisplay() {
allTabs = await getTabs();
const groups = groupTabs(allTabs);
displayTabs(groups);
}
updateDisplay();
// 修改搜索输入事件处理器
searchInput.addEventListener('input', function () {
updateDisplayWithFilter(this.value);
});
searchInput.addEventListener('keydown', function (e) {
if (e.key === 'Enter') {
const filteredCount = updateDisplayWithFilter(this.value);
if (filteredCount === 1) {
const singleTab = document.querySelector('.tab-item');
if (singleTab) {
const tabId = parseInt(singleTab.dataset.tabId);
chrome.tabs.update(tabId, { active: true });
window.close();
}
}
} else if (e.ctrlKey && e.key >= '1' && e.key <= '9') {
e.preventDefault(); // 阻止默认行为,如 Ctrl+1 打开第一个书签
const index = parseInt(e.key) - 1;
const filteredResult = getDisplayWithFilter(searchInput.value);
if (filteredResult.count <= 9 && index < filteredResult.count) {
const tabElement = filteredResult.elements[index];
const tabId = parseInt(tabElement.dataset.tabId);
chrome.tabs.update(tabId, { active: true });
window.close();
}
}
});
// 添加数字键快捷方式
document.addEventListener('keydown', function (e) {
if (e.key === 'Escape') {
window.close(); // 关闭弹出窗口
} else if (e.ctrlKey && e.key >= '1' && e.key <= '9') {
e.preventDefault(); // 阻止默认行为,如 Ctrl+1 打开第一个书签
const index = parseInt(e.key) - 1;
const filteredResult = getDisplayWithFilter(searchInput.value);
if (filteredResult.count <= 9 && index < filteredResult.count) {
const tabElement = filteredResult.elements[index];
const tabId = parseInt(tabElement.dataset.tabId);
chrome.tabs.update(tabId, { active: true });
window.close();
}
}
});
sortSelect.addEventListener('change', function () {
const sortedTabs = sortTabs(allTabs, this.value);
const groups = groupTabs(sortedTabs);
displayTabs(groups);
});
groupButton.addEventListener('click', async function () {
const tabs = await getTabs();
const groups = groupTabs(tabs);
for (const domain in groups) {
const tabIds = groups[domain].map(tab => tab.id);
chrome.tabs.group({ tabIds }, function (groupId) {
chrome.tabGroups.update(groupId, { title: domain });
});
}
updateDisplay();
});
saveSessionButton.addEventListener('click', function () {
const session = allTabs.map(tab => ({ url: tab.url, title: tab.title }));
localStorage.setItem('savedSession', JSON.stringify(session));
alert('会话已保存');
});
loadSessionButton.addEventListener('click', function () {
const savedSession = JSON.parse(localStorage.getItem('savedSession'));
if (savedSession) {
savedSession.forEach(tab => {
chrome.tabs.create({ url: tab.url });
});
alert('会话已加载');
} else {
alert('没有保存的会话');
}
});
closeSelectedButton.addEventListener('click', function () {
const checkboxes = document.querySelectorAll('.tab-checkbox:checked');
const tabIds = Array.from(checkboxes).map(checkbox =>
parseInt(checkbox.closest('.tab-item').dataset.tabId)
);
chrome.tabs.remove(tabIds);
updateDisplay();
});
const recentTabsList = document.getElementById('recent-tabs-list');
// 获取并显示最近关闭的标签页
function displayRecentTabs() {
chrome.history.search({ text: '', maxResults: 10 }, function (historyItems) {
recentTabsList.innerHTML = '';
const recentTabs = historyItems.filter(item => item.url !== undefined);
recentTabs.forEach((tab, index) => {
const tabElement = document.createElement('div');
tabElement.className = 'recent-tab-item';
const favicon = document.createElement('img');
favicon.className = 'tab-favicon';
favicon.src = `https://www.google.com/s2/favicons?domain=${new URL(tab.url).hostname}`;
tabElement.appendChild(favicon);
const tabTitle = document.createElement('span');
tabTitle.textContent = tab.title || tab.url;
tabElement.appendChild(tabTitle);
tabElement.addEventListener('click', function () {
chrome.tabs.create({ url: tab.url });
});
recentTabsList.appendChild(tabElement);
});
});
}
// 初始显示最近关闭的标签页
displayRecentTabs();
// 每次打开插件时刷新最近关闭的标签页列表
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.action === "popupOpened") {
displayRecentTabs();
}
});
const commandInput = document.getElementById('command-input');
// 命令处理函数
const commands = {
'/help': () => {
return `可用命令:
/help - 显示帮助信息
/close_duplicates - 关闭所有重复标签
/bookmark_all - 将所有标签保存为书签
/close_all - 关闭所有标签(保留当前标签)
/close_left - 关闭左侧所有标签
/close_right - 关闭右侧所有标签`;
},
'/close_duplicates': async () => {
const tabs = await getTabs();
const uniqueUrls = new Set();
const duplicates = [];
tabs.forEach(tab => {
if (uniqueUrls.has(tab.url)) {
duplicates.push(tab.id);
} else {
uniqueUrls.add(tab.url);
}
});
await chrome.tabs.remove(duplicates);
return `已关闭 ${duplicates.length} 个重复标签`;
},
'/bookmark_all': async () => {
const tabs = await getTabs();
const folder = await chrome.bookmarks.create({ title: 'Saved Tabs ' + new Date().toLocaleString() });
for (const tab of tabs) {
await chrome.bookmarks.create({ parentId: folder.id, title: tab.title, url: tab.url });
}
return `已将 ${tabs.length} 个标签保存为书签`;
},
'/close_all': async () => {
const tabs = await getTabs();
const currentTab = tabs.find(tab => tab.active);
const tabsToClose = tabs.filter(tab => tab.id !== currentTab.id).map(tab => tab.id);
await chrome.tabs.remove(tabsToClose);
return `已关闭 ${tabsToClose.length} 个标签`;
},
'/close_left': async () => {
const tabs = await getTabs();
const currentTabIndex = tabs.findIndex(tab => tab.active);
const tabsToClose = tabs.slice(0, currentTabIndex).map(tab => tab.id);
await chrome.tabs.remove(tabsToClose);
return `已关闭左侧 ${tabsToClose.length} 个标签`;
},
'/close_right': async () => {
const tabs = await getTabs();
const currentTabIndex = tabs.findIndex(tab => tab.active);
const tabsToClose = tabs.slice(currentTabIndex + 1).map(tab => tab.id);
await chrome.tabs.remove(tabsToClose);
return `已关闭右侧 ${tabsToClose.length} 个标签`;
}
};
// 处理命令输入
commandInput.addEventListener('keydown', async function (e) {
if (e.key === 'Enter') {
const command = this.value.trim().toLowerCase();
let result = '';
if (commands[command]) {
result = await commands[command]();
} else {
result = '未知命令。输入 /help 查看可用命令。';
}
// 显示命令执行结果
const resultElement = document.createElement('div');
resultElement.className = 'command-result';
resultElement.textContent = result;
this.parentNode.insertBefore(resultElement, this.nextSibling);
// 清空输入框
this.value = '';
// 更新标签页显示
updateDisplay();
}
});
});
async function updatePinnedStatus(tabId, isPinned) {
await chrome.tabs.update(tabId, { pinned: isPinned });
if (isPinned) {
await chrome.tabs.move(tabId, { index: 0 });
}
}
function updateTabElementPinnedStatus(tabElement, isPinned) {
if (isPinned) {
tabElement.classList.add('pinned');
} else {
tabElement.classList.remove('pinned');
}
}