I searched the web and it provees that there's a way to get the battery using JavaScript. I tried doing one of the JavaScript codes on the web and it does not work. Can you prove that it possible to get the battery of computer in Snap!?
this returns a Promise
which will resolve to a BatteryManager
object which you can get the battery level
from but idk how to use Promise
s
Now, show the JavaScript function in a call block, for it to work.
this is the best i can do
it's a start though
It is possible to access the computers battery status using JavaScript but integrating this functionality directly into snap may require additional steps. You can use the JavaScript navigator.getBattery()
API to get battery information.
However, snap does not natively support JavaScript within its blocks you would need to either modify snap source code or use an external JavaScript interface to bridge between snap and the browsers battery API.
I think you can better understand through this example.
navigator.getBattery().then(function(battery) {
console.log("Battery charging: " + (battery.charging ? "Yes" : "No"));
console.log("Battery level: " + battery.level * 100 + "%");
console.log("Battery charging time: " + battery.chargingTime + " seconds");
console.log("Battery discharging time: " + battery.dischargingTime + " seconds");
// You can also set up event listeners to respond to changes in the battery status
battery.addEventListener('chargingchange', function() {
console.log("Battery charging: " + (battery.charging ? "Yes" : "No"));
});
battery.addEventListener('levelchange', function() {
console.log("Battery level: " + battery.level * 100 + "%");
});
});
I hope it help you.
???
here's a project with some block that might help (by @alessandrito123):
https://snap.berkeley.edu/snap/snap.html#present:Username=joecooldoo&ProjectName=Blocks%20Translation%20(Sensing)
They are a little janky sometimes but the one you want works fine. You might have to run the block a few times before it can get the battery %
ai-o-meter is going off the charts. i used chat gpt once to help with something to do with snap and js and it also said almost the exact same thing
I wrote some native JS code to do that. Let me find it...
// We get the initial value when the promise resolves ...
return (navigator.getBattery().then(function(battery) {
return (battery.level);
};
}))
Note: this might not work...
try this in a javascript block
if("getBattery" in navigator) {
var ide = this.parentThatIsA(IDE_Morph);
if(!("batteryAPI" in ide)) {
navigator.getBattery().then(b => {ide.batteryAPI = b;});
}
}
var ide = this.parentThatIsA(IDE_Morph);
try {
return ide.batteryAPI.level * 100;
}
catch {
return false;
}
code from @alessandrito123's blocks but turned into one combined script
if it returns false try again until it returns a number
you would have to use async/await, but snap does not handle promises in the javascript definitions of their blocks, so i would doubt that the javascript block handles promises