-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
58 lines (48 loc) · 1.36 KB
/
script.js
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
if ('serviceWorker' in navigator)
navigator.serviceWorker.register('service-worker.js');
const html = document.querySelector('html');
const container = document.querySelector('.container');
const status = container.querySelector('.status');
const instructions = container.querySelector('.instructions');
const instructionsAction = instructions.querySelector('span');
const message = container.querySelector('.message');
let wakeLock = null;
const states = {
'active': {
status: 'Active',
instructionAction: 'deactivate'
},
'inactive': {
status: 'Inactive',
instructionAction: 'activate'
},
'idle': {
status: 'Idle',
instructionAction: 'activate'
}
}
function setState(state, messageText) {
const stateObject = states[state];
if (!stateObject)
throw new Error('Invalid state');
status.innerText = stateObject.status;
html.style.background = `var(--${state}-colour)`;
instructionsAction.innerText = stateObject.instructionAction;
message.innerText = messageText ?? '';
}
document.addEventListener('click', async () => {
try {
if (wakeLock)
return wakeLock.release();
wakeLock = await navigator.wakeLock.request('screen');
setState('active');
wakeLock.addEventListener('release', () => {
wakeLock = null;
setState('inactive');
});
} catch (err) {
wakeLock = null;
setState('idle', `${err.name}, ${err.message}`);
}
});
html.click();