forked from mathiasbynens/css-dbg-stories
-
Notifications
You must be signed in to change notification settings - Fork 1
/
idle-detector.html
39 lines (39 loc) · 1.45 KB
/
idle-detector.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
<!DOCTYPE html>
<meta http-equiv="origin-trial" content="AguTVwSEBWefpr8EO7VDG/hIsTlPnejeHfFx7VUmYHm88rSuXnind5pGFWAsdk9YBPp7wE6AuPPUUwzgLrqoKwEAAABeeyJvcmlnaW4iOiJodHRwczovL21hdGhpYXNieW5lbnMuZ2l0aHViLmlvOjQ0MyIsImZlYXR1cmUiOiJJZGxlRGV0ZWN0aW9uIiwiZXhwaXJ5IjoxNTk4MDEwNTAwfQ==">
<title>Demo</title>
<div id="status"></div>
<div id="error"></div>
<script>
function log(target, msg) {
document.getElementById(target).appendChild(document.createTextNode(msg));
document.getElementById(target).appendChild(document.createElement('br'));
}
async function main() {
const hasPermission = await navigator.permissions.query({ name: 'notifications' });
if (hasPermission.state !== 'granted') {
const result = await Notification.requestPermission();
if (result !== 'granted') {
log('status', 'Notifications permission not granted.');
return;
}
}
try {
const controller = new AbortController();
const signal = controller.signal;
const idleDetector = new IdleDetector({
threshold: 60000,
signal,
});
idleDetector.addEventListener('change', (e) => {
const userState = idleDetector.userState;
const screenState = idleDetector.screenState;
log('status', `Idle change: ${userState}, ${screenState}.`);
});
await idleDetector.start();
log('status', 'Idle detector started');
} catch (e) {
log('error', e.message);
}
}
main();
</script>