admin管理员组文章数量:1434900
What should I use instead of navigator.getBattery(), as it is not working?
I am working on a chrome extension and with this what I want to do is -
function toggleBatterySaver(enable) {
if (enable) {
navigator.getBattery().then(battery => {
if (battery.level < 0.2) {
chrome.power.requestKeepAwake("system");
}
});
} else {
chrome.power.releaseKeepAwake();
}
}
It is throwing an error: navigator.getBattery() is not a function.
What should I use instead of navigator.getBattery(), as it is not working?
I am working on a chrome extension and with this what I want to do is -
function toggleBatterySaver(enable) {
if (enable) {
navigator.getBattery().then(battery => {
if (battery.level < 0.2) {
chrome.power.requestKeepAwake("system");
}
});
} else {
chrome.power.releaseKeepAwake();
}
}
It is throwing an error: navigator.getBattery() is not a function.
Share Improve this question asked Nov 17, 2024 at 12:50 baranwalayushbaranwalayush 11 bronze badge 3 |1 Answer
Reset to default 0You can try to start with a minimal chrome extension that uses navigator.battery()
:
index.html:
<!DOCTYPE html>
<html>
<head><title>Battery Test</title></head>
<body><div id="bat">-</div></body>
<script src="script.js"></script>
</html>
script.js:
navigator.getBattery().then(battery => {
document.getElementById("bat").innerHTML = "Battery: " + battery.level;
});
manifest.json:
{
"name": "Battery Test",
"version": "1.0.0",
"description": "Battery Test",
"manifest_version": 3,
"author": "Markus",
"action":{
"default_popup": "index.html",
"default_title": "Battery Test"
}
}
Edit
In a service worker context the navigator object is of type WorkerNavigator
that is missing the battery
property among others. One way to get around this is to open an offscreen document.
manifest.json (set permission "offscreen"):
{
"name": "Battery Test",
"version": "1.0.0",
"description": "Battery Test",
"manifest_version": 3,
"action":{
"default_title": "Battery Test"
},
"background": {
"service_worker": "background.js"
},
"permissions": ["offscreen"]
}
background.js (create the offscreen document "offscree.html"):
chrome.runtime.onInstalled.addListener(() => {
chrome.offscreen.createDocument({
url: 'offscreen.html',
reasons: ['BATTERY_STATUS'],
justification: 'read battery status'
});
});
offscreen.html (runs your script):
<!DOCTYPE html>
<script src="script.js"></script>
script.js:
navigator.getBattery().then(battery => {
console.log("Battery: " + battery.level);
});
If you need the battery status in the background process itself you can find features to send messages from the offscreen page to your worker and vice versa in the documention linked above.
本文标签: javascriptAlternative for navigatorgetBattery() of battery apiStack Overflow
版权声明:本文标题:javascript - Alternative for navigator.getBattery() of battery api? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745634725a2667486.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
navigator.battery()
and print out the currentbattery level
. It seems, we need more debugging information from your side. I will post my minimal, (hopefully) reproducible example and would like to ask you to test it and share your observations. – Markus Commented Nov 17, 2024 at 20:47