Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
16 lines (11 sloc) 502 Bytes

Access battery information from a page

Although not common, in some specific scenarios it can be useful to know if the battery level is going too low. We can get this information this way:

navigator.getBattery().then((battery) => {
    console.log(`${battery.level * 100}%`);
    
    battery.addEventListener('levelchange', () => {
        console.log(`${battery.level * 100}%`);					
    });
});

Sadly, at the time of this writing this only works in Chrome, Firefox and Opera.