Battery Api
How do I make an battery api?
Https://www.w3.org/TR/battery-status/There is no state in Firefox to detect a fully charged and power-supplied state. Unfortunately it made the Chrome version a bit weaker as well. Safari does not support the Api.. What is a battery api? How do you make a battery api? This script and codes were developed by André Michelle on 18 October 2022, Tuesday.
Battery Api - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Battery Api</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div battery> <h1>Battery Api</h1> <svg width="320" height="144"> <rect outline x="4" y="4" rx="4" ry="4" width="296" height="136" style="fill:transparent;stroke:black;stroke-width:8" /> <rect cap x="304" y="46" width="16" height="48" style="fill:black;" /> <path id="power" d="M256.6,82.9L153.1,32.2c-1.9-0.9-3.4-0.3-3.4,1.3v25.1c0,1.6-1.5,2.2-3.4,1.3L61.6,20.5c-1.9-0.9-2.2-0.7-0.6,0.5l122.5,90.8c1.6,1.2,2.8,0.8,2.8-0.8l-0.4-19.9c0-1.6,1.4-2.2,3.3-1.3l67.5,33.8c1.9,0.9,3.4,0.4,3.4-1.3V87.5C260,85.9,258.5,83.8,256.6,82.9z" /> <rect id="level" x="16" y="16" width="272" height="112" /> </svg> <div> <span mode></span><span time></span> </div>
</div> <script src="js/index.js"></script>
</body>
</html>
Battery Api - Script Codes CSS Codes
html,
body { margin: 0; font-size: 13px; font-family: Open Sans;
}
div[battery] { top: 50%; left: 50%; transform: translateX(-50%) translateY(-50%); position: absolute;
}
div[battery] h1 { font-size: 16px; margin: 0 0 4px 4px;
}
div[battery] span[mode] { margin-left: 4px; font-weight: bold; color: #333;
}
div[battery] span[time] { margin-left: 8px; color: #111;
}
Battery Api - Script Codes JS Codes
console.clear();
var chargeRect = document.querySelector("div[battery] svg rect#level");
var chargePower = document.querySelector("div[battery] svg path#power");
var labelMode = document.querySelector("div[battery] span[mode]");
var labelTime = document.querySelector("div[battery] span[time]");
var run = function(battery) { var timeToString = function(time) { if (time == Infinity) return "Calculating time..."; if (time < 60) return "less than a minute"; var minutes = Math.floor(time / 60); var minutesRest = minutes % 60; var minutesString = 1 == minutesRest ? "one minute" : minutesRest + " minutes"; if (minutes < 60) return 1 == minutes ? "one minute" : minutesString; var hours = Math.floor(minutes / 60); return (1 == hours ? "one hour" : hours + " hours") + " and " + minutesString; } var update = function() { if (battery.charging) { // Chrome keeps saying charging when charging // is actually completed but power is still supplied, // That makes complete sense! if (0 == battery.chargingTime) { labelMode.textContent = "Charged"; labelTime.textContent = ""; } else { labelMode.textContent = "Charging"; labelTime.textContent = timeToString(battery.chargingTime); } chargeRect.setAttribute("visibility", "hidden"); chargePower.setAttribute("visibility", "visible"); } else { if (1 == battery.level && battery.dischargingTime == Infinity) { // Hack for Firefox which should let charging being // true when charged and power supplied. // Best might be another field: 'power-supplied' labelMode.textContent = "Charged"; labelTime.textContent = ""; } else { labelMode.textContent = "Remaining"; labelTime.textContent = timeToString(battery.dischargingTime); chargeRect.setAttribute("visibility", "visible"); chargeRect.width.baseVal.value = battery.level * 272; chargePower.setAttribute("visibility", "hidden"); } } console.log(battery); } battery.onchargingchange = battery.onchargingtimechange = battery.ondischargingtimechange = battery.onlevelchange = update; update();
};
if (navigator.battery) run(navigator.battery);
else if (navigator.getBattery) navigator.getBattery().then(run);
else { labelMode.textContent = "Not supported :(";
}
Developer | André Michelle |
Username | andremichelle |
Uploaded | October 18, 2022 |
Rating | 4 |
Size | 2,899 Kb |
Views | 20,240 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
Calculating Pi with Virtual Darts | 2,389 Kb |
Loop Mixing | 2,646 Kb |
Geolocation Api | 2,322 Kb |
Web Audio Api Equalizer | 2,606 Kb |
Dub Techno Chords | 3,909 Kb |
Code 7 | 2,837 Kb |
Noise monotone increasing | 2,174 Kb |
Stern-Brocot Sequence | 2,631 Kb |
Web Audio Api HardLimiter | 2,697 Kb |
Web Audio Api Convolver | 3,023 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
A Pen by Alexandru Pora | Axpro | 1,615 Kb |
Vanilla JS - A toggleClass function | Woodwork | 2,532 Kb |
Polo, the flying squirrel | Agbales | 2,445 Kb |
Another brick in the wall | Fivera | 1,955 Kb |
Mobile Nav Menu | AliKlein | 4,745 Kb |
React JS Movie Info App | MTushar | 4,870 Kb |
Old calculator | Gnarfugh | 2,815 Kb |
Box-sizing | Elad2412 | 1,572 Kb |
Growing Root - Scroll control - CANVAS | Cjonasw | 2,342 Kb |
Basic template | Tomcat | 1,675 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!