-
Notifications
You must be signed in to change notification settings - Fork 5
/
wakelock.html
62 lines (56 loc) · 1.6 KB
/
wakelock.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
<html>
<head>
<title>Wake Lock Test</title>
<style>
.time {
font-style: italic;
}
</style>
</head>
<body>
<p>Click a button below to acquire or release a wake lock.</p>
<p><button id="screen">Screen</button> <span id="screen_status"></span></p>
<p><button id="system">System</button> <span id="system_status"></span></p>
<p>Log:</p>
<p id=log></p>
<script>
function log(message) {
const log = document.getElementById('log');
log.innerHTML += `<span class="time">${new Date().toUTCString()}</span>: ${message}<br>`;
}
[ 'screen', 'system' ].forEach(type => {
const button = document.getElementById(type);
const label = document.getElementById(type + '_status');
if ('wakeLock' in navigator) {
let lock;
button.addEventListener('click', async () => {
if (lock) {
log(`Release "${type}" button pressed.`);
lock.release();
return;
}
try {
log(`Acquire "${type}" button pressed.`);
lock = await navigator.wakeLock.request(type);
label.textContent = 'Acquired';
log(`"${type}" lock acquired.`);
lock.addEventListener('release', () => {
label.textContent = 'Released';
log(`"${type}" lock released.`);
lock = null;
});
} catch (e) {
label.textContent = `${e.name}: ${e.message}`;
log(`Caught ${e.name} acquiring "${type}" lock: ${e.message}`);
}
});
} else {
label.textContent = 'Not supported';
}
});
document.addEventListener('visibilitychange', () => {
log(`Document visibility changed to "${document.visibilityState}".`);
});
</script>
</body>
</html>