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.
$ npm install battery.js
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);