Skip to content

Commit 5cedb00

Browse files
authored
v2.5.4
2 parents ce950e5 + 8b2564b commit 5cedb00

File tree

8 files changed

+362
-211
lines changed

8 files changed

+362
-211
lines changed

src/components/sidebar/sidebar.html

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@
3535
</div>
3636
</div>
3737
<div id="sidebar-content">
38-
<hr />
39-
<button id="new-tab-button">+ New Tab <span>Ctrl+T</span></button>
38+
<div id="separator">
39+
<hr />
40+
<span id="clearbtn"><i class="fa-solid fa-arrow-down"></i>Clear</span>
41+
</div>
42+
<button id="new-tab-button">
43+
<i class="fa-regular fa-plus"></i> New Tab <span>Ctrl+T</span>
44+
</button>
4045
<ul id="tab-list"></ul>
4146
</div>
4247
</div>

src/components/sidebar/sidebar.js

Lines changed: 124 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,72 @@ browser.storage.local.get('favorites', function (result) {
5252
});
5353

5454
// Update sidebar when a tab changes
55-
browser.tabs.onUpdated.addListener((changeInfo) => {
56-
if (changeInfo.status === "complete") {
57-
initTabSidebarControl();
58-
}
55+
browser.tabs.onUpdated.addListener(() => {
56+
initTabSidebarControl();
57+
});
58+
59+
// Update sidebar when tabs are created, activated, closed
60+
browser.tabs.onCreated.addListener(() => {
61+
initTabSidebarControl();
5962
});
6063

61-
// Update sidebar when a tab is created
62-
browser.tabs.onCreated.addListener(function () {
64+
browser.tabs.onActivated.addListener(() => {
6365
initTabSidebarControl();
6466
});
6567

68+
browser.tabs.onRemoved.addListener((tabId) => {
69+
const index = base.findIndex((tab) => tab.id === tabId);
70+
if (index !== -1) {
71+
base.splice(index, 1);
72+
}
73+
initTabSidebarControl();
74+
})
75+
76+
// Sidebar clear button
77+
function clearTabs(tabs) {
78+
let newTabs = tabs
79+
tabs.forEach((tab) => {
80+
if (!tab.active && !tab.audible && !openedFavorites.includes(tab.id)) {
81+
browser.tabs.remove(tab.id)
82+
newTabs = tabs.filter((tab) => {
83+
if (false) return
84+
})
85+
}
86+
})
87+
if (newTabs.length == tabs.length) {
88+
tabs.forEach((tab) => {
89+
if (!tab.active && !openedFavorites.includes(tab.id)) {
90+
browser.tabs.remove(tab.id)
91+
newTabs = tabs.filter((tab) => {
92+
if (false) return
93+
})
94+
}
95+
})
96+
}
97+
if (newTabs.length == tabs.length) {
98+
tabs.forEach((tab) => {
99+
if (!openedFavorites.includes(tab.id)) {
100+
browser.tabs.remove(tab.id)
101+
newTabs = tabs.filter((tab) => {
102+
if (false) return
103+
})
104+
}
105+
})
106+
}
107+
}
108+
109+
function canClearTabs(tabs) {
110+
let newTabs = tabs.filter((tab) => {
111+
return !openedFavorites.includes(tab.id);
112+
});
113+
return (newTabs.length > 0)
114+
}
115+
document.querySelector('#separator span').addEventListener('click', () => {
116+
browser.tabs.query({ currentWindow: true }).then((tabs) => {
117+
clearTabs(tabs)
118+
})
119+
})
120+
66121
// Browser-control
67122
function handleBrowserControl(id) {
68123
browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => {
@@ -205,73 +260,76 @@ document.querySelector('button#b2').addEventListener("click", function () {
205260

206261
function newTab() {
207262
browser.tabs.create({});
208-
document.querySelectorAll('[aria-label="favopen"]')?.forEach((elemento) => {
209-
elemento.ariaLabel = "";
210-
})
211263
}
212264

213265
// Sidebar Code
214266
let base, draggedOver, dragging, activeTabId;
215267

216268
const init = (array) => {
217-
base = array.map((tab) => ({
218-
id: tab.id,
219-
title: tab.title,
220-
favIconUrl: tab.favIconUrl,
221-
}));
269+
base = array;
222270
renderItems(base);
223271
};
224272

225273
const renderItems = (data) => {
226274
tabList.innerHTML = '';
227275
data.forEach((tab) => {
228-
const node = document.createElement('li');
229-
node.draggable = true;
230-
node.dataset.tabId = tab.id;
231-
232-
const titleNode = document.createElement('div');
233-
titleNode.classList.add('tab-title');
234-
titleNode.textContent = tab.title;
235-
236-
let iconNode;
237-
if (tab.favIconUrl) {
238-
iconNode = document.createElement('img');
239-
iconNode.src = tab.favIconUrl;
240-
iconNode.alt = tab.title;
241-
} else {
242-
iconNode = document.createElement('i');
243-
iconNode.classList.add('fa', 'fa-solid', 'fa-globe');
244-
iconNode.setAttribute('aria-hidden', 'true');
245-
}
276+
if (!openedFavorites.includes(tab.id)) {
277+
const node = document.createElement('li');
278+
node.draggable = true;
279+
node.dataset.tabId = tab.id;
280+
281+
const titleNode = document.createElement('div');
282+
titleNode.classList.add('tab-title');
283+
titleNode.textContent = tab.title;
284+
285+
let iconNode;
286+
if (tab.favIconUrl) {
287+
iconNode = document.createElement('img');
288+
iconNode.src = tab.favIconUrl;
289+
iconNode.alt = tab.title;
290+
} else {
291+
iconNode = document.createElement('i');
292+
iconNode.classList.add('fa', 'fa-solid', 'fa-globe');
293+
iconNode.setAttribute('aria-hidden', 'true');
294+
}
295+
296+
const closeButton = document.createElement('button');
297+
closeButton.classList.add('close');
298+
closeButton.innerHTML = '&times;';
299+
closeButton.addEventListener('click', closeTab);
300+
301+
node.appendChild(iconNode);
246302

247-
const closeButton = document.createElement('button');
248-
closeButton.classList.add('close');
249-
closeButton.title = 'Close Tab';
250-
closeButton.innerHTML = '&times;';
251-
closeButton.addEventListener('click', closeTab);
252-
253-
node.appendChild(iconNode);
254-
node.appendChild(titleNode);
255-
node.appendChild(closeButton);
256-
257-
node.addEventListener('drag', setDragging);
258-
node.addEventListener('dragover', setDraggedOver);
259-
node.addEventListener('drop', compare);
260-
node.addEventListener('click', navigateToTab);
261-
node.addEventListener('auxclick', (event) => {
262-
if (event.button === 1) {
263-
closeTab(event);
303+
if (tab.audible) {
304+
const soundIcon = document.createElement('i');
305+
soundIcon.classList.add('fa', 'fa-light', 'fa-volume-high');
306+
soundIcon.setAttribute('aria-hidden', 'true');
307+
node.appendChild(soundIcon)
264308
}
265-
});
266309

267-
if (tab.id === activeTabId) {
268-
node.classList.add('active');
269-
}
310+
node.appendChild(titleNode);
311+
node.appendChild(closeButton);
270312

271-
if (!openedFavorites.includes(tab.id)) {
313+
node.addEventListener('drag', setDragging);
314+
node.addEventListener('dragover', setDraggedOver);
315+
node.addEventListener('drop', compare);
316+
node.addEventListener('click', navigateToTab);
317+
node.addEventListener('auxclick', (event) => {
318+
if (event.button === 1) {
319+
closeTab(event);
320+
}
321+
});
272322
tabList.appendChild(node);
273323
}
324+
325+
document.querySelector('.active')?.classList.remove('active');
326+
document.querySelector(`[data-tab-id="${activeTabId}"]`)?.classList.add('active');
274327
});
328+
if (canClearTabs(data)) {
329+
document.querySelector('#separator span').style.display = 'block'
330+
} else {
331+
document.querySelector('#separator span').style.display = 'none'
332+
}
275333
};
276334

277335
const compare = () => {
@@ -306,26 +364,12 @@ const closeTab = (e, middleclick = false) => {
306364
browser.tabs.remove(tabId);
307365
};
308366

309-
browser.tabs.onRemoved.addListener((tabId) => {
310-
const index = base.findIndex((tab) => tab.id === tabId);
311-
if (index !== -1) {
312-
base.splice(index, 1);
313-
renderItems(base);
314-
}
315-
initTabSidebarControl();
316-
updateSearchBar();
317-
})
318-
319367
const navigateToTab = (e) => {
320368
const tabId = parseInt(e.currentTarget.dataset.tabId);
321369
browser.tabs.update(tabId, { active: true, highlighted: false });
322370
updateSearchBar();
323371

324-
tabList.querySelector('.active')?.classList.remove('active');
325-
document.querySelector('[aria-label="favopen"]')?.setAttribute('aria-label', '');
326-
327372
activeTabId = tabId;
328-
tabList.querySelector(`[data-tab-id="${activeTabId}"]`)?.classList.add('active');
329373

330374
e.currentTarget.classList.add('current-tab');
331375
};
@@ -359,10 +403,10 @@ function favoriteDrop() {
359403
if (!tabtoPin.url.startsWith('about:')) {
360404
favoritesg[i] = { url: tabtoPin.url, favicon: tabtoPin.favIconUrl, id: i }
361405
openedFavorites[i] = tabtoPin.id
362-
renderItems(base);
363406
browser.storage.local.set({
364407
favorites: favoritesg
365408
});
409+
initTabSidebarControl();
366410
}
367411
})
368412
}
@@ -374,51 +418,44 @@ favoriteDiv.addEventListener('dragover', favoriteDragOver);
374418
favoriteDiv.addEventListener('drop', favoriteDrop);
375419

376420
function loadFavorites() {
421+
favoriteDiv.innerHTML = "";
377422
// Render
378423
favorites.forEach(async (favorite) => {
379424
if (favorite !== undefined) {
380425
const element = document.createElement('button')
381-
element.className = "favorite"
382-
element.dataset.url = favorite.url;
383426
if (openedFavorites[favorite.id] && (await browser.tabs.get(openedFavorites[favorite.id]))?.active) {
384-
element.ariaLabel = 'favopen';
427+
element.dataset.tabId = openedFavorites[favorite.id];
428+
element.classList.add('active')
385429
}
386430
element.onclick = async () => {
387431
if (!openedFavorites[favorite.id]) {
388-
const tabCreated = await browser.tabs.create({ url: element.dataset.url });
432+
const tabCreated = await browser.tabs.create({ url: favorite.url });
389433
openedFavorites[favorite.id] = tabCreated.id;
390434
} else {
391-
tabList.querySelector('.active')?.classList.remove('active');
392435
browser.tabs.update(openedFavorites[favorite.id], { active: true });
393436
}
394-
document.querySelectorAll('[aria-label="favopen"]')?.forEach((elemento) => {
395-
elemento.ariaLabel = "";
396-
})
397-
element.ariaLabel = "favopen";
398-
updateSearchBar();
437+
element.dataset.tabId = openedFavorites[favorite.id];
438+
element.classList.add('active')
399439
}
400-
element.onauxclick = async (event) => {
440+
element.onauxclick = (event) => {
401441
if (event.button == 1 && openedFavorites[favorite.id]) {
402442
// Unload favorite
403443
browser.tabs.remove(openedFavorites[favorite.id])
404444
openedFavorites[favorite.id] = undefined
405445
element.ariaLabel = ""
406446
} else if (event.button == 2) {
407447
// Remove favorite
408-
initTabSidebarControl();
409-
if (openedFavorites[favorite.id]) {
410-
delete openedFavorites[favorite.id];
411-
}
448+
delete openedFavorites[favorite.id];
412449
browser.storage.local.get('favorites', function (result) {
413450
var favoritesg = result.favorites;
414451
delete favoritesg[favorite.id]
415-
favoriteDiv.innerHTML = "";
416452
favorites = favoritesg
417453
loadFavorites();
418454
browser.storage.local.set({
419455
favorites: favoritesg
420456
});
421457
})
458+
initTabSidebarControl();
422459
}
423460
}
424461

@@ -429,8 +466,8 @@ function loadFavorites() {
429466

430467
favoriteDiv.appendChild(element);
431468
}
432-
updateSearchBar();
433469
});
470+
updateSearchBar();
434471
}
435472

436473
// Look for updates
@@ -440,7 +477,6 @@ browser.storage.onChanged.addListener(() => {
440477
if (JSON.stringify(favoritesg) !== JSON.stringify(favorites)) {
441478
favoritesg.forEach(fav => {
442479
if (fav?.url !== favorites[fav.id]?.url) {
443-
favoriteDiv.innerHTML = "";
444480
favorites = favoritesg
445481
loadFavorites();
446482
}
@@ -460,16 +496,7 @@ function initTabSidebarControl() {
460496
init(tabs);
461497
activeTabId = tabs.find((tab) => tab.active).id;
462498
renderItems(base);
463-
});
464-
465-
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
466-
const index = base.findIndex((t) => t.id === tabId);
467-
if (index !== -1) {
468-
base[index].title = tab.title;
469-
base[index].favIconUrl = tab.favIconUrl;
470-
renderItems(base);
471-
updateSearchBar();
472-
}
499+
updateSearchBar();
473500
});
474501
}
475502

0 commit comments

Comments
 (0)