-
Notifications
You must be signed in to change notification settings - Fork 5
/
idle.html
155 lines (146 loc) · 3.95 KB
/
idle.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
144
145
146
147
148
149
150
151
152
153
154
155
<html>
<head>
<!-- Token for https://reillyeon.github.io -->
<meta http-equiv="origin-trial" content="Ahfau0SLkJ0xfwytPWBLmQ0buhNOMAqgDpeHf+AgN0dFgNUUmgJDXv452ZUkFxPWgbaa7k5+GSOIKLLvJvfmNwAAAABaeyJvcmlnaW4iOiJodHRwczovL3JlaWxseWVvbi5naXRodWIuaW86NDQzIiwiZmVhdHVyZSI6IklkbGVEZXRlY3Rpb24iLCJleHBpcnkiOjE2MDUwNTI3OTl9">
<title>Idle detection demo</title>
<style>
#notSupported {
border: 1px solid black;
padding: 5px;
}
.error {
background-color: lightcoral;
}
.info {
background-color: aqua;
}
table {
border-collapse: collapse;
}
table td, table th {
border: 1px solid black;
padding: 3px;
}
table tr:first-child td, table tr:first-child th {
border-top: 0;
}
table tr td:first-child, table tr th:first-child {
border-left: 0;
}
table tr:last-child td, table tr:last-child th {
border-bottom: 0;
}
table tr td:last-child, table tr th:last-child {
border-right: 0;
}
.state {
font-family: monospace;
}
</style>
</head>
<body>
<p id="notSupported" class="error">
The Idle Detection API appears not to be supported in your browser. Please
ensure that you are running a recent version of Chrome and have enabled
<code>chrome://flags/#enable-experimental-web-platform-features</code> or a
similar option in other Chromium-based browsers.
</p>
<p>
<label for="threshold">Threshold (milliseconds):</label>
<input type="number" id="threshold" value="60000"></input>
<button id="start">Start</button>
<button id="requestPermission">Request Permission</button>
</p>
<table id="table">
<tr>
<th>Timestamp</th>
<th>User</th>
<th>Screen</th>
</tr>
</table>
<script>
if ('IdleDetector' in window) {
const warning = document.getElementById('notSupported');
warning.remove();
}
const table = document.getElementById('table');
function logMessage(message) {
const row = table.insertRow(1);
row.classList.add('info');
row.innerHTML = `
<td>${new Date().toUTCString()}</td>
<td colspan="2">${message}</td>
`;
}
function logState(state) {
const row = table.insertRow(1);
row.innerHTML = `
<td>${new Date().toUTCString()}</td>
<td class="state">${state.userState}</td>
<td class="state">${state.screenState}</td>
`;
}
function logError(error) {
const row = table.insertRow(1);
row.classList.add('error');
row.innerHTML = `
<td>${new Date().toUTCString()}</td>
<td colspan="2">${error.name}: ${error.message}</td>
`;
}
const thresholdInput = document.getElementById('threshold');
const startButton = document.getElementById('start');
let detector = null;
let controller = null;
startButton.addEventListener('click', async () => {
if (detector) {
try {
controller.abort();
controller = null;
detector = null;
} catch (error) {
logError(error);
}
logMessage('Idle detector stopped.');
startButton.textContent = 'Start';
} else {
try {
controller = new AbortController();
const options = {
threshold: parseInt(thresholdInput.value),
signal: controller.signal,
};
detector = new IdleDetector();
detector.addEventListener('change', () => {
logState(detector);
});
await detector.start(options);
logMessage('Idle detector started.');
startButton.textContent = 'Stop';
} catch (error) {
logError(error);
controller = null;
detector = null;
}
}
});
const permissionButton = document.getElementById('requestPermission');
permissionButton.addEventListener('click', async () => {
try {
const state = await IdleDetector.requestPermission();
logMessage(`Permission result: ${state}`);
} catch (error) {
logError(error);
}
});
async function main() {
const status = await navigator.permissions.query({name: 'idle-detection'});
logMessage(`Permission status: ${status.state}`);
status.addEventListener('change', () => {
logMessage(`Permission change: ${status.state}`);
});
}
main();
</script>
</body>
</html>