Skip to content

Commit

Permalink
Add battery extension (#504)
Browse files Browse the repository at this point in the history
  • Loading branch information
GarboMuffin authored Jun 3, 2023
1 parent 9bea485 commit 4eb0b94
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 0 deletions.
136 changes: 136 additions & 0 deletions extensions/battery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
(function (Scratch) {
'use strict';

/** @type {Promise<BatteryManager>|null} */
let getBatteryPromise = null;
/** @type {BatteryManager|null} */
let cachedBattery = null;
/** @type {boolean} */
let batteryError = false;
const withBattery = (callback) => {
// Getting the BatteryManager is async the first time. Usually it's very fast, but we shouldn't assume that it is.
// All the logic here lets us return values immediately when we have already got the battery instead of forcing
// a delay by returning a promise.
if (!navigator.getBattery || batteryError) {
return callback(null);
}
if (cachedBattery) {
return callback(cachedBattery);
}
if (!getBatteryPromise) {
getBatteryPromise = navigator.getBattery()
.then(battery => {
getBatteryPromise = null;
cachedBattery = battery;

cachedBattery.addEventListener('chargingchange', () => {
Scratch.vm.runtime.startHats('battery_chargingChanged');
});
cachedBattery.addEventListener('levelchange', () => {
Scratch.vm.runtime.startHats('battery_levelChanged');
});
cachedBattery.addEventListener('chargingtimechange', () => {
Scratch.vm.runtime.startHats('battery_chargeTimeChanged');
});
cachedBattery.addEventListener('dischargingtimechange', () => {
Scratch.vm.runtime.startHats('battery_dischargeTimeChanged');
});

return cachedBattery;
})
.catch(error => {
getBatteryPromise = null;
console.error('Could not get battery', error);
batteryError = true;
return null;
});
}
return getBatteryPromise.then(battery => {
return callback(battery);
});
};

// Try to get the battery immediately so that event blocks work.
withBattery(() => {});

class BatteryExtension {
getInfo () {
return {
name: 'Battery',
id: 'battery',
blocks: [
{
opcode: 'charging',
blockType: Scratch.BlockType.BOOLEAN,
text: 'charging?'
},
{
opcode: 'level',
blockType: Scratch.BlockType.REPORTER,
text: 'battery level'
},
{
opcode: 'chargeTime',
blockType: Scratch.BlockType.REPORTER,
text: 'seconds until charged'
},
{
opcode: 'dischargeTime',
blockType: Scratch.BlockType.REPORTER,
text: 'seconds until empty'
},
{
opcode: 'chargingChanged',
blockType: Scratch.BlockType.HAT,
text: 'when charging changed',
isEdgeActivated: false
},
{
opcode: 'levelChanged',
blockType: Scratch.BlockType.HAT,
text: 'when battery level changed',
isEdgeActivated: false
},
{
opcode: 'chargeTimeChanged',
blockType: Scratch.BlockType.HAT,
text: 'when time until charged changed',
isEdgeActivated: false
},
{
opcode: 'dischargeTimeChanged',
blockType: Scratch.BlockType.HAT,
text: 'when time until empty changed',
isEdgeActivated: false
},
]
};
}
charging () {
return withBattery(battery => {
if (!battery) return true;
return battery.charging;
});
}
level () {
return withBattery(battery => {
if (!battery) return 100;
return battery.level * 100;
});
}
chargeTime () {
return withBattery(battery => {
if (!battery) return 0;
return battery.chargingTime;
});
}
dischargeTime () {
return withBattery(battery => {
if (!battery) return Infinity;
return battery.dischargingTime;
});
}
}

Scratch.extensions.register(new BatteryExtension());
})(Scratch);
3 changes: 3 additions & 0 deletions images/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,6 @@ All images in this folder are licensed under the [GNU General Public License ver
- Created by [@True-Fantom](https://scratch.mit.edu/users/TrueFantom/) in https://github.com/TurboWarp/extensions/pull/498.
- File icons based on https://icon-icons.com/icon/file-pdf/153412 under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/).
- Background based on https://bgjar.com/contour-line under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/).

## battery.svg
- Created by [@Martinelplayz](https://github.com/Martinelplayz) in https://github.com/TurboWarp/extensions/pull/504#issuecomment-1574243161
1 change: 1 addition & 0 deletions images/battery.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions website/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,12 @@
<p>Details about the user's browser and operating system.</p>
</div>

<div class="extension">
<%- banner('battery') %>
<h3>Battery</h3>
<p>Access information about the battery of phones or laptops. May not work on all devices and browsers.</p>
</div>

<div class="extension">
<%- banner('mdwalters/notifications') %>
<h3>Notifications</h3>
Expand Down

0 comments on commit 4eb0b94

Please sign in to comment.