lock wake for all browsers but firefox

This commit is contained in:
Sara
2023-09-26 15:33:26 +02:00
parent 98ddd64114
commit 1ce382a454
2 changed files with 36 additions and 0 deletions

28
www/app/lib/wakeLock.ts Normal file
View File

@@ -0,0 +1,28 @@
// Not possible yet in firefox
// https://bugzilla.mozilla.org/show_bug.cgi?id=1589554
const canWakeLock = () => "wakeLock" in navigator;
let wakelock: WakeLockSentinel | undefined;
async function lockWakeState() {
if (!canWakeLock()) return;
try {
wakelock = await navigator.wakeLock.request();
wakelock.addEventListener("release", () => {
console.log(
"Screen Wake State Locked:",
wakelock ? !wakelock?.released : false,
);
});
console.log("Screen Wake State Locked:", !wakelock.released);
} catch (e) {
console.error("Failed to lock wake state with reason:", e.message);
}
}
function releaseWakeState() {
if (wakelock) wakelock.release();
wakelock = undefined;
}
export { lockWakeState, releaseWakeState };