-
32 |
- Power Source 33 |
- --- 34 | 35 |
- Level percentage 36 |
- --- 37 | 38 |
- Fully charged in 39 |
- --- 40 | 41 |
- Remaining time 42 |
- --- 43 |
├── src ├── bolt.png ├── index.js └── styles.css ├── README.md ├── LICENSE └── index.html /src/bolt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/inputsh/battery/HEAD/src/bolt.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Battery Status API 2 | 3 | > Battery Status API Demo 4 | 5 | ## Demo 6 | 7 | [https://r3bl.me/battery/](https://r3bl.me/battery/) 8 | 9 | ## Screenshot 10 | 11 |  12 | 13 | ## Compatible with 14 | - Chrome 38+ 15 | - Chrome for Android 16 | - Firefox 31+ 17 | - Firefox for Android 18 | - Opera 25+ 19 | - Opera Mobile 25+ 20 | 21 | ## Not compatible with 22 | - Safari 23 | - Safari Mobile 24 | - IE Mobile 25 | - Internet Explorer 26 | - Chrome for iOS 27 | - Android Browser 28 | 29 | ## Specs 30 | 31 | [http://www.w3.org/TR/battery-status](http://www.w3.org/TR/battery-status) 32 | 33 | ## Maintained by 34 | 35 | - Aleksandar Todorović (Frontender & Web standards lover) 36 | - email: [aleksandar.todorovic@mail.ru](mailto:aleksandar.todorovic@mail.ru) 37 | - Twitter: [@r3bl_](http://twitter.com/r3bl_) 38 | - Web: [https://r3bl.me](http://r3bl.me) 39 | 40 | ## License 41 | Licensed under the MIT license. 42 | 43 | Copyright (c) 2015 [Aleksandar Todorović](https://r3bl.me). 44 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Guille Paz 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | (function() { 2 | 'use strict'; 3 | 4 | var battery; 5 | 6 | function toTime(sec) { 7 | sec = parseInt(sec, 10); 8 | 9 | var hours = Math.floor(sec / 3600), 10 | minutes = Math.floor((sec - (hours * 3600)) / 60), 11 | seconds = sec - (hours * 3600) - (minutes * 60); 12 | 13 | if (hours < 10) { hours = '0' + hours; } 14 | if (minutes < 10) { minutes = '0' + minutes; } 15 | if (seconds < 10) { seconds = '0' + seconds; } 16 | 17 | return hours + ':' + minutes; 18 | } 19 | 20 | function readBattery(b) { 21 | battery = b || battery; 22 | 23 | var percentage = parseFloat((battery.level * 100).toFixed(2)) + '%', 24 | fully, 25 | remaining; 26 | 27 | if (battery.charging && battery.chargingTime === Infinity) { 28 | fully = 'Calculating...'; 29 | } else if (battery.chargingTime !== Infinity) { 30 | fully = toTime(battery.chargingTime); 31 | } else { 32 | fully = '---'; 33 | } 34 | 35 | if (!battery.charging && battery.dischargingTime === Infinity) { 36 | remaining = 'Calculating...'; 37 | } else if (battery.dischargingTime !== Infinity) { 38 | remaining = toTime(battery.dischargingTime); 39 | } else { 40 | remaining = '---'; 41 | } 42 | 43 | document.styleSheets[0].insertRule('.battery:before{width:' + percentage + '}', 0); 44 | document.querySelector('.battery-percentage').innerHTML = percentage; 45 | document.querySelector('.battery-status').innerHTML = battery.charging ? 'Adapter' : 'Battery'; 46 | document.querySelector('.battery-level').innerHTML = percentage; 47 | document.querySelector('.battery-fully').innerHTML = fully; 48 | document.querySelector('.battery-remaining').innerHTML = remaining; 49 | 50 | } 51 | 52 | if (navigator.battery) { 53 | readBattery(navigator.battery); 54 | 55 | } else if (navigator.getBattery) { 56 | navigator.getBattery().then(readBattery); 57 | 58 | } else { 59 | document.querySelector('.not-support').removeAttribute('hidden'); 60 | } 61 | 62 | window.onload = function () { 63 | battery.addEventListener('chargingchange', function() { 64 | readBattery(); 65 | }); 66 | 67 | battery.addEventListener("levelchange", function() { 68 | readBattery(); 69 | }); 70 | }; 71 | }()); 72 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | 8 |
9 | 10 |Your browser doesn't support the Battery Status API :(
23 |