fix: correct Wake Lock API (window.WakeLock → navigator.wakeLock)

This commit is contained in:
Ash
2026-04-08 08:39:44 -07:00
parent cd04f7f1cd
commit e07ab58f54
+33 -14
View File
@@ -224,8 +224,31 @@
<div class="wake-locked" id="wakeLockStatus">Screen locked for workout</div>
<script>
// Wake Lock API
if ('WakeLock' in window && 'request' in WakeLock) {
// Wake Lock API — correct API is navigator.wakeLock
let wakeLock = null;
async function requestWakeLock() {
try {
wakeLock = await navigator.wakeLock.request('screen');
const status = document.getElementById('wakeLockStatus');
status.style.display = 'block';
status.textContent = '🔒 Screen locked for workout';
wakeLock.addEventListener('release', () => {
status.style.display = 'none';
wakeLock = null;
});
} catch (err) {
console.log('Wake Lock request failed:', err);
// Fallback: show a note that wake lock isn't available
const status = document.getElementById('wakeLockStatus');
status.style.display = 'block';
status.textContent = '⚠️ Wake lock not available — keep screen awake manually';
status.style.background = 'rgba(100, 100, 100, 0.9)';
}
}
if ('wakeLock' in navigator) {
const wakeLockButton = document.createElement('button');
wakeLockButton.textContent = '🔒 Lock Screen';
wakeLockButton.style.cssText = `
@@ -240,21 +263,17 @@
font-size: 0.85rem;
cursor: pointer;
transition: background 0.2s;
z-index: 1000;
`;
wakeLockButton.addEventListener('click', async () => {
try {
const wakeLock = await window.WakeLock.request('screen');
const status = document.getElementById('wakeLockStatus');
status.style.display = 'block';
wakeLock.addEventListener('release', () => {
status.style.display = 'none';
});
} catch (err) {
console.log('Wake Lock request failed:', err);
wakeLockButton.addEventListener('click', requestWakeLock);
document.body.appendChild(wakeLockButton);
// Re-acquire wake lock after visibility change (e.g., tab switch)
document.addEventListener('visibilitychange', async () => {
if (wakeLock !== null && document.visibilityState === 'visible') {
await requestWakeLock();
}
});
document.body.appendChild(wakeLockButton);
}
// Smooth scroll behavior for anchor links