-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
830 lines (756 loc) · 25.8 KB
/
index.html
File metadata and controls
830 lines (756 loc) · 25.8 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="theme-color" content="#4285f4">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<link rel="manifest" href="manifest.json">
<link rel="apple-touch-icon" href="icon-192.png">
<title>Class Timer</title>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 20px;
}
.container {
max-width: 500px;
margin: 0 auto;
}
h1 {
color: white;
text-align: center;
margin-bottom: 8px;
font-size: 28px;
}
.subtitle {
color: rgba(255,255,255,0.8);
text-align: center;
margin-bottom: 24px;
font-size: 14px;
}
.card {
background: white;
border-radius: 16px;
padding: 20px;
margin-bottom: 16px;
box-shadow: 0 4px 20px rgba(0,0,0,0.15);
}
.status-card {
text-align: center;
padding: 30px 20px;
}
.status-label {
font-size: 14px;
color: #666;
margin-bottom: 8px;
}
.status-value {
font-size: 24px;
font-weight: 700;
color: #333;
}
.status-value.disabled {
color: #e53935;
}
.next-class {
margin-top: 16px;
padding-top: 16px;
border-top: 1px solid #eee;
}
.next-class-label {
font-size: 12px;
color: #999;
margin-bottom: 4px;
}
.next-class-value {
font-size: 18px;
color: #4285f4;
font-weight: 600;
}
.section-title {
font-size: 12px;
font-weight: 600;
color: #666;
text-transform: uppercase;
letter-spacing: 1px;
margin-bottom: 12px;
}
.button-group {
display: flex;
gap: 8px;
flex-wrap: wrap;
}
.btn {
flex: 1;
min-width: 80px;
padding: 12px 16px;
border: none;
border-radius: 10px;
font-size: 14px;
font-weight: 600;
cursor: pointer;
transition: transform 0.1s, box-shadow 0.1s;
}
.btn:active {
transform: scale(0.98);
}
.btn-disable {
background: #ffebee;
color: #c62828;
}
.btn-enable {
background: #e8f5e9;
color: #2e7d32;
}
.btn-primary {
background: #4285f4;
color: white;
}
.row {
display: flex;
align-items: center;
padding: 12px 0;
border-bottom: 1px solid #f0f0f0;
}
.row:last-child {
border-bottom: none;
}
.row label {
flex: 1;
font-size: 15px;
color: #333;
}
.row input[type="text"] {
width: 160px;
padding: 10px 12px;
border: 2px solid #e0e0e0;
border-radius: 8px;
font-size: 15px;
transition: border-color 0.2s;
}
.row input[type="text"]:focus {
outline: none;
border-color: #4285f4;
}
.row select {
padding: 10px 12px;
border: 2px solid #e0e0e0;
border-radius: 8px;
font-size: 15px;
background: white;
}
.notification-banner {
background: #fff3e0;
color: #e65100;
padding: 16px;
border-radius: 12px;
margin-bottom: 16px;
display: none;
}
.notification-banner.show {
display: block;
}
.notification-banner button {
margin-top: 12px;
background: #e65100;
color: white;
border: none;
padding: 10px 20px;
border-radius: 8px;
font-weight: 600;
cursor: pointer;
}
.disabled-banner {
background: #ffebee;
color: #c62828;
padding: 12px 16px;
border-radius: 10px;
margin-bottom: 16px;
font-size: 14px;
display: none;
}
.disabled-banner.show {
display: block;
}
.install-banner {
background: #e3f2fd;
color: #1565c0;
padding: 16px;
border-radius: 12px;
margin-bottom: 16px;
display: none;
}
.install-banner.show {
display: block;
}
.install-banner button {
margin-top: 12px;
background: #1565c0;
color: white;
border: none;
padding: 10px 20px;
border-radius: 8px;
font-weight: 600;
cursor: pointer;
}
</style>
</head>
<body>
<div class="container">
<h1>📚 Class Timer</h1>
<p class="subtitle">Never be late to class again</p>
<div id="install-banner" class="install-banner">
<strong>Install this app!</strong><br>
Add Class Timer to your home screen for the best experience.
<br><button id="install-btn">Install App</button>
</div>
<div id="notification-banner" class="notification-banner">
<strong>Enable Notifications</strong><br>
Allow notifications to get alerts before classes start.
<br><button id="enable-notifications">Enable Notifications</button>
</div>
<div id="disabled-banner" class="disabled-banner">
Notifications disabled until <span id="disabled-until"></span>
</div>
<div class="card status-card">
<div class="status-label">Current Status</div>
<div class="status-value" id="status">Loading...</div>
<div class="next-class" id="next-class-container" style="display: none;">
<div class="next-class-label">Next up</div>
<div class="next-class-value" id="next-class"></div>
</div>
</div>
<div class="card">
<div class="section-title" id="disable-section-title">Disable Notifications</div>
<div class="button-group">
<button class="btn btn-disable" id="disable-1day">1 Day</button>
<button class="btn btn-disable" id="disable-1week">1 Week</button>
<button class="btn btn-disable" id="disable-2weeks">2 Weeks</button>
<button class="btn btn-disable" id="disable-indefinitely">Indefinitely</button>
<button class="btn btn-enable" id="enable" style="display: none;">Enable</button>
</div>
</div>
<div class="card">
<div class="section-title">Notification Delay</div>
<div class="row">
<label>Notify me</label>
<select id="delay">
<option value="1">1 min before</option>
<option value="2">2 min before</option>
<option value="3">3 min before</option>
<option value="5" selected>5 min before</option>
<option value="10">10 min before</option>
<option value="15">15 min before</option>
</select>
</div>
</div>
<div class="card">
<div class="section-title">Class Names</div>
<p style="font-size: 13px; color: #888; margin-bottom: 12px;">Leave blank to skip notifications for that line.</p>
<div class="row">
<label>Line 1</label>
<input type="text" id="line-1" placeholder="e.g. Math">
</div>
<div class="row">
<label>Line 2</label>
<input type="text" id="line-2" placeholder="e.g. English">
</div>
<div class="row">
<label>Line 3</label>
<input type="text" id="line-3" placeholder="e.g. Science">
</div>
<div class="row">
<label>Line 4</label>
<input type="text" id="line-4" placeholder="e.g. History">
</div>
<div class="row">
<label>Line 5</label>
<input type="text" id="line-5" placeholder="e.g. Art">
</div>
<div class="row">
<label>Line 6</label>
<input type="text" id="line-6" placeholder="e.g. Music">
</div>
<div class="row">
<label>Line 7</label>
<input type="text" id="line-7" placeholder="e.g. PE">
</div>
<div class="row">
<label>Line 8</label>
<input type="text" id="line-8" placeholder="e.g. Tech">
</div>
</div>
<p style="text-align: center; font-size: 12px; color: rgba(255,255,255,0.7); margin-top: 8px;">
Enable "Keep Alive" below to receive notifications when the app is in the background.
</p>
<div style="display: flex; gap: 12px; justify-content: center; margin-top: 16px;">
<button id="keep-alive" style="padding: 12px 24px; background: #4285f4; color: white; border: none; border-radius: 8px; font-size: 14px; font-weight: 600; cursor: pointer;">Keep Alive: Off</button>
</div>
</div>
<script>
// Schedule configuration
const tuesdayLines = [6, 7, 8, 1];
const wednesdayLines = [5, 3, 4, 2];
const thursdayLines = [8, 1, 6, 7];
const fridayLines = [4, 2, 5, 3];
function getMondaySchedule() {
return [
{ line: 1, startHour: 8, startMin: 50, endHour: 9, endMin: 40 },
{ line: 2, startHour: 9, startMin: 40, endHour: 10, endMin: 30 },
{ line: 3, startHour: 10, startMin: 30, endHour: 11, endMin: 20 },
{ line: 4, startHour: 11, startMin: 20, endHour: 12, endMin: 10 },
{ line: 5, startHour: 12, startMin: 40, endHour: 13, endMin: 30 },
{ line: 6, startHour: 13, startMin: 30, endHour: 14, endMin: 20 },
{ line: 7, startHour: 14, startMin: 20, endHour: 15, endMin: 10 },
{ line: 8, startHour: 15, startMin: 10, endHour: 16, endMin: 0 },
];
}
function getTuesdayToFridaySchedule(lines) {
const times = [
[8, 50, 10, 20],
[10, 35, 12, 5],
[12, 40, 13, 50],
[13, 50, 15, 0],
];
return times.map((t, i) => ({
line: lines[i],
startHour: t[0],
startMin: t[1],
endHour: t[2],
endMin: t[3],
}));
}
function getTodaySchedule() {
const day = new Date().getDay();
switch (day) {
case 1: return getMondaySchedule();
case 2: return getTuesdayToFridaySchedule(tuesdayLines);
case 3: return getTuesdayToFridaySchedule(wednesdayLines);
case 4: return getTuesdayToFridaySchedule(thursdayLines);
case 5: return getTuesdayToFridaySchedule(fridayLines);
default: return null;
}
}
function getClassTime(period, isStart) {
const now = new Date();
return new Date(now.getFullYear(), now.getMonth(), now.getDate(),
isStart ? period.startHour : period.endHour,
isStart ? period.startMin : period.endMin);
}
function pad(n) {
return n.toString().padStart(2, '0');
}
// Cookie helpers
function setCookie(name, value, days) {
try {
const expires = new Date(Date.now() + days * 864e5).toUTCString();
document.cookie = name + '=' + encodeURIComponent(value) + '; expires=' + expires + '; path=/; SameSite=Lax';
} catch (e) {}
}
function getCookie(name) {
try {
const cookies = document.cookie.split('; ');
for (const cookie of cookies) {
const [key, val] = cookie.split('=');
if (key === name) return decodeURIComponent(val);
}
} catch (e) {}
return null;
}
// Settings management (uses cookies, falls back to localStorage)
function loadSettings() {
const defaults = {
lineNames: {},
notificationDelay: 5,
disabledUntil: null
};
try {
// Try cookies first
const cookieData = getCookie('classTimerSettings');
if (cookieData) {
return { ...defaults, ...JSON.parse(cookieData) };
}
// Fall back to localStorage
const saved = localStorage.getItem('classTimerSettings');
if (saved) {
return { ...defaults, ...JSON.parse(saved) };
}
} catch (e) {}
return defaults;
}
function saveSettings(settings) {
try {
// Save to cookies (expire in 1 year)
setCookie('classTimerSettings', JSON.stringify(settings), 365);
// Also try localStorage as backup
localStorage.setItem('classTimerSettings', JSON.stringify(settings));
} catch (e) {}
scheduleNotifications();
}
function getLineName(line) {
const settings = loadSettings();
const name = settings.lineNames[line];
return name && name.trim() !== '' ? name : null;
}
function isDisabled() {
const settings = loadSettings();
if (settings.disabledUntil) {
return new Date() < new Date(settings.disabledUntil);
}
return false;
}
// Status updates
function updateStatus() {
const statusEl = document.getElementById('status');
const nextClassContainer = document.getElementById('next-class-container');
const nextClassEl = document.getElementById('next-class');
const disabledBanner = document.getElementById('disabled-banner');
const disabledUntilEl = document.getElementById('disabled-until');
const settings = loadSettings();
// Check if disabled
if (settings.disabledIndefinitely) {
disabledBanner.classList.remove('show');
statusEl.textContent = 'Notifications Off';
statusEl.classList.add('disabled');
nextClassContainer.style.display = 'none';
return;
} else if (isDisabled()) {
const until = new Date(settings.disabledUntil);
disabledUntilEl.textContent = until.toLocaleDateString();
disabledBanner.classList.add('show');
statusEl.textContent = 'Notifications Off';
statusEl.classList.add('disabled');
nextClassContainer.style.display = 'none';
return;
} else {
disabledBanner.classList.remove('show');
statusEl.classList.remove('disabled');
}
const schedule = getTodaySchedule();
if (!schedule) {
statusEl.textContent = 'No classes today';
nextClassContainer.style.display = 'none';
return;
}
const now = new Date();
// Check if in a class
for (let i = 0; i < schedule.length; i++) {
const period = schedule[i];
const start = getClassTime(period, true);
const end = getClassTime(period, false);
if (now >= start && now < end) {
const name = getLineName(period.line);
// If this line is empty/inactive, show "No class" and look for next
if (!name) {
statusEl.textContent = 'No class';
// Find next class with a name
let nextNamedPeriod = null;
for (let j = i + 1; j < schedule.length; j++) {
if (getLineName(schedule[j].line)) {
nextNamedPeriod = schedule[j];
break;
}
}
if (nextNamedPeriod) {
const nextName = getLineName(nextNamedPeriod.line);
nextClassContainer.style.display = 'block';
nextClassEl.textContent = `Next: ${nextName} at ${nextNamedPeriod.startHour}:${pad(nextNamedPeriod.startMin)}`;
} else {
// No more classes today
statusEl.textContent = 'End of day';
nextClassContainer.style.display = 'none';
}
return;
}
statusEl.textContent = `In ${name}`;
nextClassContainer.style.display = 'block';
// Find next class with a name set
let nextNamedPeriod = null;
for (let j = i + 1; j < schedule.length; j++) {
if (getLineName(schedule[j].line)) {
nextNamedPeriod = schedule[j];
break;
}
}
// Check if next class starts right after this one ends (no gap)
const currentEnd = getClassTime(period, false);
const nextStart = nextNamedPeriod ? getClassTime(nextNamedPeriod, true) : null;
const isBackToBack = nextStart && (nextStart.getTime() - currentEnd.getTime() < 60000); // within 1 min
if (nextNamedPeriod && isBackToBack) {
const nextName = getLineName(nextNamedPeriod.line);
nextClassEl.textContent = `Next: ${nextName} at ${nextNamedPeriod.startHour}:${pad(nextNamedPeriod.startMin)}`;
} else {
// Gap before next class, or no more classes
nextClassEl.textContent = `Until ${period.endHour}:${pad(period.endMin)}`;
}
return;
}
}
// Check for next class (skip empty lines)
for (const period of schedule) {
const name = getLineName(period.line);
if (!name) continue; // Skip empty lines
const start = getClassTime(period, true);
if (start > now) {
const mins = Math.round((start - now) / 60000);
if (mins < 60) {
statusEl.textContent = `${name} in ${mins}m`;
} else {
statusEl.textContent = `Next: ${name}`;
}
nextClassContainer.style.display = 'block';
nextClassEl.textContent = `Starts at ${period.startHour}:${pad(period.startMin)}`;
return;
}
}
statusEl.textContent = 'End of day';
nextClassContainer.style.display = 'none';
}
// Notifications - polls wall clock time instead of using setTimeout
const sentNotifications = new Set();
let lastCheckedDay = -1;
function checkNotifications() {
try {
const settings = loadSettings();
if (isDisabled()) return;
if (settings.disabledIndefinitely) return;
if (!('Notification' in window) || Notification.permission !== 'granted') return;
const schedule = getTodaySchedule();
if (!schedule) return;
const now = new Date();
const today = now.getDate();
// Reset sent notifications on a new day
if (today !== lastCheckedDay) {
sentNotifications.clear();
lastCheckedDay = today;
}
const delayMins = settings.notificationDelay || 5;
const delayMs = delayMins * 60 * 1000;
for (const period of schedule) {
const name = getLineName(period.line);
if (!name) continue;
const start = getClassTime(period, true);
const notifyTime = new Date(start.getTime() - delayMs);
const identifier = `${period.line}-${period.startHour}-${period.startMin}`;
// Send if we're past notify time but before class starts, and haven't sent yet
if (now >= notifyTime && now < start && !sentNotifications.has(identifier)) {
sentNotifications.add(identifier);
(async () => {
try {
const reg = await navigator.serviceWorker.ready;
await reg.showNotification('Class Starting Soon', {
body: `${name} starts in ${delayMins} minutes`,
icon: 'icon-192.png',
tag: `class-${period.line}`
});
} catch (e) {
console.error('Notification failed:', e);
}
})();
}
}
} catch (e) {
console.error('checkNotifications error:', e);
}
}
// Keep old name for compatibility with other calls
function scheduleNotifications() {
sentNotifications.clear();
checkNotifications();
}
// Initialize
function init() {
const settings = loadSettings();
// Populate line names
for (let i = 1; i <= 8; i++) {
const input = document.getElementById(`line-${i}`);
input.value = settings.lineNames[i] || '';
input.addEventListener('input', () => {
const current = loadSettings();
current.lineNames[i] = input.value;
saveSettings(current);
updateStatus();
});
}
// Delay select
const delaySelect = document.getElementById('delay');
delaySelect.value = (settings.notificationDelay || 5).toString();
delaySelect.addEventListener('change', () => {
const current = loadSettings();
current.notificationDelay = parseInt(delaySelect.value);
saveSettings(current);
});
// Disable buttons
document.getElementById('disable-1day').addEventListener('click', () => {
const current = loadSettings();
current.disabledUntil = new Date(Date.now() + 24 * 60 * 60 * 1000).toISOString();
saveSettings(current);
updateStatus();
updateDisableButtons();
});
document.getElementById('disable-1week').addEventListener('click', () => {
const current = loadSettings();
current.disabledUntil = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000).toISOString();
saveSettings(current);
updateStatus();
updateDisableButtons();
});
document.getElementById('disable-2weeks').addEventListener('click', () => {
const current = loadSettings();
current.disabledUntil = new Date(Date.now() + 14 * 24 * 60 * 60 * 1000).toISOString();
saveSettings(current);
updateStatus();
updateDisableButtons();
});
document.getElementById('enable').addEventListener('click', () => {
const current = loadSettings();
current.disabledUntil = null;
current.disabledIndefinitely = false;
saveSettings(current);
updateStatus();
scheduleNotifications();
updateDisableButtons();
});
document.getElementById('disable-indefinitely').addEventListener('click', () => {
const settings = loadSettings();
settings.disabledIndefinitely = true;
saveSettings(settings);
scheduledTimeouts.forEach(t => clearTimeout(t));
scheduledTimeouts = [];
updateStatus();
updateDisableButtons();
});
// Toggle between Indefinitely and Enable buttons based on disabled state
function updateDisableButtons() {
const settings = loadSettings();
const isCurrentlyDisabled = isDisabled() || settings.disabledIndefinitely;
const indefinitelyBtn = document.getElementById('disable-indefinitely');
const enableBtn = document.getElementById('enable');
const sectionTitle = document.getElementById('disable-section-title');
if (isCurrentlyDisabled) {
indefinitelyBtn.style.display = 'none';
enableBtn.style.display = '';
sectionTitle.textContent = 'Enable Notifications';
} else {
indefinitelyBtn.style.display = '';
enableBtn.style.display = 'none';
sectionTitle.textContent = 'Disable Notifications';
}
}
updateDisableButtons();
// Notification permission banner (only shown when permission not yet granted)
const notificationBanner = document.getElementById('notification-banner');
function updateNotificationBanner() {
try {
if (!('Notification' in window)) return;
if (Notification.permission === 'default') {
notificationBanner.classList.add('show');
} else {
notificationBanner.classList.remove('show');
}
} catch (e) {}
}
updateNotificationBanner();
document.getElementById('enable-notifications').addEventListener('click', async () => {
const result = await Notification.requestPermission();
if (result === 'granted') {
updateNotificationBanner();
scheduleNotifications();
}
});
// Update status and check notifications every 30 seconds
updateStatus();
checkNotifications();
setInterval(() => {
updateStatus();
checkNotifications();
}, 30000);
}
// PWA Install prompt
let deferredPrompt;
window.addEventListener('beforeinstallprompt', (e) => {
e.preventDefault();
deferredPrompt = e;
document.getElementById('install-banner').classList.add('show');
});
document.getElementById('install-btn').addEventListener('click', async () => {
if (deferredPrompt) {
deferredPrompt.prompt();
const result = await deferredPrompt.userChoice;
if (result.outcome === 'accepted') {
document.getElementById('install-banner').classList.remove('show');
}
deferredPrompt = null;
}
});
// Register service worker
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('sw.js');
}
// Keep Alive - plays silent audio to prevent browser from suspending tab
let audioContext = null;
let keepAliveActive = false;
document.getElementById('keep-alive').addEventListener('click', async () => {
const btn = document.getElementById('keep-alive');
if (!keepAliveActive) {
try {
// Create audio context with silent oscillator
audioContext = new (window.AudioContext || window.webkitAudioContext)();
const oscillator = audioContext.createOscillator();
const gainNode = audioContext.createGain();
// Set volume to nearly silent (not zero, or browser may optimize it away)
gainNode.gain.value = 0.001;
oscillator.connect(gainNode);
gainNode.connect(audioContext.destination);
oscillator.start();
keepAliveActive = true;
btn.textContent = 'Keep Alive: On';
btn.style.background = '#43a047';
// Save preference
const settings = loadSettings();
settings.keepAlive = true;
saveSettings(settings);
} catch (e) {
btn.textContent = 'Keep Alive: Error';
btn.style.background = '#e53935';
}
} else {
if (audioContext) {
audioContext.close();
audioContext = null;
}
keepAliveActive = false;
btn.textContent = 'Keep Alive: Off';
btn.style.background = '#4285f4';
// Save preference
const settings = loadSettings();
settings.keepAlive = false;
saveSettings(settings);
}
});
// Restore keep-alive state on load
try {
const savedSettings = loadSettings();
if (savedSettings.keepAlive) {
document.getElementById('keep-alive').click();
}
} catch (e) {}
// Start
try {
init();
} catch (e) {
console.error('init error:', e);
document.getElementById('status').textContent = 'Error loading';
}
</script>
</body>
</html>