Repository navigation
Expand file tree
/
Copy pathtimer.js
More file actions
129 lines (112 loc) · 3.21 KB
/
Copy pathtimer.js
File metadata and controls
129 lines (112 loc) · 3.21 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
//made by Unspoken
// github- https://github.com/G1ODd
(() => {
if (window.__testTimerHUDSetup) return;
window.__testTimerHUDSetup = true;
let initialized = false;
function initTimerHUD() {
if (initialized) return;
initialized = true;
// Info text
const info = document.createElement('div');
info.textContent = 'Delete = init | U = start/stop | I = reset';
Object.assign(info.style, {
position: 'fixed',
left: '10px',
bottom: '10px',
fontSize: '14px',
color: '#fff',
opacity: '0.8',
zIndex: 999999,
pointerEvents: 'none',
fontFamily: 'sans-serif',
});
document.body.appendChild(info);
// HUD top-left
const hud = document.createElement('div');
hud.id = 'test-timer-hud';
hud.textContent = '00:00.0';
Object.assign(hud.style, {
position: 'fixed',
top: '10px',
left: '10px',
padding: '8px 12px',
background: 'rgba(0, 0, 0, 0.75)',
color: '#0f0',
fontFamily: 'monospace',
fontSize: '20px',
borderRadius: '4px',
zIndex: 999999,
userSelect: 'none',
pointerEvents: 'none',
});
document.body.appendChild(hud);
let running = false;
let elapsedMs = 0;
let startTime = 0;
let intervalId = null;
function formatTime(ms) {
const totalSeconds = ms / 1000;
const minutes = Math.floor(totalSeconds / 60);
const seconds = Math.floor(totalSeconds % 60);
const tenths = Math.floor((ms % 1000) / 100);
return (
String(minutes).padStart(2, '0') +
':' +
String(seconds).padStart(2, '0') +
'.' +
tenths
);
}
function updateHUD() {
hud.textContent = formatTime(elapsedMs);
}
function startTimer() {
if (running) return;
running = true;
hud.style.color = '#0f0';
startTime = Date.now() - elapsedMs;
intervalId = setInterval(() => {
elapsedMs = Date.now() - startTime;
updateHUD();
}, 100);
}
function stopTimer() {
if (!running) return;
running = false;
hud.style.color = '#f00';
clearInterval(intervalId);
intervalId = null;
}
function resetTimer() {
elapsedMs = 0;
updateHUD();
if (running) {
startTime = Date.now();
}
}
// Keybinds active after HUD is initialized
window.addEventListener('keydown', (e) => {
const tag = (e.target && e.target.tagName) || '';
if (tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT') return;
if (e.code === 'KeyU') {
e.preventDefault();
running ? stopTimer() : startTimer();
} else if (e.code === 'KeyI') {
e.preventDefault();
resetTimer();
}
});
updateHUD();
}
// Press Delete to initialize HUD
window.addEventListener('keydown', (e) => {
if (initialized) return;
const tag = (e.target && e.target.tagName) || '';
if (tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT') return;
if (e.code === 'Delete') {
e.preventDefault();
initTimerHUD();
}
});
})();