This commit is contained in:
parent
2d526f189b
commit
f91d315440
2 changed files with 67 additions and 51 deletions
64
website/scripts/status_cafe.ts
Normal file
64
website/scripts/status_cafe.ts
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
interface LatestStatus {
|
||||
username: string;
|
||||
status: string;
|
||||
title: string;
|
||||
emoji?: string;
|
||||
updated: Date;
|
||||
}
|
||||
|
||||
(async () => {
|
||||
function timeAgo(input: number | Date) {
|
||||
const date = input instanceof Date ? input : new Date(input);
|
||||
const formatter = new Intl.RelativeTimeFormat("en");
|
||||
const ranges = [
|
||||
["years", 3600 * 24 * 365],
|
||||
["months", 3600 * 24 * 30],
|
||||
["weeks", 3600 * 24 * 7],
|
||||
["days", 3600 * 24],
|
||||
["hours", 3600],
|
||||
["minutes", 60],
|
||||
["seconds", 1],
|
||||
] as const;
|
||||
const secondsElapsed = (date.getTime() - Date.now()) / 1000;
|
||||
|
||||
for (const [rangeType, rangeVal] of ranges) {
|
||||
if (rangeVal < Math.abs(secondsElapsed)) {
|
||||
const delta = secondsElapsed / rangeVal;
|
||||
return formatter.format(Math.round(delta), rangeType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const req = await fetch("https://status.cafe/users/sophie.atom");
|
||||
const parser = new DOMParser();
|
||||
const doc = parser.parseFromString(await req.text(), "text/xml");
|
||||
const latestEntryRaw = doc.querySelector("feed > entry");
|
||||
if (!latestEntryRaw) throw new Error("Missing latest entry!");;
|
||||
|
||||
const latestEntry: LatestStatus = {
|
||||
username: doc.querySelector("feed > author > name")?.textContent!,
|
||||
status: latestEntryRaw.querySelector("content")?.textContent!,
|
||||
title: latestEntryRaw.querySelector("title")?.textContent!,
|
||||
updated: new Date(latestEntryRaw.querySelector("updated")?.textContent!),
|
||||
};
|
||||
latestEntry.emoji = latestEntry.title
|
||||
.slice(latestEntry.username.length)
|
||||
.slice(0, -Math.min(latestEntry.status.length, 53))
|
||||
.trim();
|
||||
const statusElement = document.getElementById("status");
|
||||
if(!statusElement) throw new Error("Missing status element!");
|
||||
const h2 = document.createElement("h2");
|
||||
const a = document.createElement("a");
|
||||
const p = document.createElement("p");
|
||||
|
||||
a.href = "https://status.cafe/users/" + latestEntry.username;
|
||||
a.innerText = latestEntry.emoji + " " + latestEntry.username;
|
||||
h2.appendChild(a);
|
||||
h2.innerHTML += " - " + timeAgo(latestEntry.updated);
|
||||
|
||||
p.style.textAlign = "end";
|
||||
p.innerText = latestEntry.status;
|
||||
|
||||
statusElement.appendChild(h2);
|
||||
statusElement.appendChild(p);
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue