-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
143 lines (123 loc) · 3.19 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="manifest" href="manifest.json">
<title>Countdown</title>
<style>
html {
font-size: 24px;
}
</style>
</head>
<body>
<div id="end"></div>
<div id="cd"></div>
<script>
const final = '2021-07-23';
const finalDate = new Date(final);
const $end = document.querySelector('#end');
const $cd = document.querySelector('#cd');
const start = Date.now();
const end = finalDate.getTime();
const diff = end - start;
const days = Math.floor(diff / (1000 * 60 * 60 * 24));
$end.innerHTML = final;
$cd.innerHTML = `${days} days`;
//=== Service Workers
(function() {
if ('serviceWorker' in navigator) {
/**
* Show service worker status
* @param {boolean} status true if sw is active
*/
const swUIStatus = status => {
console.log('sw status', !!status);
};
/**
* Show service worker has been installed for the first time ever
*/
const swUIFirstTime = () => {
// console.log('sw first time ever');
swUIStatus(true);
swUIMessage('This app is now available offline!');
};
/**
* Show service worker has been installed
*/
const swUIInstalled = () => {
// console.log('sw installed');
swUIStatus(true);
};
/**
* Show that service worker has a new update to show
*/
const swUIUpdate = () => {
// console.log('sw there is a new update, please refresh');
swUIMessage('This app has an update, please refresh.');
};
/**
* Show service worker has returned an error
* @param {Object} err error
*/
const swUIError = err => {
console.error('sw registration failed: ', err);
};
/**
* Change the sw message
* @param {string} msg
*/
const swUIMessage = (msg = '') => {
console.log(msg);
};
/**
* Check if service worker is active
* @returns {boolean}
*/
const swCheckStatus = () => {
return !!navigator.serviceWorker.controller;
};
let isSWInstalled = false;
const onStateChange = newWorker => {
// console.log('onStateChange', newWorker.state);
if (newWorker.state === 'activated') {
if (!isSWInstalled) {
isSWInstalled = swCheckStatus();
swUIFirstTime();
} else {
swUIInstalled();
}
} else if (
newWorker.state === 'installed' &&
navigator.serviceWorker.controller
) {
swUIUpdate();
}
};
if (true) {
isSWInstalled = swCheckStatus();
if (isSWInstalled) {
swUIInstalled();
}
navigator.serviceWorker
.register('sw.js')
.then(registration => {
registration.addEventListener('updatefound', () => {
const newWorker = registration.installing;
registration.installing.addEventListener(
'statechange',
() => onStateChange(newWorker)
);
});
})
.catch(err => {
swUIError(err);
});
}
}
})();
</script>
</body>
</html>