Ive been trying to see if i can make my own "Api's" to use for snap tho im running into an issue. when i use the url block its just returning me the code i have on the page not the page value. if anyone has anything that could help please lmk. https://batteryapi1.glitch.me/
// Override the page content with JSON when the battery data is ready
navigator.getBattery().then(battery => {
const data = {
batteryLevel: battery.level * 100,
charging: battery.charging
};
// Set MIME type and write JSON (not standard, but may help some tools)
document.write(JSON.stringify(data));
document.close();
});
I see // Override the page content with JSON when the battery data is ready navigator.getBattery().then(battery => { const data = { batteryLevel: battery.level * 100, charging: battery.charging }; // Set MIME type and write JSON (not standard, but may help some tools) document.write(JSON.stringify(data)); document.close(); }); in snap, but
in the actual website. probably because it uses document.write though, which replaces the webpage data. I don't think there's anyway around this unless you turn on js, and in that case why use the url [ block at all.
Your issue is that the url block just gets what the browser gets before it renders or runs any javascript. You have to have the page be json from the start, aka, have the server send the json instead of html.
If you can't run code on the backend (not in the html file), then I suggest you don't try to make an api. If you can, you can try to search up "how to make a REST api" instead of asking here, since this forum is about snap, not any other language.
i made a node js at a point to put it as json but the problem was the snap block reported too early also i am doing it here because my code works its a matter of how the url block works
// Endpoint for Snap-like functionality
app.get("/api/snap", (req, res) => {
// Wait for 10 seconds before creating the response data
setTimeout(() => {
// Create the JSON data
const responseData = { result: true };
// Set the correct headers for JSON response
res.setHeader("Content-Type", "application/json");
// Send the response after the delay
res.end(JSON.stringify(responseData));
}, 1000); // 10-second delay
});
// Root route to prevent "Cannot GET /"
app.get("/", (req, res) => {
res.send("Welcome to the Snap API! Use /api/snap to see the delay.");
});
// Start the server
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(API is live on port ${port});
});
this is something i have been trying to see if i can get everything loaded before returning something but fore some reason it waits but still reports empty
thats not how the url block works
as already explained by @ego-lay_atman-bay the URL block returns the data that was sent by the server. it does not run any javascript.
you cannot get a devices battery from the backend. you can get it by running javascript on the frontend, however the URL block only returns the data sent from the server. it cannot execute code.
On the backend maybe, but you can't run js on the frontend if you're making an api. Basically, when making an api, no code can run on the frontend, it all has to run on the backend.