fix: correct Wake Lock API (window.WakeLock → navigator.wakeLock)
This commit is contained in:
+32
-13
@@ -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';
|
||||
wakeLockButton.addEventListener('click', requestWakeLock);
|
||||
document.body.appendChild(wakeLockButton);
|
||||
|
||||
wakeLock.addEventListener('release', () => {
|
||||
status.style.display = 'none';
|
||||
});
|
||||
} catch (err) {
|
||||
console.log('Wake Lock request failed:', err);
|
||||
// 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
|
||||
|
||||
Reference in New Issue
Block a user