Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pstadler/battery.js
A tiny wrapper for the HTML5 Battery Status API.
https://github.com/pstadler/battery.js
battery browser html5 javascript library
Last synced: 6 days ago
JSON representation
A tiny wrapper for the HTML5 Battery Status API.
- Host: GitHub
- URL: https://github.com/pstadler/battery.js
- Owner: pstadler
- License: mit
- Created: 2012-11-09T16:40:19.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2023-02-17T15:23:12.000Z (over 1 year ago)
- Last Synced: 2024-10-03T09:31:48.668Z (about 1 month ago)
- Topics: battery, browser, html5, javascript, library
- Language: JavaScript
- Homepage: https://pstadler.dev/battery.js
- Size: 27.3 KB
- Stars: 118
- Watchers: 11
- Forks: 25
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Battery.js [![NPM version][npm-version-image]][npm-url] [![NPM downloads][npm-downloads-image]][npm-url] [![MIT License][license-image]][license-url]
A tiny JavaScript wrapper for the [HTML5 Battery Status API](http://www.w3.org/TR/battery-status/).
~~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
```shell
$ npm install battery.js
```## Usage
```JavaScript
var Battery = require('battery.js'); // or similarfunction 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.dev/battery.js](http://pstadler.dev/battery.js)
[npm-url]: https://npmjs.com/package/battery.js
[npm-version-image]: https://img.shields.io/npm/v/battery.js.svg?style=flat-square
[npm-downloads-image]: https://img.shields.io/npm/dm/battery.js.svg?style=flat-square[license-url]: https://github.com/pstadler/battery.js/blob/master/LICENSE
[license-image]: https://img.shields.io/npm/l/battery.js.svg?style=flat-square