Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

maybe show popup if mobile is in lower power mode #304

Open
evvvritt opened this issue Aug 14, 2024 · 1 comment
Open

maybe show popup if mobile is in lower power mode #304

evvvritt opened this issue Aug 14, 2024 · 1 comment

Comments

@evvvritt
Copy link
Collaborator

from claude

Yes, it's possible to detect if a device is in low power mode using JavaScript, but the implementation depends on the specific platform and browser. Here's how you can do it for different scenarios:

  1. For iOS devices (Safari browser):
if ('deviceLowPower' in navigator) {
  navigator.deviceLowPower.addEventListener('change', (event) => {
    console.log('Low Power Mode:', event.lowPower);
  });

  navigator.deviceLowPower.query().then((state) => {
    console.log('Current Low Power Mode:', state.lowPower);
  });
}
  1. For Android devices:
    Currently, there's no direct API to detect low power mode on Android using JavaScript. However, you can use the Battery Status API to infer if the device might be in a power-saving state:
if ('getBattery' in navigator) {
  navigator.getBattery().then((battery) => {
    console.log('Battery level:', battery.level);
    console.log('Is charging:', battery.charging);
    
    battery.addEventListener('levelchange', () => {
      console.log('Battery level changed:', battery.level);
    });
    
    battery.addEventListener('chargingchange', () => {
      console.log('Charging status changed:', battery.charging);
    });
  });
}
  1. For desktop browsers:
    Desktop browsers don't typically have a low power mode, but you can still use the Battery Status API to get information about the battery status:
if ('getBattery' in navigator) {
  navigator.getBattery().then((battery) => {
    console.log('Battery level:', battery.level);
    console.log('Is charging:', battery.charging);
  });
}

Note that the Battery Status API is being deprecated in some browsers due to privacy concerns, so its availability may be limited.

Would you like me to explain or break down any part of this code?

@evvvritt
Copy link
Collaborator Author

duplicate of #275

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant