2026-07-24 03:54:29 +01:00
|
|
|
const labelEl = document.getElementById("label");
|
|
|
|
|
|
2026-07-24 17:24:07 +01:00
|
|
|
const label = location.pathname.split("/twitch/labels/")[1];
|
2026-07-24 03:54:29 +01:00
|
|
|
const labelLatestFollower = "latest-follower"
|
|
|
|
|
const labelLatestSubscriber = "latest-subscriber"
|
|
|
|
|
const labelLatestCheer = "latest-cheer"
|
|
|
|
|
const allowedLabels = [
|
|
|
|
|
labelLatestFollower,
|
|
|
|
|
labelLatestSubscriber,
|
|
|
|
|
labelLatestCheer,
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
/**
|
2026-07-24 17:24:07 +01:00
|
|
|
* @param {string} label
|
2026-07-24 03:54:29 +01:00
|
|
|
*/
|
|
|
|
|
async function sync() {
|
2026-07-24 17:24:07 +01:00
|
|
|
if (label == null) {
|
|
|
|
|
const exampleUrl = `${location.protocol}//${location.host}/twitch/labels/${labelLatestFollower}`;
|
2026-07-24 03:54:29 +01:00
|
|
|
|
|
|
|
|
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 {
|
2026-07-24 17:24:07 +01:00
|
|
|
eventSource = new EventSource("/twitch/sse/" + label);
|
2026-07-24 03:54:29 +01:00
|
|
|
} 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...");
|
|
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
sync();
|
|
|
|
|
}, 1000);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
eventSource.addEventListener("update", event => {
|
2026-07-24 17:24:07 +01:00
|
|
|
console.log(`[${label}] ${event.data}`);
|
2026-07-24 03:54:29 +01:00
|
|
|
labelEl.innerText = event.data;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
eventSource.addEventListener("open", () => {
|
|
|
|
|
console.log("Connected.");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sync();
|