-
Notifications
You must be signed in to change notification settings - Fork 0
/
gum_visiblefocus.html
37 lines (32 loc) · 1.1 KB
/
gum_visiblefocus.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
<html>
<meta charset=utf-8>
<div id="div"></div>
<script>
const console = { log: msg => div.innerHTML += msg + "<br>" };
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
window.onfocus = () => console.log("FOCUS");
window.onblur = () => console.log("BLUR");
document.onvisibilitychange = () => console.log(document.visibilityState.toUpperCase());
const info = () => `${document.hasFocus()? "has" : "no "} focus, ${document.visibilityState}`;
(async (seconds = 5) => {
try {
console.log(`Waiting ${seconds} seconds to start, in case you want to tab away.`);
await wait(seconds * 1000);
while (true) {
console.log(info());
const p = navigator.mediaDevices.getUserMedia({video: true});
console.log(`Waiting ${seconds} seconds for camera access...`);
const stream = await Promise.race([p, wait(seconds * 1000)]);
console.log(`...Camera access ${stream? "GRANTED" : "DELAYED"}`);
console.log(info());
if (stream) {
stream.getTracks()[0].stop();
}
await wait(1000);
}
} catch(e) {
console.log(e);
}
})();
</script>
</html>