-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwakelock.js
More file actions
35 lines (30 loc) · 1.16 KB
/
Copy pathwakelock.js
File metadata and controls
35 lines (30 loc) · 1.16 KB
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
if ('wakeLock' in navigator && 'request' in navigator.wakeLock) {
let wakeLock = null;
const statusDiv = document.querySelector('#statusDiv');
const requestWakeLock = async () => {
try {
wakeLock = await navigator.wakeLock.request('screen');
wakeLock.addEventListener('release', (e) => {
console.log(e);
statusDiv.textContent = 'Wake lock was released';
console.log('Wake lock was released');
});
statusDiv.textContent = 'Wake lock is active; the screen saver will not turn on.';
console.log('Wake lock is active');
} catch (e) {
statusDiv.textContent = `${e.name}, ${e.message}`;
console.error(`${e.name}, ${e.message}`);
}
};
const handleVisibilityChange = () => {
if (wakeLock !== null && document.visibilityState === 'visible') {
requestWakeLock();
}
};
document.addEventListener('visibilitychange', handleVisibilityChange);
document.addEventListener('fullscreenchange', handleVisibilityChange);
requestWakeLock();
} else {
statusDiv.textContent = 'Wake lock API not supported; the screen saver will turn on.';
console.error('Wake lock API not supported.');
}