-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
54 lines (52 loc) · 2.72 KB
/
script.js
File metadata and controls
54 lines (52 loc) · 2.72 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
// When the DOM is "fully loaded and parsed"
document.addEventListener('DOMContentLoaded', event => {
const watchDiv = document.querySelector('div#stopwatchWatchDiv')
const lapsUl = document.querySelector('ul#stopwatchLapsUl')
const controlsUl = document.querySelector('ul#stopwatchControlsUl')
const dataControlsUl = document.querySelector('ul#stopwatchDataControlsUl')
// @ts-ignore
const stopwatch = new StopwatchApi(watchDiv, { controlsUl, dataControlsUl, lapsUl, keyboardControls: true })
stopwatch.removeEventListener('start', (startedTime, startedDate) => {
console.debug('start', `startedTime=${startedTime}<=>${TimeConverter.humanReadableTimeString(startedTime)}`, `startedDate=${startedDate}`)
})
stopwatch.removeEventListener('stop', (stoppedTime, stoppedDate) => {
console.debug('stop', `stoppedTime=${stoppedTime}<=>${TimeConverter.humanReadableTimeString(stoppedTime)}`, `stoppedDate=${stoppedDate}`)
})
stopwatch.removeEventListener('restart', () => { console.log('restart') })
stopwatch.removeEventListener('add_lap', (index, lapTimeInMs) => {
console.debug('add_lap', `index=${index}`, `lapTime=${lapTimeInMs}<=>${TimeConverter.humanReadableTimeString(lapTimeInMs)}`)
})
stopwatch.removeEventListener('remove_lap', (index, lapTimeInMs) => {
console.debug('remove_lap', `index=${index}`, `lapTime=${lapTimeInMs}<=>${TimeConverter.humanReadableTimeString(lapTimeInMs)}`)
})
stopwatch.removeEventListener('clear_laps', () => { console.debug('clear_laps') })
})
// PWA Support if possible (register service worker)
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('./sw.js')
.then((registration) => {
console.log('Service Worker registered successfully with scope:', registration.scope)
// Listen for updates to the service worker
registration.onupdatefound = () => {
const installingWorker = registration.installing;
if (installingWorker) {
installingWorker.onstatechange = () => {
if (installingWorker.state === 'installed') {
if (navigator.serviceWorker.controller) {
if (confirm('Service Worker detected new content, do you want to refresh?')) {
location.reload()
}
}
}
}
}
}
})
.catch((error) => {
console.error('Service Worker registration failed:', error)
})
})
} else {
console.warn('Service Workers are not supported')
}