What should I use instead of navigator.getBattery()

216 Views Asked by At

I am looking for a navigator.getBattery() alternative. What I want to achieve is, that when you have under 30% of battery and you're not charging, it tells you to go charge. Right now I have this:

navigator.getBattery().then(function(battery) {
    if (battery.level < 0.3 && battery.charging === false) {
        let goCharge = document.createElement('h1');
        goCharge.style.fontSize = '40px';
        goCharge.style.color = '#FF0101';
        goCharge.textContent = 'GO CHARGE YOUR DEVICE NOW!';
        document.body.appendChild(goCharge);
        document.title = 'GO CHARGE YOUR DEVICE NOW!';
        document.getElementById('ico').href = 'battery_alert.png';
        alert('GO CHARGE YOUR DEVICE NOW!');
        throw new Error('GO CHARGE YOUR DEVICE NOW!');
    }
});

The problem is, that Firefox (and Safari) doesn't support this. And while I was looking for a Firefox alternative, I found many sites (e.g. 1, 2) saying, that I should no longer use it. I couldn't find any other way to get the battery info.

If someone knows a solution, please share your idea with me.

Thank you in advance.

0

There are 0 best solutions below