3

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.

thisistemporary
  • 102
  • 1
  • 9
  • 2
    doubt you will find it since most browsers did not implement or removed it due to security reasons. – epascarello Sep 01 '22 at 16:51
  • 1
    So does that mean, that there is **no** intended, not deprecated way to get the battery level? – thisistemporary Sep 01 '22 at 17:59
  • 2
    Safari never supported because Apple want to prevent web-apps from becoming too powerful (forcing devs to create native apps). That Firefox dropped it is . . . And the Mozilla team is wondering why Firefox adoption is dropping. The only future I see is that you have a local server providing you that via native APIs or a new spec that requests a permission like you have for location or notifications. – TomDK Feb 01 '23 at 02:12

0 Answers0