-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpopup.js
More file actions
1193 lines (1089 loc) · 47.2 KB
/
popup.js
File metadata and controls
1193 lines (1089 loc) · 47.2 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
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
document.addEventListener('DOMContentLoaded', init);
// Per-tab memory estimate. Must match MB_PER_TAB in background.js. If you
// retune one, retune both: the popup uses it for stats display, the
// background uses it for toast counts and the forecast number.
var MB_PER_TAB = 150;
var _loaded = false;
var _toastTimer = null;
var _allTabs = null;
var _tabListSig = '';
var _sessionListSig = '';
var _renderedSessionIds = null;
var _pollTimer = null;
function t(key, subs) {
var val = chrome.i18n.getMessage(key, subs);
return val || key;
}
var BADGES = {
'Active tab': { icon: 'eye', type: 'active', labelKey: 'badgeActive' },
'System page': { icon: 'lock', type: 'system', labelKey: 'badgeSystem' },
'Pinned': { icon: 'pin', type: 'pinned', labelKey: 'badgePinned' },
'Audio': { icon: 'volume2', type: 'audio', labelKey: 'badgeAudio' },
'Whitelisted': { icon: 'shield', type: 'starred', labelKey: 'badgeWhitelisted' },
};
async function init() {
try {
// Apply persisted section states synchronously before any await so the
// browser paints the final open/closed state instead of transitioning
// from closed to open when localStorage restore lands mid-init.
restoreSectionStates();
localizeHtml();
await initTheme();
injectIcons();
await loadAll();
attachListeners();
await checkReviewPrompt();
// Poll for tab changes instead of registering persistent chrome.tabs listeners
// (popup context is short-lived; persistent listeners leak across reopens)
if (_pollTimer) clearInterval(_pollTimer);
_pollTimer = setInterval(function() { loadAll(); }, 5000);
// Pause polling when sidepanel is hidden, resume when visible
document.addEventListener('visibilitychange', function() {
if (document.visibilityState === 'hidden') {
clearInterval(_pollTimer);
_pollTimer = null;
} else if (document.visibilityState === 'visible' && !_pollTimer) {
loadAll();
_pollTimer = setInterval(function() { loadAll(); }, 5000);
}
});
} finally {
document.body.classList.add('popup-ready');
}
}
function filterTabs(tabs) {
if (!tabs) return tabs;
var input = document.getElementById('tabSearchInput');
if (!input || !input.value.trim()) return tabs;
var q = input.value.trim().toLowerCase();
return tabs.filter(function(tab) {
return (tab.title && tab.title.toLowerCase().includes(q)) || (tab.url && tab.url.toLowerCase().includes(q));
});
}
function localizeHtml() {
document.querySelectorAll('[data-i18n]').forEach(function(el) {
var msg = chrome.i18n.getMessage(el.getAttribute('data-i18n'));
if (msg) el.textContent = msg;
});
document.querySelectorAll('[data-i18n-placeholder]').forEach(function(el) {
var msg = chrome.i18n.getMessage(el.getAttribute('data-i18n-placeholder'));
if (msg) el.placeholder = msg;
});
document.querySelectorAll('[data-i18n-title]').forEach(function(el) {
var msg = chrome.i18n.getMessage(el.getAttribute('data-i18n-title'));
if (msg) el.title = msg;
});
document.querySelectorAll('[data-i18n-aria]').forEach(function(el) {
var msg = chrome.i18n.getMessage(el.getAttribute('data-i18n-aria'));
if (msg) el.setAttribute('aria-label', msg);
});
// Set version in footer dynamically
var footer = document.querySelector('.popup-footer .footer-version');
if (footer) footer.textContent = 'Drowzy v' + chrome.runtime.getManifest().version;
// Set lang attribute to match browser locale
document.documentElement.lang = chrome.i18n.getUILanguage();
}
async function initTheme() {
var res = await chrome.storage.local.get('theme');
if (res.theme) {
document.documentElement.setAttribute('data-theme', res.theme);
} else {
var dark = window.matchMedia('(prefers-color-scheme: dark)').matches;
document.documentElement.setAttribute('data-theme', dark ? 'dark' : 'light');
}
updateThemeIcon();
}
function toggleTheme() {
var cur = document.documentElement.getAttribute('data-theme');
var next = cur === 'dark' ? 'light' : 'dark';
document.documentElement.setAttribute('data-theme', next);
chrome.storage.local.set({ theme: next });
updateThemeIcon();
var btn = document.getElementById('themeToggle');
if (!btn) return;
btn.classList.remove('theme-icon-enter');
void btn.offsetWidth;
btn.classList.add('theme-icon-enter');
}
function updateThemeIcon() {
var btn = document.getElementById('themeToggle');
if (!btn) return;
var dark = document.documentElement.getAttribute('data-theme') === 'dark';
btn.innerHTML = dark ? icon('sunMedium', 16) : icon('moon', 16);
var label = dark ? t('switchToLight') : t('switchToDark');
btn.title = label;
btn.setAttribute('aria-label', label);
}
function injectIcons() {
document.querySelectorAll('[data-icon]').forEach(function(el) {
el.innerHTML = icon(el.dataset.icon, parseInt(el.dataset.iconSize || '14', 10));
});
}
async function loadAll() {
try {
var [status, tabList, settings, sessions, stats] = await Promise.all([
msg({ action: 'getStatus' }),
msg({ action: 'getTabList' }),
msg({ action: 'getSettings' }),
msg({ action: 'getSessions' }),
msg({ action: 'getStats' })
]);
_allTabs = tabList;
var scrollEl = document.querySelector('.main-content');
// In sidepanel, .main-content doesn't scroll (body does) — detect correct container
if (!scrollEl || scrollEl.scrollHeight <= scrollEl.clientHeight) {
scrollEl = document.documentElement;
}
var savedScroll = scrollEl.scrollTop;
renderStatsStrip(status);
renderTabList(filterTabs(tabList));
renderCurrentTab(tabList, settings);
renderSettings(settings);
renderSessions(sessions);
renderLifetimeStats(stats);
// Re-poll shortcut bindings — user may have edited them in
// chrome://extensions/shortcuts via the Customize link without closing
// the popup. Cheap call; runs on the same 5s cadence as everything else.
renderShortcutsStatus();
if (scrollEl) scrollEl.scrollTop = savedScroll;
_loaded = true;
} catch (e) {
var list = document.getElementById('tabList');
if (list) list.innerHTML = '<div class="tab-list-empty"><span class="empty-icon">' + icon('x', 22) + '</span><span>' + esc(t('failedToLoad')) + '</span></div>';
// Reset sig so a successful recovery render is not skipped by dedupe
_tabListSig = '';
}
}
async function msg(data) {
try { return await chrome.runtime.sendMessage(data); }
catch { return null; }
}
function animateStat(el, val) {
var text = String(val);
if (_loaded && el.textContent !== text) {
el.textContent = text;
el.classList.remove('stat-updated');
void el.offsetWidth;
el.classList.add('stat-updated');
} else {
el.textContent = text;
}
}
function renderStatsStrip(status) {
if (!status) return;
animateStat(document.getElementById('statSleepingValue'), status.suspendedCount);
animateStat(document.getElementById('statProtectedValue'), status.protectedCount);
// The third strip slot adapts based on state to avoid a 4-item strip:
// - if Drowzy has actually freed memory, show that ("saved", blue dot)
// - else if there are eligible tabs, show the forecast ("available", amber dot via .strip-stat-forecast)
// - else show an em-dash with the default "saved" label
// Visual + label both differ between saved and forecast so the forecast
// doesn't read as already-realized savings.
var memItem = document.getElementById('statMemoryItem');
var memLabel = document.getElementById('statMemoryLabel');
var memValue = document.getElementById('statMemoryValue');
if (status.estimatedMbSaved > 0) {
memItem.classList.remove('strip-stat-forecast');
memItem.title = t('ramEstimateTooltip');
memLabel.textContent = t('statSaved');
animateStat(memValue, fmtRam(status.estimatedMbSaved));
} else if (status.eligibleCount && status.estimatedMbForecast) {
memItem.classList.add('strip-stat-forecast');
memItem.title = t('ramForecastTooltip');
memLabel.textContent = t('statForecast');
animateStat(memValue, fmtRam(status.estimatedMbForecast));
} else {
memItem.classList.remove('strip-stat-forecast');
memItem.title = t('ramEstimateTooltip');
memLabel.textContent = t('statSaved');
animateStat(memValue, '\u2014');
}
// Disable quick-action buttons when there's nothing to act on, with an
// explanatory tooltip \u2014 prevents the "click does nothing" feel that makes
// a new user think the extension is broken.
var btnSusp = document.getElementById('btnSuspendOthers');
if (btnSusp) {
var noEligible = !status.eligibleCount;
btnSusp.disabled = noEligible;
btnSusp.title = noEligible ? t('noOthersToSuspend') : '';
}
var btnWake = document.getElementById('btnWakeAll');
if (btnWake) {
var noSleeping = !status.suspendedCount;
btnWake.disabled = noSleeping;
btnWake.title = noSleeping ? t('noSuspendedToWake') : '';
}
}
function tabListSignature(tabs) {
if (!tabs) return '';
var parts = [];
for (var i = 0; i < tabs.length; i++) {
// Don't shadow the outer `t()` i18n helper
var tab = tabs[i];
parts.push(tab.id + '|' + tab.status + '|' + (tab.title || '') + '|' + (tab.timeLeft == null ? '' : tab.timeLeft) + '|' + (tab.protectReason || '') + '|' + (tab.favIconUrl || ''));
}
return parts.join('\u0001');
}
function renderTabList(tabs) {
var container = document.getElementById('tabList');
if (!tabs || !tabs.length) {
var emptySig = '__empty__';
if (_tabListSig === emptySig) return;
_tabListSig = emptySig;
container.innerHTML = '<div class="tab-list-empty"><span class="empty-icon">' + icon('search', 22) + '</span><span>' + esc(t('noTabsFound')) + '</span></div>';
return;
}
var sig = tabListSignature(tabs);
if (sig === _tabListSig) return;
var firstRender = _tabListSig === '';
_tabListSig = sig;
container.innerHTML = '';
for (var i = 0; i < tabs.length; i++) {
var tab = tabs[i];
var el = document.createElement('div');
el.className = 'tab-item' + (tab.status === 'suspended' ? ' sleeping' : '') + (firstRender ? '' : ' no-anim');
el.setAttribute('role', 'button');
el.setAttribute('tabindex', '0');
if (firstRender) el.style.setProperty('--i', Math.min(i, 10));
el.appendChild(buildFavicon(tab));
var title = document.createElement('span');
title.className = 'tab-title';
title.textContent = tab.title;
title.title = tab.title;
el.appendChild(title);
var meta = document.createElement('span');
meta.className = 'tab-meta';
if (tab.status === 'suspended') {
meta.innerHTML = '<span class="badge badge-sleeping">' + icon('moon', 11) + ' ' + esc(t('badgeZzz')) + '</span>';
} else if (tab.status === 'protected' || tab.status === 'active') {
var info = BADGES[tab.protectReason] || { icon: 'shield', type: 'system', labelKey: 'badgeProtected' };
meta.innerHTML = '<span class="badge badge-' + info.type + '" title="' + esc(t(info.labelKey)) + '">' + icon(info.icon, 11) + ' ' + esc(t(info.labelKey)) + '</span>';
} else if (tab.status === 'idle' && tab.timeLeft !== null) {
var timeText = tab.timeLeft > 0 ? esc(String(tab.timeLeft)) + 'm' : esc(t('suspendingSoon'));
meta.innerHTML = '<span class="badge badge-timer">' + icon('clock', 11) + ' ' + timeText + '</span>';
}
if (tab.status === 'idle') {
var suspendBtn = document.createElement('button');
suspendBtn.className = 'tab-suspend-btn';
suspendBtn.innerHTML = icon('pause', 11);
suspendBtn.title = t('ctxSuspendThis');
suspendBtn.setAttribute('aria-label', t('ctxSuspendThis'));
(function(tid) {
suspendBtn.addEventListener('click', async function(e) {
e.stopPropagation();
await msg({ action: 'suspendTab', tabId: tid });
await loadAll();
});
})(tab.id);
el.appendChild(meta);
el.appendChild(suspendBtn);
} else {
el.appendChild(meta);
}
var tabAction = (function(tabData) {
return async function() {
if (tabData.status === 'suspended') {
await msg({ action: 'wakeTab', tabId: tabData.id });
}
// Activate the tab and focus its window. Without the window focus,
// clicking a tab that lives in a non-focused Chrome window updates
// its active state but leaves the user looking at the popup's window
// — the tab they asked for is "selected" somewhere they can't see.
try {
var updated = await chrome.tabs.update(tabData.id, { active: true });
if (updated && updated.windowId != null) {
try { await chrome.windows.update(updated.windowId, { focused: true }); } catch {}
}
} catch {}
if (tabData.status === 'suspended') await loadAll();
};
})(tab);
el.addEventListener('click', tabAction);
(function(action) {
el.addEventListener('keydown', function(e) {
if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); action(); }
});
})(tabAction);
container.appendChild(el);
}
}
function buildFavicon(tab) {
if (tab.favIconUrl && !tab.favIconUrl.startsWith('chrome://')) {
var img = document.createElement('img');
img.className = 'favicon';
img.src = tab.favIconUrl;
img.alt = '';
img.onerror = function() { img.replaceWith(faviconFallback(tab.url)); };
return img;
}
return faviconFallback(tab.url);
}
function faviconFallback(url) {
var span = document.createElement('span');
span.className = 'favicon-fallback';
try {
span.textContent = new URL(url).hostname.replace(/^www\./, '').charAt(0).toUpperCase();
} catch { span.textContent = '?'; }
return span;
}
function esc(str) {
return str.replace(/&/g, '&').replace(/"/g, '"').replace(/'/g, ''').replace(/</g, '<').replace(/>/g, '>');
}
function renderCurrentTab(tabList, settings) {
if (!tabList || !tabList.length || !settings) return;
var active = tabList.find(function(tab) { return tab.status === 'active'; });
if (!active) return;
var domain = '\u2014';
var whitelisted = false;
var urlObj = null;
try {
urlObj = new URL(active.url);
domain = urlObj.hostname;
if (urlObj.protocol === 'chrome-extension:') {
domain = active.title || t('extensionPage');
}
var clean = domain.replace(/^www\./, '').toLowerCase();
var fullUrl = (urlObj.hostname + urlObj.pathname).toLowerCase().replace(/^www\./, '');
whitelisted = (settings.whitelist || []).some(function(w) {
w = w.toLowerCase();
if (w.includes('/')) {
var escaped = w.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').replace(/\\\*/g, '.*');
try { return new RegExp('^' + escaped + '(/.*)?$').test(fullUrl); } catch { return false; }
}
return clean === w || clean.endsWith('.' + w);
});
} catch {}
var isInternal = true;
try {
var proto = (urlObj || new URL(active.url)).protocol;
isInternal = proto !== 'http:' && proto !== 'https:';
} catch {}
// Pick the status line based on why this tab is/isn't suspendable. Active
// is the fallback; the more specific reasons take priority since a pinned
// active tab is "active AND pinned" — the pin is the actually informative bit.
var statusKey = 'activeWontSuspend';
if (isInternal) statusKey = 'systemPageCantSuspend';
else if (settings.protectPinned && active.pinned) statusKey = 'pinnedWontSuspend';
else if (settings.protectAudio && active.audible) statusKey = 'audioWontSuspend';
else if (whitelisted) statusKey = 'whitelistedWontSuspend';
// Signature-dedupe: the polling loop runs every 5s; skip DOM writes when
// nothing changed. Prevents the favicon/domain flicker from re-setting
// img.src and retriggering onerror on every poll.
var section = document.getElementById('currentTabSection');
var sig = (active.url || '') + '|' + (active.favIconUrl || '') + '|' + domain + '|' + (whitelisted ? 1 : 0) + '|' + (isInternal ? 1 : 0) + '|' + statusKey;
if (section && section.dataset.sig === sig) return;
if (section) section.dataset.sig = sig;
document.getElementById('currentTabDomain').textContent = domain;
var fav = document.getElementById('currentTabFavicon');
if (active.favIconUrl) {
if (fav.getAttribute('src') !== active.favIconUrl) {
fav.style.display = '';
fav.onerror = function() { fav.style.display = 'none'; };
fav.src = active.favIconUrl;
} else if (fav.style.display === 'none' && fav.complete && fav.naturalWidth > 0) {
fav.style.display = '';
}
} else {
fav.style.display = 'none';
fav.removeAttribute('src');
}
document.getElementById('currentTabStatus').textContent = t(statusKey);
var btn = document.getElementById('btnToggleWhitelist');
btn.style.display = isInternal ? 'none' : '';
var btnText = btn.querySelector('.btn-text');
var btnIcon = btn.querySelector('.btn-icon');
if (whitelisted) {
if (btnText) btnText.textContent = t('siteWhitelisted');
if (btnIcon) btnIcon.innerHTML = icon('shieldCheck', 14);
btn.classList.add('is-whitelisted');
} else {
if (btnText) btnText.textContent = t('neverSuspendSite');
if (btnIcon) btnIcon.innerHTML = icon('shield', 14);
btn.classList.remove('is-whitelisted');
}
btn.dataset.domain = domain.replace(/^www\./, '').toLowerCase();
btn.dataset.whitelisted = whitelisted ? '1' : '0';
}
function sessionListSignature(sessions) {
if (!sessions || !sessions.length) return '__empty__';
var parts = [];
for (var i = 0; i < sessions.length; i++) {
var s = sessions[i];
parts.push(s.id + '|' + (s.name || '') + '|' + (s.tabs ? s.tabs.length : 0) + '|' + (s.createdAt || 0));
}
return parts.join('\u0001');
}
function renderSessions(sessions) {
var container = document.getElementById('sessionList');
var sig = sessionListSignature(sessions);
if (sig === _sessionListSig) return;
_sessionListSig = sig;
if (!sessions || !sessions.length) {
container.innerHTML = '<div class="empty-state"><span class="empty-icon">' + icon('folderOpen', 20) + '</span><span>' + esc(t('emptySessionState')) + '</span></div>';
_renderedSessionIds = new Set();
return;
}
// Track which session IDs were already on screen so we can apply a fresh
// entrance animation only to genuinely new ones, not re-animate existing.
var prevIds = _renderedSessionIds;
var firstRender = prevIds === null;
var currentIds = new Set();
for (var n = 0; n < sessions.length; n++) currentIds.add(sessions[n].id);
container.innerHTML = '';
for (var i = 0; i < sessions.length; i++) {
var s = sessions[i];
var card = document.createElement('div');
card.className = 'session-card';
card.style.setProperty('--i', i);
if (!firstRender && prevIds && !prevIds.has(s.id)) {
card.classList.add('is-new');
}
var stack = document.createElement('div');
stack.className = 'session-favicon-stack';
var icons = (s.tabs || []).slice(0, 4);
for (var j = 0; j < icons.length; j++) {
if (icons[j].favIconUrl) {
var fi = document.createElement('img');
fi.src = icons[j].favIconUrl;
fi.alt = '';
fi.width = 16;
fi.height = 16;
fi.onerror = function() { this.style.display = 'none'; };
stack.appendChild(fi);
}
}
var info = document.createElement('div');
info.className = 'session-info';
info.innerHTML = '<div class="session-name" title="' + esc(s.name || '') + '">' + esc(s.name || '') + '</div>' +
'<div class="session-meta">' + t('tabsCount', [String((s.tabs || []).length)]) + ' \u00B7 ' + relativeTime(s.createdAt) + '</div>';
var actions = document.createElement('div');
actions.className = 'session-actions';
var restoreBtn = document.createElement('button');
restoreBtn.className = 'btn btn-sm';
restoreBtn.innerHTML = icon('download', 12) + ' ' + esc(t('open'));
restoreBtn.title = t('openSessionTitle');
var deleteBtn = document.createElement('button');
deleteBtn.className = 'btn btn-sm btn-ghost-danger';
deleteBtn.innerHTML = icon('trash2', 13);
deleteBtn.title = t('deleteSessionTitle');
var replaceBtn = document.createElement('button');
replaceBtn.className = 'btn btn-sm';
replaceBtn.innerHTML = icon('download', 12) + ' ' + esc(t('replace'));
replaceBtn.title = t('replaceSessionTitle');
(function(session, rBtn, rpBtn, dBtn) {
function disableAll() { rBtn.disabled = true; rpBtn.disabled = true; dBtn.disabled = true; }
function enableAll() { rBtn.disabled = false; rpBtn.disabled = false; dBtn.disabled = false; }
rBtn.addEventListener('click', async function(e) {
e.stopPropagation();
disableAll();
rBtn.textContent = t('loading');
var res = await msg({ action: 'restoreSession', id: session.id, mode: 'open' });
if (res && res.success) showToast(t('restoredTabs', [String(res.count)]));
else showToast((res && res.error) || t('failedToRestore'));
enableAll();
rBtn.innerHTML = icon('download', 12) + ' ' + esc(t('open'));
});
rpBtn.addEventListener('click', async function(e) {
e.stopPropagation();
disableAll();
rpBtn.textContent = t('loading');
var res = await msg({ action: 'restoreSession', id: session.id, mode: 'replace' });
if (res && res.success) showToast(t('restoredTabs', [String(res.count)]));
else showToast((res && res.error) || t('failedToRestore'));
enableAll();
rpBtn.innerHTML = icon('download', 12) + ' ' + esc(t('replace'));
});
})(s, restoreBtn, replaceBtn, deleteBtn);
(function(session, dBtn, sessionCard) {
var confirmTimer = null;
function reset() {
dBtn.dataset.confirming = '';
dBtn.classList.remove('confirming');
dBtn.innerHTML = icon('trash2', 13);
dBtn.title = t('deleteSessionTitle');
}
dBtn.addEventListener('click', async function(e) {
e.stopPropagation();
if (dBtn.dataset.confirming) {
clearTimeout(confirmTimer);
sessionCard.classList.add('is-removing');
// Wait for the leave animation before actually deleting + reloading
setTimeout(async function() {
await msg({ action: 'deleteSession', id: session.id });
await loadAll();
}, 220);
} else {
dBtn.dataset.confirming = '1';
dBtn.classList.add('confirming');
dBtn.innerHTML = icon('check', 13);
dBtn.title = t('confirmDeleteTitle');
confirmTimer = setTimeout(reset, 3000);
}
});
})(s, deleteBtn, card);
actions.appendChild(restoreBtn);
actions.appendChild(replaceBtn);
actions.appendChild(deleteBtn);
card.appendChild(stack);
card.appendChild(info);
card.appendChild(actions);
container.appendChild(card);
}
_renderedSessionIds = currentIds;
}
function renderLifetimeStats(stats) {
if (!stats) return;
animateStat(document.getElementById('statToday'), stats.totalTabsSuspendedToday || 0);
animateStat(document.getElementById('statAllTime'), stats.totalTabsSuspended || 0);
var ram = (stats.totalTabsSuspended || 0) * MB_PER_TAB;
animateStat(document.getElementById('statRamAllTime'), ram ? fmtRam(ram) : '\u2014');
if (stats.installDate) {
var d = new Date(stats.installDate).toLocaleDateString(undefined, { month: 'short', day: 'numeric', year: 'numeric' });
document.getElementById('statMemberSince').textContent = t('statMemberSince') + ' ' + d;
}
}
function renderSettings(settings) {
if (!settings) return;
// Skip updating form controls when the user is actively interacting with settings
var active = document.activeElement;
var timer = document.getElementById('suspendTimer');
if (active !== timer) {
timer.value = settings.enableAutoSuspend ? String(settings.suspendAfterMinutes) : '0';
}
var toggles = [
['togglePinned', 'protectPinned'], ['toggleAudio', 'protectAudio'],
['toggleForms', 'protectForms'], ['toggleStartup', 'suspendOnStartup'],
['toggleAutoSuspend', 'enableAutoSuspend'], ['toggleMarkSuspended', 'markSuspendedTabs']
];
for (var i = 0; i < toggles.length; i++) {
var el = document.getElementById(toggles[i][0]);
if (el && active !== el) el.checked = settings[toggles[i][1]];
}
renderWhitelist(settings.whitelist);
}
var _whitelistSig = '';
function renderWhitelist(list) {
var el = document.getElementById('whitelistList');
// Keep the count badge in sync even when the sig dedupe early-returns.
var countEl = document.getElementById('whitelistCount');
if (countEl) countEl.textContent = (list && list.length) ? '(' + list.length + ')' : '';
var sig = list && list.length ? list.join('\u0001') : '__empty__';
if (sig === _whitelistSig) return;
_whitelistSig = sig;
el.innerHTML = '';
if (!list || !list.length) {
el.innerHTML = '<li class="empty-whitelist"><span class="empty-icon">' + icon('shield', 16) + '</span><span>' + esc(t('noSitesWhitelisted')) + '</span></li>';
return;
}
for (var i = 0; i < list.length; i++) {
var li = document.createElement('li');
li.className = 'whitelist-item';
var span = document.createElement('span');
span.className = 'whitelist-domain';
span.textContent = list[i];
span.title = list[i];
var rm = document.createElement('button');
rm.type = 'button';
rm.className = 'whitelist-remove';
// Plain text label — the previous icon-only X was easy to miss.
// The trailing × glyph is a literal character (not an SVG) so it always
// renders even if the icons.js bundle hasn't loaded yet.
rm.innerHTML = '<span class="whitelist-remove-text">' + esc(t('remove')) + '</span><span class="whitelist-remove-x" aria-hidden="true">×</span>';
rm.title = t('remove');
rm.setAttribute('aria-label', t('remove') + ': ' + list[i]);
(function(domain, listItem) {
rm.addEventListener('click', function(e) {
e.stopPropagation();
e.preventDefault();
// Optimistic removal — fade the row out immediately so the click
// feels instant. The polling timer can race and refresh
// _whitelistSig to the post-removal shape before our own loadAll
// runs, in which case renderWhitelist's sig dedupe would
// early-return and leave the stale DOM untouched. Resetting the
// sig forces the next render to execute.
listItem.style.transition = 'opacity 140ms ease-out';
listItem.style.opacity = '0';
setTimeout(function() {
if (listItem.parentNode) listItem.parentNode.removeChild(listItem);
}, 150);
_whitelistSig = '';
msg({ action: 'removeWhitelist', domain: domain }).then(function() { loadAll(); });
});
})(list[i], li);
li.appendChild(span);
li.appendChild(rm);
el.appendChild(li);
}
}
function attachListeners() {
document.getElementById('themeToggle').addEventListener('click', toggleTheme);
document.getElementById('btnSuspendOthers').addEventListener('click', async function() {
if (this.disabled) return;
var text = this.querySelector('.btn-text');
var origLabel = t('suspendOthers');
text.textContent = t('suspending');
this.disabled = true;
var res = await msg({ action: 'suspendOthers' });
var count = (res && res.count) || 0;
if (count > 0) {
showToast(t('suspendedToast', [String(count), fmtRam(res.mbFreed || count * MB_PER_TAB)]));
} else {
showToast(t('noOthersToSuspend'));
}
text.textContent = origLabel;
this.disabled = false;
await loadAll();
});
document.getElementById('btnWakeAll').addEventListener('click', async function() {
if (this.disabled) return;
var text = this.querySelector('.btn-text');
var origLabel = t('wakeAll');
text.textContent = t('waking');
this.disabled = true;
var res = await msg({ action: 'unsuspendAll' });
var count = (res && res.count) || 0;
if (count > 0) {
showToast(t('wokeToast', [String(count)]));
} else {
showToast(t('noSuspendedToWake'));
}
text.textContent = origLabel;
this.disabled = false;
await loadAll();
});
document.getElementById('btnToggleWhitelist').addEventListener('click', async function() {
if (this.disabled) return;
var d = this.dataset.domain;
if (!d || d === '\u2014') return;
// Match background.addWhitelist's accept set: registrable domains, plus
// localhost and bare IP literals (IPv4 + IPv6 loopback). Without this the
// toggle silently no-ops on http://[::1]/ and similar local-dev URLs even
// though the background would have whitelisted them.
var isLocal = d === 'localhost' || d === '::1' || /^\d{1,3}(\.\d{1,3}){3}$/.test(d);
if (!isLocal && !d.includes('.')) {
showToast(t('invalidDomain'));
return;
}
var wasWhitelisted = this.dataset.whitelisted === '1';
// Optimistic flip \u2014 switch the button label/state immediately so the
// click feels instant. The renderCurrentTab signature dedupe could
// otherwise let a polling-timer render run first and leave a stale sig
// that early-returns later.
var btnText = this.querySelector('.btn-text');
var btnIcon = this.querySelector('.btn-icon');
if (wasWhitelisted) {
if (btnText) btnText.textContent = t('neverSuspendSite');
if (btnIcon) btnIcon.innerHTML = icon('shield', 14);
this.classList.remove('is-whitelisted');
this.dataset.whitelisted = '0';
} else {
if (btnText) btnText.textContent = t('siteWhitelisted');
if (btnIcon) btnIcon.innerHTML = icon('shieldCheck', 14);
this.classList.add('is-whitelisted');
this.dataset.whitelisted = '1';
}
// Reset the currentTab signature so the next render is forced to
// re-execute (otherwise it could match the now-stale pre-click sig).
var section = document.getElementById('currentTabSection');
if (section) section.dataset.sig = '';
this.disabled = true;
try {
await msg({ action: wasWhitelisted ? 'removeWhitelist' : 'addWhitelist', domain: d });
showToast(wasWhitelisted ? t('removedFromWhitelist', [d]) : t('addedToWhitelist', [d]));
} finally {
this.disabled = false;
}
await loadAll();
});
document.getElementById('btnCloseDuplicates').addEventListener('click', async function() {
var res = await msg({ action: 'closeDuplicates' });
if (res && res.closed > 0) {
showToast(t('closedDuplicates', [String(res.closed)]));
await loadAll();
} else {
showToast(t('noDuplicates'));
}
});
document.getElementById('btnSearchToggle').addEventListener('click', function() {
var wrap = document.getElementById('tabSearchWrap');
var input = document.getElementById('tabSearchInput');
if (wrap.style.display === 'none') {
wrap.style.display = '';
input.focus();
} else {
wrap.style.display = 'none';
input.value = '';
if (_allTabs) renderTabList(_allTabs);
}
});
document.getElementById('tabSearchInput').addEventListener('input', function() {
if (_allTabs) renderTabList(filterTabs(_allTabs));
});
document.getElementById('tabSearchInput').addEventListener('keydown', function(e) {
if (e.key === 'Escape') {
var wrap = document.getElementById('tabSearchWrap');
this.value = '';
wrap.style.display = 'none';
if (_allTabs) renderTabList(_allTabs);
document.getElementById('btnSearchToggle').focus();
}
});
document.getElementById('btnExportTabs').addEventListener('click', async function() {
if (this.disabled) return;
this.disabled = true;
var btn = this;
var tabs = await msg({ action: 'getTabList' });
if (!tabs || !tabs.length) { btn.disabled = false; return; }
var text = tabs.map(function(tab) { return tab.title + '\n' + tab.url; }).join('\n\n');
try {
await navigator.clipboard.writeText(text);
var iconEl = btn.querySelector('[data-icon]');
if (iconEl) iconEl.innerHTML = icon('clipboardCheck', 14);
showToast(t('copiedTabs', [String(tabs.length)]));
setTimeout(function() {
var iconEl2 = btn.querySelector('[data-icon]');
if (iconEl2) iconEl2.innerHTML = icon('clipboard', 14);
btn.disabled = false;
}, 2000);
} catch {
showToast(t('failedToCopy'));
btn.disabled = false;
}
});
setupCollapsible('sessionsToggle', 'sessionsBody');
setupCollapsible('statsToggle', 'statsBody');
setupCollapsible('settingsToggle', 'settingsBody');
document.getElementById('btnSaveSession').addEventListener('click', saveSession);
document.getElementById('sessionNameInput').addEventListener('keydown', function(e) {
if (e.key === 'Enter') saveSession();
});
document.getElementById('btnExportSessions').addEventListener('click', async function() {
var sessions = await msg({ action: 'getSessions' });
if (!sessions || !sessions.length) { showToast(t('noSessionsToExport')); return; }
try {
await navigator.clipboard.writeText(JSON.stringify(sessions, null, 2));
showToast(t('copiedSessionsJson', [String(sessions.length)]));
} catch { showToast(t('failedToCopy')); }
});
document.getElementById('suspendTimer').addEventListener('change', async function() {
var val = parseInt(this.value, 10);
var s = await msg({ action: 'getSettings' });
if (!s) return;
if (val === 0) { s.enableAutoSuspend = false; }
else { s.enableAutoSuspend = true; s.suspendAfterMinutes = val; }
await msg({ action: 'updateSettings', settings: s });
document.getElementById('toggleAutoSuspend').checked = s.enableAutoSuspend;
await loadAll();
});
bindToggle('togglePinned', 'protectPinned');
bindToggle('toggleAudio', 'protectAudio');
bindHostToggle('toggleForms', 'protectForms');
bindToggle('toggleStartup', 'suspendOnStartup');
bindToggle('toggleAutoSuspend', 'enableAutoSuspend');
bindHostToggle('toggleMarkSuspended', 'markSuspendedTabs');
document.getElementById('btnAddWhitelist').addEventListener('click', addFromInput);
document.getElementById('whitelistInput').addEventListener('keydown', function(e) {
if (e.key === 'Enter') addFromInput();
});
document.getElementById('btnExportWhitelist').addEventListener('click', async function() {
var settings = await msg({ action: 'getSettings' });
if (!settings || !settings.whitelist || !settings.whitelist.length) {
showToast(t('noSitesWhitelisted'));
return;
}
try {
await navigator.clipboard.writeText(settings.whitelist.join('\n'));
showToast(t('copiedSites', [String(settings.whitelist.length)]));
} catch { showToast(t('failedToCopy')); }
});
document.getElementById('btnImportWhitelist').addEventListener('click', async function() {
try {
var text = await navigator.clipboard.readText();
var domains = text.split(/[\n,;]+/).map(function(d) {
var clean = d.trim().toLowerCase().replace(/^https?:\/\//, '').replace(/^www\./, '');
if (!clean.includes('/')) clean = clean.replace(/[/:?#].*$/, '');
return clean;
}).filter(function(d) { return d && d.split('/')[0].includes('.') && !d.startsWith('.') && !d.split('/')[0].endsWith('.'); });
if (!domains.length) { showToast(t('noValidDomains')); return; }
var settings = await msg({ action: 'getSettings' });
if (!settings) return;
var added = 0;
for (var i = 0; i < domains.length; i++) {
if (!settings.whitelist.includes(domains[i])) {
await msg({ action: 'addWhitelist', domain: domains[i] });
added++;
}
}
if (added) await loadAll();
showToast(t('importedSites', [String(added)]));
} catch { showToast(t('clipboardReadFailed')); }
});
document.getElementById('btnShareStats').addEventListener('click', async function() {
var stats = await msg({ action: 'getStats' });
if (!stats) return;
var total = stats.totalTabsSuspended || 0;
var today = stats.totalTabsSuspendedToday || 0;
var ram = total * MB_PER_TAB;
var lines = [
t('shareStatsHeader'),
'',
'\u2022 ' + t('shareStatsLineAllTime', [String(total)]),
'\u2022 ' + t('shareStatsLineToday', [String(today)]),
'\u2022 ' + t('shareStatsLineRam', [fmtRam(ram)]),
];
if (stats.installDate) {
var since = new Date(stats.installDate).toLocaleDateString(undefined, { month: 'short', year: 'numeric' });
lines.push('\u2022 ' + t('shareStatsLineSince', [since]));
}
lines.push('', 'https://chromewebstore.google.com/detail/drowzy-tab-suspender-memo/oijfnkaakdamnijjgehjpfmclhigmapa');
try {
await navigator.clipboard.writeText(lines.join('\n'));
showToast(t('copiedStats'));
} catch { showToast(t('failedToCopy')); }
});
// Customize-shortcuts link: opens chrome://extensions/shortcuts. Chrome
// forbids extensions from setting shortcuts programmatically (no
// chrome.commands.update in Chrome, only in Firefox), so the manual
// shortcuts editor is the only path. The status text reflects how many
// commands Chrome actually bound, so users with conflicting suggested_keys
// can see at a glance that something isn't set.
var customizeBtn = document.getElementById('btnCustomizeShortcuts');
if (customizeBtn) {
customizeBtn.addEventListener('click', function(e) {
e.preventDefault();
chrome.tabs.create({ url: 'chrome://extensions/shortcuts' });
});
}
renderShortcutsStatus();
var whatsNewBtn = document.getElementById('btnWhatsNew');
// `whatsNew` uses a $VERSION$ placeholder so each locale puts the version
// in the right grammatical position. Pass the live version as the sub.
whatsNewBtn.textContent = chrome.i18n.getMessage('whatsNew', [chrome.runtime.getManifest().version]) || ('What\'s new v' + chrome.runtime.getManifest().version);
whatsNewBtn.addEventListener('click', function(e) {
e.preventDefault();
chrome.tabs.create({ url: 'changelog.html' });
});
}
function bindToggle(id, key) {
document.getElementById(id).addEventListener('change', async function() {
var s = await msg({ action: 'getSettings' });
if (!s) return;
s[key] = this.checked;
await msg({ action: 'updateSettings', settings: s });
await loadAll();
});
}
function bindHostToggle(id, key) {
var el = document.getElementById(id);
el.addEventListener('change', async function() {
if (this.checked) {
var granted = false;
try {
granted = await chrome.permissions.request({ origins: ['<all_urls>'] });
} catch { granted = false; }
if (!granted) {
this.checked = false;
return;
}
}
var s = await msg({ action: 'getSettings' });
if (!s) return;
s[key] = this.checked;
await msg({ action: 'updateSettings', settings: s });
await loadAll();
});
}
var SECTION_IDS = [['sessionsToggle', 'sessionsBody'], ['statsToggle', 'statsBody'], ['settingsToggle', 'settingsBody']];
function restoreSectionStates() {
for (var i = 0; i < SECTION_IDS.length; i++) {
var toggleId = SECTION_IDS[i][0];
var bodyId = SECTION_IDS[i][1];
try {
if (localStorage.getItem('drowzy_section_' + toggleId) === '1') {
var body = document.getElementById(bodyId);
var toggle = document.getElementById(toggleId);
if (body) body.classList.add('open');
if (toggle) toggle.setAttribute('aria-expanded', 'true');
}
} catch {}
}
}
function setupCollapsible(toggleId, bodyId) {