-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
100 lines (88 loc) · 2.85 KB
/
popup.js
File metadata and controls
100 lines (88 loc) · 2.85 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
// DOM Elements
const sortBtn = document.getElementById('sortBtn');
const saveBtn = document.getElementById('saveBtn');
const loadBtn = document.getElementById('loadBtn');
const statusBar = document.getElementById('statusBar');
const statusText = document.getElementById('statusText');
const statusIcon = document.querySelector('.status-icon');
// Status update helper
function updateStatus(message, type = 'default') {
statusBar.className = 'status-bar ' + type;
statusText.textContent = message;
const icons = {
default: '✨',
loading: '⟳',
success: '✓',
error: '✕'
};
statusIcon.textContent = icons[type] || icons.default;
}
// Set button loading state
function setButtonLoading(button, loading) {
if (loading) {
button.classList.add('loading');
} else {
button.classList.remove('loading');
}
}
// Send message to background script
async function sendMessage(action) {
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage({ action }, (response) => {
if (chrome.runtime.lastError) {
reject(new Error(chrome.runtime.lastError.message));
} else if (response && response.error) {
reject(new Error(response.error));
} else {
resolve(response);
}
});
});
}
// Sort & Group Tabs
sortBtn.addEventListener('click', async () => {
setButtonLoading(sortBtn, true);
updateStatus('Sorting and grouping tabs...', 'loading');
try {
const response = await sendMessage('sortAndGroup');
updateStatus(`Created ${response.groupCount} group${response.groupCount !== 1 ? 's' : ''} from ${response.tabCount} tabs`, 'success');
} catch (error) {
updateStatus('Failed to sort tabs: ' + error.message, 'error');
} finally {
setButtonLoading(sortBtn, false);
}
});
// Save All Tabs
saveBtn.addEventListener('click', async () => {
setButtonLoading(saveBtn, true);
updateStatus('Saving your tabs...', 'loading');
try {
const response = await sendMessage('saveTabs');
updateStatus(`Saved ${response.tabCount} tabs successfully`, 'success');
} catch (error) {
updateStatus('Failed to save tabs: ' + error.message, 'error');
} finally {
setButtonLoading(saveBtn, false);
}
});
// Load Saved Tabs
loadBtn.addEventListener('click', async () => {
setButtonLoading(loadBtn, true);
updateStatus('Loading saved tabs...', 'loading');
try {
const response = await sendMessage('loadTabs');
if (response.tabCount === 0) {
updateStatus('No saved tabs found', 'default');
} else {
updateStatus(`Restored ${response.tabCount} tabs`, 'success');
}
} catch (error) {
updateStatus('Failed to load tabs: ' + error.message, 'error');
} finally {
setButtonLoading(loadBtn, false);
}
});
// Initialize
document.addEventListener('DOMContentLoaded', () => {
updateStatus('Ready to organize your tabs', 'default');
});