Code
Browser Web APIs
Allows web applications to redirect audio output to a permitted Bluetooth headset, speakerphone, or other device.
Provides a way for browsers to better manage audio sessions and inform browsers about what kind of audio is being played.
Provides a method for managing downloads that may take a significant amount of time, running in the background if the user navigates away.
Enables web apps to defer tasks to be run in a service worker once the user has a stable network connection.
Allows scheduling of non-critical work to be performed during idle periods via requestIdleCallback.
Allows installed web apps to set a badge on the app icon to notify users of pending activity such as messages or updates.
Detects linear and two-dimensional barcodes, including QR codes, in images and video streams.
Provides information about the battery charge level of the device and notifications when the level or status changes.
Sends asynchronous, non-blocking requests to a server without expecting a response, ideal for analytics and diagnostics.
Enables simple communication between browsing contexts such as tabs, windows, and iframes sharing the same origin.
Battery Status API
Provides information about the battery charge level of the host device and fires events when the level or charging state changes. Useful for adapting app behaviour — such as reducing network activity or disabling resource-intensive features — when battery is low.
Key Interface — BatteryManager
navigator.getBattery()— Returns aPromise<BatteryManager>.BatteryManager.charging—trueif the battery is currently charging.BatteryManager.level— Battery level as a value between0.0and1.0.BatteryManager.chargingTime— Seconds until fully charged, or0if already full.BatteryManager.dischargingTime— Seconds until battery is empty, orInfinityif charging.
Battery Events
| Event | Fires when |
|---|---|
chargingchange | The charging state changes. |
levelchange | The battery level changes. |
chargingtimechange | The estimated charge time changes. |
dischargingtimechange | The estimated discharge time changes. |
Example — Read battery status
const battery = await navigator.getBattery();
console.log('Charging:', battery.charging);
console.log('Level:', Math.round(battery.level * 100) + '%');
console.log('Charging time:', battery.chargingTime, 'seconds');
console.log('Discharging time:', battery.dischargingTime, 'seconds');Example — Adapt behaviour on low battery
Listen for level changes and reduce polling or disable animations when the battery drops below a threshold.
const battery = await navigator.getBattery();
function checkBattery() {
if (!battery.charging && battery.level < 0.2) {
console.warn('Low battery — entering power-save mode');
reduceSyncFrequency();
pauseBackgroundAnimations();
}
}
battery.addEventListener('levelchange', checkBattery);
battery.addEventListener('chargingchange', checkBattery);
checkBattery(); // run immediately on load