Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Alikberov
Last active March 13, 2023 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alikberov/1ce10b3c9a6e23525bded8c2c750f638 to your computer and use it in GitHub Desktop.
Save Alikberov/1ce10b3c9a6e23525bded8c2c750f638 to your computer and use it in GitHub Desktop.
Battery Status
<!DOCTYPE html>
<html lang='en'>
<head>
<meta name='author' content='Alikberov' />
<meta name='copyright' content='Alikberov' />
<meta name='creator' content='Alikberov' />
<meta name='description' content='Battery charging journal' />
<meta name='document-state' content='Static' />
<title>Battery Status</title>
<script>
var logs = ["Achtung!!!", "=========="];
function log(text) {
logs.push(`${(new Date().toTimeString()).split(" ")[0]}: ${text}`);
setTimeout(function() {
document.querySelector("pre").textContent = logs.join("\r\n");
window.localStorage.battery = logs.join("\r\n");
}, 0);
}
window.addEventListener("error", (event) => {
log(`${log.textContent}${event.type}: ${event.message}`);
console.log(event);
});
</script>
<script>
navigator.getBattery().then((battery) => {
function updateAllBatteryInfo() {
if("battery" in window.localStorage && window.localStorage.battery != "") {
logs = window.localStorage.battery.split(/\r?\n/);
log("Session continuing...");
} else
logs = [];
updateChargeInfo();
updateLevelInfo();
updateChargingInfo();
updateDischargingInfo();
}
updateAllBatteryInfo();
battery.addEventListener("chargingchange", () => {
updateChargeInfo();
});
function updateChargeInfo() {
log(`Battery is ${battery.charging ? "charging" : "discharging"}`);
}
battery.addEventListener("levelchange", () => {
updateLevelInfo();
});
function updateLevelInfo() {
log(`Battery level: ${battery.level * 100}%`);
}
battery.addEventListener("chargingtimechange", () => {
updateChargingInfo();
});
function updateChargingInfo() {
log(`Battery charging time: ${battery.chargingTime} seconds`);
}
battery.addEventListener("dischargingtimechange", () => {
updateDischargingInfo();
});
function updateDischargingInfo() {
log(`Battery discharging time: ${battery.dischargingTime} seconds`);
}
});
</script>
</head>
<body>
<button onclick='window.localStorage.battery=""; window.location.href=""'>CLEAR</button>
<pre>Oops!!!</pre>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment