forked from TurboWarp/extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbattery.js
More file actions
143 lines (136 loc) · 4.38 KB
/
battery.js
File metadata and controls
143 lines (136 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Name: Battery
// ID: battery
// Description: Access information about the battery of phones or laptops. May not work on all devices and browsers.
// License: MIT AND MPL-2.0
(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: Scratch.translate("Battery"),
id: "battery",
color1: "#cf8436",
blocks: [
{
opcode: "charging",
blockType: Scratch.BlockType.BOOLEAN,
text: Scratch.translate("charging?"),
},
{
opcode: "level",
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate("battery level"),
},
{
opcode: "chargeTime",
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate("seconds until charged"),
},
{
opcode: "dischargeTime",
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate("seconds until empty"),
},
{
opcode: "chargingChanged",
blockType: Scratch.BlockType.EVENT,
text: Scratch.translate("when charging changed"),
isEdgeActivated: false,
},
{
opcode: "levelChanged",
blockType: Scratch.BlockType.EVENT,
text: Scratch.translate("when battery level changed"),
isEdgeActivated: false,
},
{
opcode: "chargeTimeChanged",
blockType: Scratch.BlockType.EVENT,
text: Scratch.translate("when time until charged changed"),
isEdgeActivated: false,
},
{
opcode: "dischargeTimeChanged",
blockType: Scratch.BlockType.EVENT,
text: Scratch.translate("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);