Deploy APOD Button
Co-authored-by: Panos Triantafyllidis <panostriantafyllidis@users.noreply.github.com> Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
29
js/main.js
29
js/main.js
@@ -187,6 +187,35 @@ const main = function () {
|
||||
player.addEventListener("ended", playNextTrack);
|
||||
playNextTrack();
|
||||
|
||||
// APOD
|
||||
document.getElementById("apodButton").addEventListener("click", function () {
|
||||
window.open("https://apod.nasa.gov/apod/astropix.html", "_blank");
|
||||
});
|
||||
|
||||
document.getElementById("downloadApodButton").addEventListener("click", async function () {
|
||||
const apiKey = process.env.NASA_API_KEY;
|
||||
const apiUrl = `https://api.nasa.gov/planetary/apod?api_key=${apiKey}`;
|
||||
|
||||
try {
|
||||
const response = await fetch(apiUrl);
|
||||
const data = await response.json();
|
||||
|
||||
if (data.media_type === "image") {
|
||||
const a = document.createElement("a");
|
||||
a.href = data.hdurl || data.url;
|
||||
a.download = data.title.replace(/[^a-zA-Z0-9]/g, "_") + ".jpg";
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
} else {
|
||||
alert("The APOD for today is not an image.");
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to fetch APOD data:", error);
|
||||
alert("Failed to download the APOD image. Please try again later.");
|
||||
}
|
||||
});
|
||||
|
||||
// Animation
|
||||
let simulationSpeed = 1;
|
||||
|
||||
|
Reference in New Issue
Block a user