-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
215 lines (186 loc) Β· 6.32 KB
/
popup.js
File metadata and controls
215 lines (186 loc) Β· 6.32 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
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
// Enhanced popup script for tab counter extension with tab list and duplicate detection
document.addEventListener("DOMContentLoaded", function () {
const tabCountEl = document.getElementById("tabCount");
const lastWindowTimeEl = document.getElementById("lastWindowTime");
const refreshBtn = document.getElementById("refreshBtn");
const showTabsBtn = document.getElementById("showTabsBtn");
const killDuplicatesBtn = document.getElementById("killDuplicatesBtn");
const taskManagerBtn = document.getElementById("taskManagerBtn");
const funFactEl = document.getElementById("funFact");
const tabListSection = document.getElementById("tabListSection");
const tabList = document.getElementById("tabList");
const closeTabListBtn = document.getElementById("closeTabListBtn");
// Update display
function updateDisplay() {
// Get tab info from background script
chrome.runtime.sendMessage({ action: "getTabInfo" }, (response) => {
if (response) {
tabCountEl.textContent = response.tabCount || 0;
// Format last window time
if (response.lastWindowOpenTime) {
const time = new Date(response.lastWindowOpenTime);
const timeStr = time.toLocaleTimeString([], {
hour: "2-digit",
minute: "2-digit",
});
lastWindowTimeEl.textContent = timeStr;
}
}
});
// Set random fun fact
setRandomFunFact();
}
// Set random fun fact
function setRandomFunFact() {
const funFacts = [
"π‘ Fun fact: Each tab is like a little sheep eating your RAM!",
"π Did you know? Chrome can use up to 1GB per tab!",
"π Pro tip: Close unused tabs to save memory!",
"β‘ Memory management is key to browser performance!",
"π Chrome Task Manager shows real memory usage!",
"β° Stale tabs consume memory even when inactive!",
"ποΈ Duplicate tabs are memory wasters!",
];
const randomFact =
funFacts[Math.floor(Math.random() * funFacts.length)];
funFactEl.textContent = randomFact;
}
// Show all tabs
function showAllTabs() {
chrome.runtime.sendMessage({ action: "getAllTabs" }, (response) => {
if (response && response.tabs) {
displayTabs(response.tabs);
tabListSection.style.display = "block";
}
});
}
// Display tabs in the list
function displayTabs(tabs) {
tabList.innerHTML = "";
// Sort tabs by staleness (most stale first)
tabs.sort((a, b) => b.staleness - a.staleness);
tabs.forEach((tab) => {
const tabElement = createTabElement(tab);
tabList.appendChild(tabElement);
});
}
// Create individual tab element
function createTabElement(tab) {
const tabDiv = document.createElement("div");
tabDiv.className = `tab-item ${tab.active ? "active" : ""}`;
// Determine staleness class
let stalenessClass = "fresh";
if (tab.staleness > 300) stalenessClass = "stale"; // 5 minutes
if (tab.staleness > 1800) stalenessClass = "very-stale"; // 30 minutes
tabDiv.innerHTML = `
<div class="tab-title" title="${tab.title}">${truncateText(tab.title, 40)}</div>
<div class="tab-url" title="${tab.url}">${truncateText(tab.url, 50)}</div>
<div class="tab-meta">
<span class="tab-staleness ${stalenessClass}">${tab.stalenessFormatted}</span>
<span class="tab-window">Window ${tab.windowId}</span>
</div>
<button class="close-tab-item-btn" data-tab-id="${tab.id}">β</button>
`;
// Add click handler to focus the tab
tabDiv.addEventListener("click", (event) => {
if (!event.target.classList.contains("close-tab-item-btn")) {
chrome.tabs.update(tab.id, { active: true });
showNotification(`π Switched to: ${tab.title}`);
}
});
// Add click handler for the close button
const closeButton = tabDiv.querySelector(".close-tab-item-btn");
closeButton.addEventListener("click", (event) => {
event.stopPropagation(); // Prevent the tabDiv click event from firing
chrome.tabs.remove(tab.id, () => {
showNotification(`ποΈ Closed tab: ${tab.title}`);
// Refresh the tab list after closing a tab
showAllTabs();
updateDisplay();
});
});
return tabDiv;
}
// Truncate text with ellipsis
function truncateText(text, maxLength) {
if (text.length <= maxLength) return text;
return text.substring(0, maxLength) + "...";
}
// Kill duplicate tabs
function killDuplicateTabs() {
showNotification("π Searching for duplicate tabs...");
chrome.runtime.sendMessage(
{ action: "closeDuplicateTabs" },
(response) => {
if (response && response.success) {
if (response.closedCount > 0) {
showNotification(
`ποΈ Closed ${response.closedCount} duplicate tabs!`
);
// Refresh the display
setTimeout(() => {
updateDisplay();
if (tabListSection.style.display !== "none") {
showAllTabs();
}
}, 1000);
} else {
showNotification("β
No duplicate tabs found!");
}
} else {
showNotification("β Error closing duplicate tabs");
}
}
);
}
// Show notification
function showNotification(message) {
// Create temporary notification element
const notification = document.createElement("div");
notification.className = "notification";
notification.textContent = message;
notification.style.cssText = `
position: fixed;
top: 10px;
right: 10px;
background: #4CAF50;
color: white;
padding: 10px 15px;
border-radius: 5px;
font-size: 12px;
z-index: 1000;
animation: slideIn 0.3s ease;
`;
document.body.appendChild(notification);
// Remove after 3 seconds
setTimeout(() => {
notification.remove();
}, 3000);
}
// Event listeners
refreshBtn.addEventListener("click", () => {
updateDisplay();
showNotification("π Refreshed!");
});
showTabsBtn.addEventListener("click", () => {
showAllTabs();
showNotification("π Loading all tabs...");
});
killDuplicatesBtn.addEventListener("click", () => {
killDuplicateTabs();
});
taskManagerBtn.addEventListener("click", () => {
// Open help page with Task Manager instructions
chrome.tabs.create({
url: chrome.runtime.getURL("task-manager-help.html"),
});
showNotification("βοΈ Opening Task Manager help...");
});
closeTabListBtn.addEventListener("click", () => {
tabListSection.style.display = "none";
});
// Initial load
updateDisplay();
// Update every 5 seconds
setInterval(updateDisplay, 5000);
});