const labelEl = document.getElementById("label"); const label = location.pathname.split("/twitch/labels/")[1]; const labelLatestFollower = "latest-follower" const labelLatestSubscriber = "latest-subscriber" const labelLatestCheer = "latest-cheer" const allowedLabels = [ labelLatestFollower, labelLatestSubscriber, labelLatestCheer, ] /** * @param {string} label */ async function sync() { if (label == null) { const exampleUrl = `${location.protocol}//${location.host}/twitch/labels/${labelLatestFollower}`; labelEl.innerHTML = "Needs label to listen to, " + `such as ${labelLatestFollower}`; console.error("Needs label to listen to, such as " + exampleUrl); return; } let eventSource; try { eventSource = new EventSource("/twitch/sse/" + label); } 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 => { console.log(`[${label}] ${event.data}`); labelEl.innerText = event.data; }); eventSource.addEventListener("open", () => { console.log("Connected."); }); } sync();