Skip to content
A tiny wrapper for the HTML5 Battery Status API.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.eslintrc
.gitignore
LICENSE
README.md
battery.js
battery.min.js
package.json

README.md

Battery.js NPM version NPM downloads MIT License

A tiny JavaScript wrapper for the HTML5 Battery Status API.

As of October 2015 the Battery Status API is supported by Firefox, Chrome, Opera, Android Browser and Chrome for Android: http://caniuse.com/battery-status

Due to privacy concerns, support for the Battery Status API has been dropped from most browsers. See: https://caniuse.com/#feat=battery-status.

Install

$ npm install battery.js

Usage

var Battery = require('battery.js'); // or similar

function logBatteryStatus(status) {
  console.log('Level: ' + Math.floor(status.level * 100) + '%'); // 30%
  console.log('Charging: ' + status.charging);                   // true
  console.log('Time until charged: ' + status.chargingTime);     // 3600 (seconds) or Infinity
  console.log('Battery time left: ' + status.dischargingTime);   // 3600 (seconds) or Infinity
}

// Get battery status as soon as it's available
Battery.getStatus(function(status, error) {
  if(error) {
    console.error('Battery status is not supported');
    return;
  }

  logBatteryStatus(status);
});

// Register a handler to get notified when battery status changes
Battery.onUpdate(logBatteryStatus);

Demo

pstadler.sh/battery.js

You can’t perform that action at this time.