Open in a new tab

Ask for a screen wake lock with one button, release it with the same button, and report a browser without the API or a request it refuses.

Uses @awacloud/fw (Apache-2.0). This example's code is Apache-2.0.

When to use it

A page someone reads without touching it: a recipe, a slide show, a boarding pass, a dashboard on a wall screen. The screen must not dim while the tab is visible.

Copy-paste steps

  1. npm install @awacloud/fw
  2. Serve node_modules/@awacloud/fw/src/ at /vendor/fw/ (node serve.js from the examples root does this; any static server works).
  3. Copy index.html and main.js next to each other and open index.html through that server. The ../../_style/examples.css link is optional: copy that file along, or delete the link.

What it shows

  • wakeLock.isSupported() first: without the API the button is disabled and says why.
  • wakeLock.acquire() returns a handle with release() and onRelease(fn). The browser releases the lock itself when the tab is hidden, and fw takes it again when the tab comes back.
  • acquire() may be refused (a hidden tab, a low battery, a policy): the page logs the reason instead of leaving an unhandled rejection.

Code

main.js

javascriptApache-2.050 lines

// SPDX-License-Identifier: Apache-2.0
import fw from '@awacloud/fw';
import { wakeLock } from '@awacloud/fw/dom/lifecycle/wakeLock.js';

fw.runtime.registerAllDeep([wakeLock]);
const WAKE = fw.runtime.resolve('wakeLock');

/** @param {string} id @param {string} value */
const show = (id, value) => { /** @type {HTMLElement} */ (document.getElementById(id)).textContent = value; };
/** @param {string} line */
export function log(line) {
    const list = /** @type {HTMLElement} */ (document.getElementById('log'));
    list.appendChild(document.createElement('li')).textContent = line;
}

/** Wake lock: feature-detected, toggled by one button, released on hide by the API itself. */
export function setupWakeLock() {
    const button = /** @type {HTMLButtonElement} */ (document.getElementById('wake'));
    if (!WAKE.isSupported()) {
        button.disabled = true;
        show('wake-reason', 'Screen Wake Lock API not supported in this browser');
        return false;
    }
    button.disabled = false;
    /** @type {{ release: () => Promise<void>, onRelease: (fn: () => void) => void } | null} */
    let handle = null;
    const released = () => {
        handle = null;
        button.textContent = 'Keep the screen awake';
        log('wake lock released');
    };
    button.onclick = async () => {
        if (handle) {
            await handle.release();
            if (handle) released();
            return;
        }
        try {
            handle = await WAKE.acquire(); // the browser may refuse: a hidden tab, a low battery
        } catch (err) {
            log(`wake lock refused: ${/** @type {Error} */ (err).message}`);
            return;
        }
        handle.onRelease(released);
        button.textContent = 'Let the screen sleep';
        log('wake lock acquired');
    };
    return true;
}
export const wakeLockAvailable = setupWakeLock();

index.html

htmlApache-2.024 lines

<!doctype html>
<!-- SPDX-License-Identifier: Apache-2.0 -->
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>fw · Keep the screen awake</title>
  <link rel="stylesheet" href="../../_style/examples.css">
  <script type="importmap">
  { "imports": {
    "@awacloud/fw": "/vendor/fw/main.js", "@awacloud/fw/": "/vendor/fw/" } }
  </script>
  <script type="module" src="./main.js"></script>
</head>
<body>
  <main>
    <h1>Keep the screen awake</h1>
    <p>For a recipe, a slide show or a boarding pass: the screen stays on while the tab is visible.</p>
    <button id="wake" type="button">Keep the screen awake</button>
    <p id="wake-reason"></p>
    <ol id="log" aria-live="polite" aria-label="Wake lock log"></ol>
  </main>
</body>
</html>