From c300125c9468f425d735e550853793ff3b85df0c Mon Sep 17 00:00:00 2001 From: Redume Date: Sun, 21 Jul 2024 21:06:49 +0300 Subject: [PATCH] I wrote the js code to change the image --- static/script/wallpaperLoad.js | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 static/script/wallpaperLoad.js diff --git a/static/script/wallpaperLoad.js b/static/script/wallpaperLoad.js new file mode 100644 index 0000000..9462bcd --- /dev/null +++ b/static/script/wallpaperLoad.js @@ -0,0 +1,37 @@ +function load(date) { + fetch(`https://api.starlio.space/wallpaper/${date}`).then(res => { + res.json().then(data => { + console.log(data); + if (data.length === 0) return; + + document.querySelector("div.wallpaper img").src = data.url; + document.querySelector("div.wallpaper div.wallpaper-text div.wallpaper-desc h1").innerHTML = data.title; + document.querySelector(".copyright").innerHTML = `Image Credit & Copyright: ${data.copyright}`; + document.querySelector(".desc").innerHTML = data.explanation; + }); + }); +} + +function parseURL() { + const pathname = document.location.pathname + + if (isNaN(new Date(pathname).getTime())) return null; + if (!isValidDate(pathname)) return null; + + return Number(pathname); +} + + +/* THX, bro <3 + https://stackoverflow.com/a/35413963/20781634 + */ +function isValidDate(dateString) { + var regEx = /^\d{4}-\d{2}-\d{2}$/; + if(!dateString.match(regEx)) return false; + var d = new Date(dateString); + var dNum = d.getTime(); + if(!dNum && dNum !== 0) return false; + return d.toISOString().slice(0,10) === dateString; +} + +load(parseURL()); \ No newline at end of file