-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
720 lines (640 loc) · 24.5 KB
/
Copy pathscript.js
File metadata and controls
720 lines (640 loc) · 24.5 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
const Timer = {
elements: {
timer: null,
timerState: null,
timerContainer: null,
historyList: null,
scrambleText: null,
eventSelect: null,
sectionSelect: null,
wcaInspection: null,
showNumbering: null,
autoScramble: null,
darkMode: null
},
state: {
interval: null,
preparing: false,
timing: false,
history: [],
solveCounter: 0,
inspectionTime: 15,
isInspecting: false,
inspectionInterval: null,
inspectionStartTime: 0
},
scramblers: {
'2x2': () => {
const moves = ['R', 'U', 'F', 'L', "R'", "U'", "F'", "L'", 'R2', 'U2', 'F2', 'L2'];
let scramble = [];
let lastMove = '';
for (let i = 0; i < 9; i++) {
let move;
do {
move = moves[Math.floor(Math.random() * moves.length)];
} while (move[0] === lastMove[0]);
lastMove = move;
scramble.push(move);
}
return scramble.join(' ');
},
'3x3': () => {
const moves = ['R', 'L', 'U', 'D', 'F', 'B', "R'", "L'", "U'", "D'", "F'", "B'", 'R2', 'L2', 'U2', 'D2', 'F2', 'B2'];
let scramble = [];
let lastFace = '';
for (let i = 0; i < 20; i++) {
let move;
do {
move = moves[Math.floor(Math.random() * moves.length)];
} while (move[0] === lastFace);
scramble.push(move);
lastFace = move[0];
}
return scramble.join(' ');
},
'4x4': () => {
const moves = ['R', 'L', 'U', 'D', 'F', 'B', "R'", "L'", "U'", "D'", "F'", "B'", 'R2', 'L2', 'U2', 'D2', 'F2', 'B2'];
const wideMoves = ['Rw', 'Lw', 'Uw', 'Dw', 'Fw', 'Bw', "Rw'", "Lw'", "Uw'", "Dw'", "Fw'", "Bw'", 'Rw2', 'Lw2', 'Uw2', 'Dw2', 'Fw2', 'Bw2'];
let scramble = [];
let lastFace = '';
// First part: regular moves
for (let i = 0; i < 20; i++) {
let move;
do {
move = moves[Math.floor(Math.random() * moves.length)];
} while (move[0] === lastFace);
scramble.push(move);
lastFace = move[0];
}
// Second part: wide moves
for (let i = 0; i < 20; i++) {
let move = wideMoves[Math.floor(Math.random() * wideMoves.length)];
scramble.push(move);
}
return scramble.join(' ');
},
'5x5': () => Timer.generateWithMoves([
'R', 'L', 'U', 'D', 'F', 'B', "R'", "L'", "U'", "D'", "F'", "B'", 'R2', 'L2', 'U2', 'D2', 'F2', 'B2',
'Rw', 'Lw', 'Uw', 'Dw', 'Fw', 'Bw', "Rw'", "Lw'", "Uw'", "Dw'", "Fw'", "Bw'", 'Rw2', 'Lw2', 'Uw2', 'Dw2', 'Fw2', 'Bw2',
'3Rw', '3Lw', '3Uw', '3Dw', '3Fw', '3Bw', "3Rw'", "3Lw'", "3Uw'", "3Dw'", "3Fw'", "3Bw'", '3Rw2', '3Lw2', '3Uw2', '3Dw2', '3Fw2', '3Bw2'
], 60),
'6x6': () => Timer.generateWithMoves([
'R', 'L', 'U', 'D', 'F', 'B', "R'", "L'", "U'", "D'", "F'", "B'", 'R2', 'L2', 'U2', 'D2', 'F2', 'B2',
'Rw', 'Lw', 'Uw', 'Dw', 'Fw', 'Bw', "Rw'", "Lw'", "Uw'", "Dw'", "Fw'", "Bw'", 'Rw2', 'Lw2', 'Uw2', 'Dw2', 'Fw2', 'Bw2',
'3Rw', '3Lw', '3Uw', '3Dw', '3Fw', '3Bw', "3Rw'", "3Lw'", "3Uw'", "3Dw'", "3Fw'", "3Bw'", '3Rw2', '3Lw2', '3Uw2', '3Dw2', '3Fw2', '3Bw2'
], 80),
'7x7': () => Timer.generateWithMoves([
'R', 'L', 'U', 'D', 'F', 'B', "R'", "L'", "U'", "D'", "F'", "B'", 'R2', 'L2', 'U2', 'D2', 'F2', 'B2',
'Rw', 'Lw', 'Uw', 'Dw', 'Fw', 'Bw', "Rw'", "Lw'", "Uw'", "Dw'", "Fw'", "Bw'", 'Rw2', 'Lw2', 'Uw2', 'Dw2', 'Fw2', 'Bw2',
'3Rw', '3Lw', '3Uw', '3Dw', '3Fw', '3Bw', "3Rw'", "3Lw'", "3Uw'", "3Dw'", "3Fw'", "3Bw'", '3Rw2', '3Lw2', '3Uw2', '3Dw2', '3Fw2', '3Bw2',
'3Dw', '3Bw', '3Lw', '3Rw', '3Fw', '3Uw'
], 100),
'skewb': () => Timer.generateWithMoves(['R', 'L', 'U', 'B', "R'", "L'", "U'", "B'"], 9),
'megaminx': () => {
let scramble = [];
for (let i = 0; i < 7; i++) {
const moves = ['R++ D++', 'R++ D--', 'R-- D++', 'R-- D--'];
for (let j = 0; j < 5; j++) {
scramble.push(moves[Math.floor(Math.random() * moves.length)]);
}
scramble.push('U');
}
return scramble.join(' ');
},
'pyraminx': () => {
const moves = ['L', 'R', 'U', 'B', "L'", "R'", "U'", "B'"];
const tips = ['l', 'r', 'u', 'b', "l'", "r'", "u'", "b'"];
let scramble = [];
// Add tips (0-2 for each tip)
for (let i = 0; i < 4; i++) {
if (Math.random() < 0.75) {
scramble.push(tips[Math.floor(Math.random() * tips.length)]);
}
}
// Add regular moves
for (let i = 0; i < 8; i++) {
scramble.push(moves[Math.floor(Math.random() * moves.length)]);
}
return scramble.join(' ');
},
'square-1': () => {
let scramble = [];
for (let i = 0; i < 12; i++) {
const top = Math.floor(Math.random() * 7) - 3;
const bottom = Math.floor(Math.random() * 7) - 3;
scramble.push(`(${top},${bottom})`);
if (i < 11) scramble.push('/');
}
return scramble.join(' ');
},
'clock': () => {
const moves = ['UR', 'DR', 'DL', 'UL', 'U', 'R', 'D', 'L', 'ALL'];
let scramble = [];
// First face
for (let i = 0; i < 9; i++) {
const move = moves[i];
const amount = Math.floor(Math.random() * 12) - 5;
scramble.push(`${move}${amount > 0 ? '+' : ''}${amount}`);
}
// Flip
scramble.push('y2');
// Second face
for (let i = 0; i < 4; i++) {
const move = moves[i];
const amount = Math.floor(Math.random() * 12) - 5;
scramble.push(`${move}${amount > 0 ? '+' : ''}${amount}`);
}
return scramble.join(' ');
}
},
generateWithMoves(moves, length) {
let scramble = [];
let lastAxis = '';
for (let i = 0; i < length; i++) {
let move;
do {
move = moves[Math.floor(Math.random() * moves.length)];
const currentAxis = move[0];
if (currentAxis === lastAxis) continue;
lastAxis = currentAxis;
break;
} while (true);
scramble.push(move);
}
return scramble.join(' ');
}, init() {
this.initElements();
this.setupEventListeners();
this.loadSettings();
this.updateScramble();
this.setInitialTheme();
},
initElements() {
this.elements = {
timer: document.getElementById('timer'),
timerState: document.querySelector('.timer-state'),
timerContainer: document.getElementById('timer-container'),
historyList: document.getElementById('history-list'),
scrambleText: document.getElementById('scramble-text'),
eventSelect: document.getElementById('cube-event'),
wcaInspection: document.getElementById('wcaInspection'),
showNumbering: document.getElementById('show-numbering'),
autoScramble: document.getElementById('auto-scramble'),
darkMode: document.getElementById('darkModeToggle')
};
},
setupEventListeners() {
document.addEventListener('keydown', (e) => {
if (e.code === 'Space' && document.activeElement === document.body) {
e.preventDefault();
if (!this.state.timing) {
this.startPreparing();
}
}
});
document.addEventListener('keyup', (e) => {
if (e.code === 'Space' && document.activeElement === document.body) {
e.preventDefault();
this.stopOrStart();
}
});
this.elements.timerContainer.addEventListener('mousedown', (e) => {
e.preventDefault();
if (!this.state.timing) {
this.startPreparing();
}
});
this.elements.timerContainer.addEventListener('mouseup', (e) => {
e.preventDefault();
this.stopOrStart();
});
this.elements.eventSelect.addEventListener('change', () => {
this.updateScramble();
this.saveSettings();
});
this.elements.darkMode.addEventListener('change', () => {
document.body.classList.toggle('dark-theme', this.elements.darkMode.checked);
this.saveSettings();
});
this.elements.wcaInspection.addEventListener('change', () => this.saveSettings());
this.elements.showNumbering.addEventListener('change', () => {
this.updateNumbering();
this.saveSettings();
});
this.elements.autoScramble.addEventListener('change', () => this.saveSettings());
// Clear history button
document.getElementById('clear-history').addEventListener('click', () => {
document.getElementById('confirmation-modal').style.display = 'flex';
});
document.getElementById('cancel-clear').addEventListener('click', () => {
document.getElementById('confirmation-modal').style.display = 'none';
});
document.getElementById('confirm-clear').addEventListener('click', () => {
this.state.history = [];
this.state.solveCounter = 0;
this.elements.historyList.innerHTML = '';
this.updateStats();
this.saveSettings();
document.getElementById('confirmation-modal').style.display = 'none';
});
// Export history button
document.getElementById('export-history').addEventListener('click', () => {
const data = JSON.stringify(this.state.history.map(solve => ({
time: solve.time,
date: solve.date,
event: solve.event
})), null, 2);
const blob = new Blob([data], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'twistytime-history.json';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
// New scramble button
document.getElementById('new-scramble').addEventListener('click', () => {
this.updateScramble();
});
},
startPreparing() {
if (!this.state.timing && !this.state.preparing && !this.state.isInspecting) {
if (this.elements.wcaInspection.checked) {
this.startInspection();
} else {
this.state.preparing = true;
this.elements.timerState.textContent = 'Set';
this.elements.timer.style.color = '#0f0';
this.elements.timerContainer.classList.add('preparing');
}
}
},
startInspection() {
this.state.isInspecting = true;
this.state.inspectionStartTime = Date.now();
this.elements.timer.style.color = '#ff0';
this.elements.timer.textContent = '15';
this.elements.timerState.textContent = 'Inspection';
this.state.inspectionInterval = setInterval(() => {
const elapsed = Math.floor((Date.now() - this.state.inspectionStartTime) / 1000);
const remaining = 15 - elapsed;
if (remaining >= 0) {
this.elements.timer.textContent = remaining.toString();
if (remaining <= 8) {
this.elements.timer.style.color = '#f00';
}
} else if (remaining < -2) {
clearInterval(this.state.inspectionInterval);
this.elements.timer.textContent = 'DNF';
this.state.isInspecting = false;
setTimeout(() => {
this.elements.timer.textContent = '0.000';
this.elements.timer.style.color = '';
this.elements.timerState.textContent = 'Ready';
}, 2000);
}
}, 100);
}, stopOrStart() {
if (this.state.timing) {
clearInterval(this.state.interval);
const endTime = performance.now();
const duration = ((endTime - this.state.startTime) / 1000).toFixed(3);
this.state.solveCounter++;
if (this.state.inspectionStartTime > 0) {
const inspectionDuration = (this.state.startTime - this.state.inspectionStartTime) / 1000;
if (inspectionDuration > 15 && inspectionDuration <= 17) {
this.addToHistory((parseFloat(duration) + 2).toFixed(3));
} else {
this.addToHistory(duration);
}
this.state.inspectionStartTime = 0;
} else {
this.addToHistory(duration);
}
document.body.classList.remove('timing-active');
this.elements.timerState.textContent = 'Ready';
this.elements.timer.style.color = '';
this.elements.timerContainer.classList.remove('timing');
this.state.timing = false;
if (this.elements.autoScramble.checked) {
this.updateScramble();
}
this.saveSettings();
} else if (this.state.preparing || this.state.isInspecting) {
if (this.state.isInspecting) {
clearInterval(this.state.inspectionInterval);
this.state.isInspecting = false;
}
this.state.startTime = performance.now();
this.elements.timerState.textContent = 'Timing...';
this.elements.timer.style.color = '';
this.elements.timerContainer.classList.remove('preparing');
this.elements.timerContainer.classList.add('timing');
document.body.classList.add('timing-active');
this.state.interval = setInterval(() => {
const time = ((performance.now() - this.state.startTime) / 1000).toFixed(3);
this.elements.timer.textContent = time;
}, 10);
this.state.timing = true;
this.state.preparing = false;
}
},
updateScramble() {
const currentEvent = this.elements.eventSelect.value;
const scramble = this.generateScramble(currentEvent);
this.elements.scrambleText.textContent = scramble;
},
generateScramble(event) {
const scrambler = this.scramblers[event] || this.scramblers['3x3'];
return scrambler();
},
addToHistory(time) {
const solveData = {
time: parseFloat(time),
number: this.state.solveCounter,
date: new Date(), event: this.elements.eventSelect.value
};
this.state.history.unshift(solveData);
this.addHistoryItemToDOM(solveData);
this.updateStats();
}, addHistoryItemToDOM(solveData) {
const item = document.createElement('div');
item.className = 'history-item';
const numberSpan = document.createElement('span');
numberSpan.className = 'solve-number';
numberSpan.textContent = `#${solveData.number}`;
numberSpan.style.display = this.elements.showNumbering.checked ? 'block' : 'none';
const timeSpan = document.createElement('span');
timeSpan.className = 'solve-time';
timeSpan.textContent = solveData.time.toFixed(3);
if (solveData.isDNF) timeSpan.classList.add('dnf');
if (solveData.isPlus2) timeSpan.classList.add('plus2');
// Check if this is a best or worst time
if (this.state.history.length > 0) {
const times = this.state.history.map(solve => solve.time);
const bestTime = Math.min(...times);
const worstTime = Math.max(...times);
if (solveData.time === bestTime) item.classList.add('best-solve');
if (solveData.time === worstTime) item.classList.add('worst-solve');
}
const dateSpan = document.createElement('span');
dateSpan.className = 'solve-date';
dateSpan.textContent = `${solveData.event} ${solveData.date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}`;
if (solveData.comment) {
const commentDiv = document.createElement('div');
commentDiv.className = 'solve-comment';
commentDiv.textContent = solveData.comment;
dateSpan.appendChild(commentDiv);
}
const menuBtn = document.createElement('button');
menuBtn.className = 'solve-menu-btn';
menuBtn.textContent = '⋮';
menuBtn.addEventListener('click', (e) => {
e.stopPropagation();
const menu = this.createSolveMenu(solveData, item);
// Close any other open menus
document.querySelectorAll('.solve-menu').forEach(m => m !== menu && m.remove());
// Position the menu
const rect = menuBtn.getBoundingClientRect();
menu.style.top = rect.bottom + 'px';
menu.style.left = rect.left + 'px';
menu.style.display = 'block';
// Close menu when clicking outside
const closeMenu = (e) => {
if (!menu.contains(e.target) && e.target !== menuBtn) {
menu.remove();
document.removeEventListener('click', closeMenu);
}
};
document.addEventListener('click', closeMenu);
});
const deleteBtn = document.createElement('button');
deleteBtn.className = 'delete-btn';
deleteBtn.textContent = '×';
deleteBtn.addEventListener('click', () => {
const index = this.state.history.indexOf(solveData);
if (index > -1) {
this.state.history.splice(index, 1);
item.remove();
this.updateStats();
this.saveSettings();
}
});
item.appendChild(numberSpan);
item.appendChild(timeSpan);
item.appendChild(dateSpan);
item.appendChild(menuBtn);
item.appendChild(deleteBtn);
this.elements.historyList.prepend(item);
}, updateStats() {
document.getElementById('ao5').textContent = this.getAverage(5);
document.getElementById('ao12').textContent = this.getAverage(12);
if (this.state.history.length > 0) {
const times = this.state.history.map(solve => solve.time);
const bestTime = Math.min(...times);
const worstTime = Math.max(...times);
document.getElementById('best-time').textContent = bestTime.toFixed(3);
document.getElementById('worst-time').textContent = worstTime.toFixed(3);
// Update highlighting for all history items
this.elements.historyList.querySelectorAll('.history-item').forEach(item => {
item.classList.remove('best-solve', 'worst-solve');
const timeEl = item.querySelector('.solve-time');
const time = parseFloat(timeEl.textContent);
if (time === bestTime) item.classList.add('best-solve');
if (time === worstTime) item.classList.add('worst-solve');
});
} else {
document.getElementById('best-time').textContent = '-';
document.getElementById('worst-time').textContent = '-';
}
}, getAverage(n) {
if (this.state.history.length < n) return '-';
const solves = this.state.history.slice(0, n);
// If too many DNFs, return DNF
const dnfCount = solves.filter(solve => solve.isDNF).length;
if (dnfCount > Math.ceil(n * 0.2)) return 'DNF';
const times = solves.map(solve => solve.isDNF ? Infinity : solve.time);
const min = Math.min(...times);
const max = Math.max(...times);
// For Ao5 and larger, remove best and worst times
let sum = 0;
let count = 0;
if (n >= 5) {
times.forEach(time => {
if (time !== min && time !== max) {
sum += time;
count++;
}
});
} else {
sum = times.reduce((a, b) => a + b, 0);
count = n;
}
return (sum / count).toFixed(3);
},
updateNumbering() {
const show = this.elements.showNumbering.checked;
this.elements.historyList.querySelectorAll('.solve-number').forEach(el => {
el.style.display = show ? 'block' : 'none';
});
}, saveSettings() {
try {
const data = {
darkMode: this.elements.darkMode.checked,
showNumbering: this.elements.showNumbering.checked,
autoScramble: this.elements.autoScramble.checked,
wcaInspection: this.elements.wcaInspection.checked,
currentEvent: this.elements.eventSelect.value,
solveCounter: this.state.solveCounter,
solveHistory: this.state.history.map(solve => ({
time: solve.time,
number: solve.number,
date: solve.date.toISOString(),
event: solve.event,
isDNF: solve.isDNF || false,
isPlus2: solve.isPlus2 || false,
comment: solve.comment || ''
}))
};
localStorage.setItem('cubeTimerData', JSON.stringify(data));
} catch (error) {
console.error('Error saving settings:', error);
}
},
loadSettings() {
try {
const saved = localStorage.getItem('cubeTimerData');
if (saved) {
const data = JSON.parse(saved);
this.elements.darkMode.checked = data.darkMode === true;
document.body.classList.toggle('dark-theme', data.darkMode === true);
this.elements.showNumbering.checked = data.showNumbering !== false;
this.elements.autoScramble.checked = data.autoScramble !== false;
this.elements.wcaInspection.checked = data.wcaInspection === true;
if (data.currentEvent) {
this.elements.eventSelect.value = data.currentEvent;
}
if (Array.isArray(data.solveHistory)) {
this.state.solveCounter = data.solveCounter || 0; this.state.history = data.solveHistory.map(solve => ({
time: solve.time,
number: solve.number,
date: new Date(solve.date),
event: solve.event || '3x3',
isDNF: solve.isDNF || false,
isPlus2: solve.isPlus2 || false,
comment: solve.comment || ''
}));
this.elements.historyList.innerHTML = '';
this.state.history.forEach(solveData => this.addHistoryItemToDOM(solveData));
this.updateStats();
}
}
} catch (error) {
console.error('Error loading settings:', error);
localStorage.removeItem('cubeTimerData');
}
}, setInitialTheme() {
document.querySelector('header').style.backgroundColor = '#121212';
document.querySelectorAll('button').forEach(btn => {
if (!btn.classList.contains('delete-btn') && !btn.classList.contains('clear-btn')) {
btn.style.backgroundColor = '#121212';
btn.style.color = '#fff';
btn.style.borderColor = '#121212';
}
});
document.querySelectorAll('.slider').forEach(slider => {
slider.style.backgroundColor = '#121212';
});
},
createSolveMenu(solveData, itemElement) {
const menu = document.createElement('div');
menu.className = 'solve-menu';
document.body.appendChild(menu);
const createMenuItem = (text, onClick) => {
const item = document.createElement('div');
item.className = 'solve-menu-item';
item.textContent = text;
item.addEventListener('click', onClick);
menu.appendChild(item);
};
createMenuItem(solveData.isDNF ? 'Remove DNF' : 'DNF', () => {
solveData.isDNF = !solveData.isDNF;
const timeSpan = itemElement.querySelector('.solve-time');
timeSpan.classList.toggle('dnf');
this.saveSettings();
this.updateStats();
menu.remove();
});
createMenuItem(solveData.isPlus2 ? 'Remove +2' : '+2', () => {
solveData.isPlus2 = !solveData.isPlus2;
if (solveData.isPlus2) {
solveData.time += 2;
} else {
solveData.time -= 2;
}
const timeSpan = itemElement.querySelector('.solve-time');
timeSpan.textContent = solveData.time.toFixed(3);
timeSpan.classList.toggle('plus2');
this.saveSettings();
this.updateStats();
menu.remove();
});
createMenuItem(solveData.comment ? 'Edit Comment' : 'Add Comment', () => {
const comment = prompt('Enter comment:', solveData.comment || '');
if (comment !== null) {
solveData.comment = comment.trim();
let commentDiv = itemElement.querySelector('.solve-comment');
if (solveData.comment) {
if (!commentDiv) {
commentDiv = document.createElement('div');
commentDiv.className = 'solve-comment';
itemElement.querySelector('.solve-date').appendChild(commentDiv);
}
commentDiv.textContent = solveData.comment;
} else if (commentDiv) {
commentDiv.remove();
}
this.saveSettings();
}
menu.remove();
});
return menu;
}
};
// Initialize timer when page loads
document.addEventListener('DOMContentLoaded', () => Timer.init());
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('/service-worker.js')
.then(reg => console.log('SW registered:', reg))
.catch(err => console.error('SW failed:', err));
});
}
const screenshotBtn = document.getElementById("screenshot-btn");
screenshotBtn.addEventListener("click", () => {
alert("It will only take the screenshot of the visible part of the page, not the full page. If you want to take a full page screenshot, use the browser's built-in screenshot feature.");
screenshotBtn.style.visibility = "hidden"; // hide button
const target = document.getElementById("screenshot-target");
setTimeout(() => {
html2canvas(target, {
useCORS: true,
backgroundColor: getComputedStyle(document.body).backgroundColor,
scrollY: -window.scrollY,
scrollX: -window.scrollX,
windowWidth: document.documentElement.scrollWidth,
windowHeight: document.documentElement.scrollHeight
}).then(canvas => {
const link = document.createElement("a");
link.download = "full-page-screenshot.png";
link.href = canvas.toDataURL("image/png");
link.click();
screenshotBtn.style.visibility = "visible"; // restore button
}).catch(err => {
console.error("Screenshot failed:", err);
alert("Screenshot failed. See console.");
screenshotBtn.style.visibility = "visible";
});
}, 100);
});