-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
280 lines (232 loc) · 9.45 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
document.addEventListener("DOMContentLoaded", () => {
const searchInput = document.getElementById("searchInput");
const tabsList = document.getElementById("tabsList");
const closedTabsList = document.createElement("ul");
closedTabsList.id = "closedTabsList";
document.body.appendChild(closedTabsList);
let tabs = [];
let recentlyClosedTabs = [];
let currentIndex = 0;
let activeTabId = null;
let lastActiveTabId = null;
let hasInitialLoadCompleted = false;
const fetchTabs = async () => {
const activeTab = await browser.tabs.query({ active: true, currentWindow: true });
activeTabId = activeTab[0].id;
const allTabs = await browser.tabs.query({});
tabs = allTabs.filter(tab => tab.id !== activeTabId);
// Sort sound-playing and regular tabs
const soundPlayingTabs = tabs.filter(tab => tab.audible).sort((a, b) => b.lastAccessed - a.lastAccessed);
const nonSoundPlayingTabs = tabs.filter(tab => !tab.audible).sort((a, b) => b.lastAccessed - a.lastAccessed);
tabs = soundPlayingTabs.concat(nonSoundPlayingTabs);
// Identify the last active tab
const recentlyAccessedTabs = allTabs.filter(tab => tab.id !== activeTabId).sort((a, b) => b.lastAccessed - a.lastAccessed);
if (recentlyAccessedTabs.length > 0) {
lastActiveTabId = recentlyAccessedTabs[0].id;
}
displayTabs(tabs);
const closedSessions = await browser.sessions.getRecentlyClosed({ maxResults: 5 });
recentlyClosedTabs = closedSessions.filter(session => session.tab).map(session => session.tab);
displayClosedTabs(recentlyClosedTabs);
tabsList.scrollTop = 0;
searchInput.focus();
// Update selection after initial rendering
updateCombinedSelection();
};
const displayTabs = (tabsToDisplay) => {
tabsList.innerHTML = "";
// Separate sound-playing and regular tabs
const soundPlayingTabs = tabsToDisplay.filter(tab => tab.audible);
const regularTabs = tabsToDisplay.filter(tab => !tab.audible);
// Audio Tabs Section
if (soundPlayingTabs.length > 0) {
const audioHeading = document.createElement("li");
audioHeading.innerHTML = "<strong>Audio Tabs:</strong>";
audioHeading.classList.add("section-heading");
tabsList.appendChild(audioHeading);
soundPlayingTabs.forEach((tab) => {
const tabElement = document.createElement("li");
tabElement.classList.add("tab-item");
tabElement.setAttribute("data-id", tab.id);
const titleText = document.createTextNode(tab.title);
const audioIcon = document.createElement("span");
audioIcon.textContent = " 🔊";
audioIcon.classList.add("audio-icon");
tabElement.appendChild(audioIcon);
tabElement.appendChild(titleText);
tabElement.onclick = () => {
browser.tabs.update(tab.id, { active: true });
window.close();
};
tabsList.appendChild(tabElement);
});
}
// Open Tabs Section
if (regularTabs.length > 0) {
const openTabsHeading = document.createElement("li");
openTabsHeading.innerHTML = "<strong>Open Tabs:</strong>";
openTabsHeading.classList.add("section-heading");
tabsList.appendChild(openTabsHeading);
regularTabs.forEach((tab) => {
const tabElement = document.createElement("li");
tabElement.classList.add("tab-item");
tabElement.setAttribute("data-id", tab.id);
const titleText = document.createTextNode(tab.title);
tabElement.appendChild(titleText);
tabElement.onclick = () => {
browser.tabs.update(tab.id, { active: true });
window.close();
};
tabsList.appendChild(tabElement);
});
}
};
const displayClosedTabs = (closedTabs) => {
closedTabsList.innerHTML = ""; // Clear previous results
if (closedTabs.length > 0) {
const header = document.createElement("li");
header.innerHTML = "<strong>Recently Closed Tabs:</strong>";
header.classList.add("section-heading");
closedTabsList.appendChild(header);
closedTabs.forEach((tab) => {
const tabElement = document.createElement("li");
tabElement.textContent = tab.title;
tabElement.classList.add("closed-tab-item");
tabElement.onclick = () => {
browser.sessions.restore(tab.sessionId);
window.close();
};
closedTabsList.appendChild(tabElement);
});
}
};
const displayDuckDuckGoResults = (results) => {
closedTabsList.innerHTML = ""; // Clear previous results
if (results.length > 0) {
const header = document.createElement("li");
header.innerHTML = "<strong>DuckDuckGo Results:</strong>";
header.classList.add("section-heading");
closedTabsList.appendChild(header);
results.forEach((result) => {
if (result.Text && result.FirstURL) {
const resultElement = document.createElement("li");
resultElement.textContent = result.Text;
resultElement.classList.add("online-result");
resultElement.setAttribute("data-url", result.FirstURL);
resultElement.onclick = () => {
browser.tabs.create({ url: result.FirstURL });
window.close();
};
closedTabsList.appendChild(resultElement);
}
});
}
};
const updateCombinedSelection = () => {
const allItems = [...document.querySelectorAll(".tab-item, .closed-tab-item, .history-item, .online-result")];
currentIndex = allItems.findIndex(item => item.getAttribute("data-id") === String(lastActiveTabId));
if (currentIndex === -1) currentIndex = 0;
updateSelection(allItems);
};
const updateSelection = (allItems) => {
allItems.forEach((item, index) => {
if (index === currentIndex) {
item.classList.add("selected");
if (hasInitialLoadCompleted) {
item.scrollIntoView({ block: "nearest" });
}
} else {
item.classList.remove("selected");
}
});
hasInitialLoadCompleted = true;
};
searchInput.addEventListener("input", async () => {
const query = searchInput.value.toLowerCase();
const filteredTabs = tabs.filter(tab =>
tab.title.toLowerCase().includes(query) || tab.url.toLowerCase().includes(query)
);
const filteredClosedTabs = recentlyClosedTabs.filter(tab =>
tab.title.toLowerCase().includes(query) || tab.url.toLowerCase().includes(query)
);
displayTabs(filteredTabs);
displayClosedTabs(filteredClosedTabs);
if (filteredTabs.length === 0 && filteredClosedTabs.length === 0) {
await searchHistory(query);
} else {
updateCombinedSelection();
}
});
const displayHistoryResults = (historyItems) => {
closedTabsList.innerHTML = "";
if (historyItems.length > 0) {
const header = document.createElement("li");
header.innerHTML = "<strong>History Results:</strong>";
closedTabsList.appendChild(header);
historyItems.forEach((item) => {
const historyElement = document.createElement("li");
historyElement.textContent = item.title;
historyElement.classList.add("history-item");
historyElement.setAttribute("data-url", item.url);
historyElement.onclick = () => {
browser.tabs.create({ url: item.url });
window.close();
};
closedTabsList.appendChild(historyElement);
});
updateCombinedSelection();
}
};
const searchHistory = async (query) => {
const daysToSearch = await browser.storage.local.get("daysToSearch").then(result => result.daysToSearch || 1);
const startTime = Date.now() - daysToSearch * 24 * 60 * 60 * 1000;
const historyItems = await browser.history.search({
text: query,
startTime: startTime,
maxResults: 5,
});
if (historyItems.length === 0) {
await searchDuckDuckGo(query);
} else {
displayHistoryResults(historyItems);
}
};
const searchDuckDuckGo = async (query) => {
try {
const response = await fetch(`https://api.duckduckgo.com/?q=${encodeURIComponent(query)}&format=json&no_redirect=1`);
const data = await response.json();
displayDuckDuckGoResults(data.RelatedTopics);
} catch (error) {
console.error("Error fetching DuckDuckGo results:", error);
}
};
document.addEventListener("keydown", (event) => {
const allItems = document.querySelectorAll(".tab-item, .closed-tab-item, .history-item, .online-result");
if (event.key === "ArrowDown") {
event.preventDefault();
currentIndex = (currentIndex + 1) % allItems.length;
updateSelection(allItems);
}
if (event.key === "ArrowUp") {
event.preventDefault();
currentIndex = (currentIndex - 1 + allItems.length) % allItems.length;
updateSelection(allItems);
}
if (event.key === "Enter" && currentIndex >= 0) {
event.preventDefault();
const selectedItem = allItems[currentIndex];
if (selectedItem.classList.contains("tab-item")) {
const tabId = tabs[currentIndex].id;
browser.tabs.update(tabId, { active: true });
} else if (selectedItem.classList.contains("closed-tab-item")) {
const tabSessionId = recentlyClosedTabs[currentIndex - tabs.length].sessionId;
browser.sessions.restore(tabSessionId);
} else if (selectedItem.classList.contains("history-item") || selectedItem.classList.contains("online-result")) {
const url = selectedItem.getAttribute("data-url");
browser.tabs.create({ url: url });
}
window.close();
}
});
fetchTabs();
});