-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
421 lines (361 loc) · 13.4 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
409
410
411
412
413
414
415
416
417
418
419
420
421
let selectedIndex = -1;
const maxResults = 100;
document.addEventListener('DOMContentLoaded', () => {
// Tab Navigation
const tabButtons = document.querySelectorAll('.tab-button');
tabButtons.forEach(button => {
button.addEventListener('click', () => {
// Remove active class from all tabs
tabButtons.forEach(btn => btn.classList.remove('active'));
document.querySelectorAll('.tab-pane').forEach(pane => pane.classList.remove('active'));
// Add active class to clicked tab
button.classList.add('active');
const tabId = button.dataset.tab;
document.getElementById(`${tabId}-tab`).classList.add('active');
// Initialize content based on tab
if (tabId === 'search') {
document.getElementById('searchInput').focus();
searchTabs('');
}
else if (tabId === 'organize') {
loadWindowsAndTabs();
} else if (tabId === 'manage') {
loadDuplicateTabs();
}
else if (tabId === 'screentime') {
loadScreenTime();
}
});
});
const searchInput = document.getElementById('searchInput');
// Focus search input on popup open
searchInput.focus();
searchInput.addEventListener('input', () => {
searchTabs(searchInput.value.toLowerCase());
});
// Keyboard navigation
document.addEventListener('keydown', (e) => {
const items = document.querySelectorAll('.tab-item');
switch(e.key) {
case 'ArrowDown':
e.preventDefault();
selectedIndex = Math.min(selectedIndex + 1, items.length - 1);
updateSelection(items);
break;
case 'ArrowUp':
e.preventDefault();
selectedIndex = Math.max(selectedIndex - 1, -1);
updateSelection(items);
break;
case 'Enter':
e.preventDefault();
if (selectedIndex >= 0) {
items[selectedIndex].click();
}
break;
}
});
// Initial search to show all tabs
searchTabs('');
});
function updateSelection(items) {
items.forEach((item, index) => {
item.classList.toggle('selected', index === selectedIndex);
if (index === selectedIndex) {
item.scrollIntoView({ block: 'nearest' });
}
});
}
async function searchTabs(query) {
try {
const windows = await chrome.windows.getAll({ populate: true });
const results = document.getElementById('searchResults');
const statsContainer = document.getElementById('searchStatsContainer');
results.innerHTML = '';
selectedIndex = -1;
// Calculate total tabs
const totalTabs = windows.reduce((sum, window) => sum + window.tabs.length, 0);
let matchingTabs = [];
const queryWords = query.split(/\s+/).filter(Boolean);
windows.forEach(window => {
window.tabs.forEach(tab => {
const title = tab.title.toLowerCase();
const url = tab.url.toLowerCase();
const titleMatches = queryWords.every(word => title.includes(word));
const urlMatches = queryWords.every(word => url.includes(word));
if (titleMatches || urlMatches) {
matchingTabs.push({
tab,
window,
titleMatch: titleMatches,
urlMatch: urlMatches
});
}
});
});
// Sort matches (title matches first)
matchingTabs.sort((a, b) => {
if (a.titleMatch && !b.titleMatch) return -1;
if (!a.titleMatch && b.titleMatch) return 1;
return 0;
});
// Update stats display - MOVED OUTSIDE THE LOOP
if (query.trim() === '') {
statsContainer.textContent = `${totalTabs} tab${totalTabs !== 1 ? 's' : ''} open`;
} else {
statsContainer.textContent = `${matchingTabs.length} of ${totalTabs} tab${totalTabs !== 1 ? 's' : ''}`;
}
// Limit results for performance
matchingTabs = matchingTabs.slice(0, maxResults);
matchingTabs.forEach(({ tab, window }) => {
const tabElement = document.createElement('div');
tabElement.className = 'tab-item';
tabElement.innerHTML = `
<div class="tab-content">
<img src="${tab.favIconUrl || 'default-favicon.png'}" class="tab-icon" alt="">
<div class="tab-info">
<div class="tab-title">${escapeHtml(tab.title)}</div>
<div class="tab-url">${escapeHtml(tab.url)}</div>
</div>
</div>
`;
tabElement.addEventListener('click', async () => {
try {
await chrome.windows.update(window.id, { focused: true });
await chrome.tabs.update(tab.id, { active: true });
window.close();
} catch (error) {
console.error('Failed to switch tab:', error);
}
});
results.appendChild(tabElement);
});
} catch (error) {
console.error('Error searching tabs:', error);
document.getElementById('searchResults').innerHTML =
'<div style="padding: 8px; color: red;">Error accessing tabs</div>';
}
}
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
async function loadDuplicateTabs() {
try {
const windows = await chrome.windows.getAll({ populate: true });
const duplicateResults = document.getElementById('duplicateResults');
const statsContainer = document.getElementById('manageStatsContainer');
duplicateResults.innerHTML = '';
// Group tabs by URL
const tabsByUrl = new Map();
windows.forEach(window => {
window.tabs.forEach(tab => {
const normalizedUrl = tab.url.toLowerCase();
if (!tabsByUrl.has(normalizedUrl)) {
tabsByUrl.set(normalizedUrl, []);
}
tabsByUrl.get(normalizedUrl).push({ tab, window });
});
});
// Filter and display only duplicate tabs
let duplicateGroups = [];
tabsByUrl.forEach((tabs, url) => {
if (tabs.length > 1) {
duplicateGroups.push(tabs);
}
});
// Update stats with chip style
statsContainer.textContent = `${duplicateGroups.length} set of duplicate${duplicateGroups.length !== 1 ? 's' : ''} found`;
// Display duplicate groups
duplicateGroups.forEach(tabs => {
const groupElement = document.createElement('div');
groupElement.className = 'duplicate-group';
const { tab } = tabs[tabs.length - 1]; // Get the latest tab
groupElement.innerHTML = `
<div class="duplicate-header">
<img src="${tab.favIconUrl || 'default-favicon.png'}" class="tab-icon" alt="">
<div class="duplicate-info">
<div class="tab-title">${escapeHtml(tab.title)}</div>
<span class="duplicate-count">${tabs.length} duplicates</span>
</div>
<button class="keep-latest-button">Keep Latest</button>
</div>
`;
// Add click handler
const keepLatestButton = groupElement.querySelector('.keep-latest-button');
keepLatestButton.addEventListener('click', async () => {
// Keep the latest tab (last in array) and close others
const tabIds = tabs.slice(0, -1).map(t => t.tab.id);
await chrome.tabs.remove(tabIds);
loadDuplicateTabs(); // Refresh list
});
duplicateResults.appendChild(groupElement);
});
} catch (error) {
console.error('Error loading duplicate tabs:', error);
document.getElementById('duplicateResults').innerHTML =
'<div style="padding: 8px; color: red;">Error accessing tabs</div>';
}
}
async function loadWindowsAndTabs() {
try {
const windows = await chrome.windows.getAll({ populate: true });
const container = document.getElementById('windowsContainer');
container.innerHTML = '';
// Get saved window names from storage
const windowNames = await getWindowNames();
windows.forEach(window => {
const windowElement = document.createElement('div');
windowElement.className = 'window-group';
const windowName = windowNames[window.id] || `Window ${window.id}`;
windowElement.innerHTML = `
<div class="window-header">
<div class="window-title-group">
<span class="window-name" data-window-id="${window.id}">${escapeHtml(windowName)}</span>
<button class="edit-name-button" title="Edit window name">
<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"/>
<path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"/>
</svg>
</button>
</div>
<span class="tab-count">${window.tabs.length} tabs</span>
</div>
<div class="window-tabs" data-window-id="${window.id}">
${window.tabs.map(tab => `
<div class="draggable-tab" draggable="true" data-tab-id="${tab.id}">
<img src="${tab.favIconUrl || 'default-favicon.png'}" class="tab-icon" alt="">
<div class="tab-info">
<div class="tab-title">${escapeHtml(tab.title)}</div>
</div>
</div>
`).join('')}
</div>
`;
// Add edit name functionality
const editButton = windowElement.querySelector('.edit-name-button');
const windowNameSpan = windowElement.querySelector('.window-name');
editButton.addEventListener('click', () => {
const input = document.createElement('input');
input.type = 'text';
input.className = 'window-name-input';
input.value = windowNameSpan.textContent;
windowNameSpan.replaceWith(input);
input.focus();
input.addEventListener('blur', async () => {
const newName = input.value.trim() || `Window ${window.id}`;
await saveWindowName(window.id, newName);
input.replaceWith(windowNameSpan);
windowNameSpan.textContent = newName;
});
input.addEventListener('keydown', async (e) => {
if (e.key === 'Enter') {
input.blur();
} else if (e.key === 'Escape') {
input.value = windowNameSpan.textContent;
input.blur();
}
});
});
container.appendChild(windowElement);
});
setupDragAndDrop();
} catch (error) {
console.error('Error loading windows and tabs:', error);
}
}
// Helper functions for window name storage
async function getWindowNames() {
try {
const data = await chrome.storage.local.get('windowNames');
return data.windowNames || {};
} catch (error) {
console.error('Error getting window names:', error);
return {};
}
}
async function saveWindowName(windowId, name) {
try {
const windowNames = await getWindowNames();
windowNames[windowId] = name;
await chrome.storage.local.set({ windowNames });
} catch (error) {
console.error('Error saving window name:', error);
}
}
function setupDragAndDrop() {
const draggableTabs = document.querySelectorAll('.draggable-tab');
const dropZones = document.querySelectorAll('.window-tabs');
draggableTabs.forEach(tab => {
tab.addEventListener('dragstart', handleDragStart);
tab.addEventListener('dragend', handleDragEnd);
});
dropZones.forEach(zone => {
zone.addEventListener('dragover', handleDragOver);
zone.addEventListener('dragleave', handleDragLeave);
zone.addEventListener('drop', handleDrop);
});
}
function handleDragStart(e) {
e.target.classList.add('dragging');
e.dataTransfer.setData('text/plain', JSON.stringify({
tabId: e.target.dataset.tabId,
sourceWindowId: e.target.closest('.window-tabs').dataset.windowId
}));
}
function handleDragEnd(e) {
e.target.classList.remove('dragging');
document.querySelectorAll('.window-tabs').forEach(zone => {
zone.classList.remove('drag-over');
});
}
function handleDragOver(e) {
e.preventDefault();
e.currentTarget.classList.add('drag-over');
}
function handleDragLeave(e) {
e.currentTarget.classList.remove('drag-over');
}
async function handleDrop(e) {
e.preventDefault();
const dropZone = e.currentTarget;
dropZone.classList.remove('drag-over');
const data = JSON.parse(e.dataTransfer.getData('text/plain'));
const targetWindowId = parseInt(dropZone.dataset.windowId);
const tabId = parseInt(data.tabId);
if (targetWindowId !== parseInt(data.sourceWindowId)) {
try {
await chrome.tabs.move(tabId, { windowId: targetWindowId, index: -1 });
loadWindowsAndTabs(); // Refresh the view
} catch (error) {
console.error('Error moving tab:', error);
}
}
}
async function loadScreenTime() {
const screenTimeResults = document.getElementById('screentimeResults');
screenTimeResults.innerHTML = '';
const { screenTime } = await chrome.storage.local.get('screenTime');
if (!screenTime) {
screenTimeResults.textContent = 'No data available.';
return;
}
const sortedDomains = Object.entries(screenTime).sort((a, b) => b[1] - a[1]);
sortedDomains.forEach(([domain, time]) => {
const timeSpent = (time / 1000 / 60).toFixed(2); // Convert to minutes
const domainElement = document.createElement('div');
domainElement.className = 'screentime-item';
domainElement.innerHTML = `
<div class="screentime-domain">${escapeHtml(domain)}</div>
<div class="screentime-bar">
<div class="screentime-bar-fill" style="width: ${Math.min(timeSpent / 60 * 100, 100)}%;"></div>
</div>
<div class="screentime-time">${timeSpent} minutes</div>
`;
screenTimeResults.appendChild(domainElement);
});
}