add twitch API stuff, add follow/sub/cheer labels

This commit is contained in:
ari melody 2026-07-24 03:54:29 +01:00
parent 8a7210ad56
commit 626540e728
Signed by: ari
GPG key ID: 60B5F0386E3DDB7E
17 changed files with 1143 additions and 3 deletions

60
twitch/public/labels.js Normal file
View file

@ -0,0 +1,60 @@
const labelEl = document.getElementById("label");
const selectedLabel = new URLSearchParams(location.search).get("l");
const labelLatestFollower = "latest-follower"
const labelLatestSubscriber = "latest-subscriber"
const labelLatestCheer = "latest-cheer"
const allowedLabels = [
labelLatestFollower,
labelLatestSubscriber,
labelLatestCheer,
]
/**
* @param {string} selectedLabel
*/
async function sync() {
if (selectedLabel == null) {
const exampleUrl =
location.pathname +
"?l=" +
labelLatestFollower;
labelEl.innerHTML =
"Needs label to listen to, " +
`such as <a href="${exampleUrl}">${labelLatestFollower}</a>`;
console.error("Needs label to listen to, such as " + exampleUrl);
return;
}
let eventSource;
try {
eventSource = new EventSource("/twitch/sse?l=" + selectedLabel);
} catch (err) {
console.error("Failed to connect to event source", err);
return;
}
eventSource.addEventListener("error", () => {
eventSource.close();
console.error("Connection lost, or an error has occurred.");
console.log("Attempting to reconnect...");
// TODO: stop reconnecting after 10 failed attempts
setTimeout(() => {
sync();
}, 1000);
});
eventSource.addEventListener("update", event => {
console.log(event.data);
labelEl.innerText = event.data;
});
eventSource.addEventListener("open", () => {
console.log("Connected.");
});
}
sync();