add twitch API stuff, add follow/sub/cheer labels
This commit is contained in:
parent
8a7210ad56
commit
626540e728
17 changed files with 1143 additions and 3 deletions
10
twitch/public/labels.css
Normal file
10
twitch/public/labels.css
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
body {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
#label {
|
||||
margin: 16px;
|
||||
font-family: "Inter", sans-serif;
|
||||
font-size: 64px;
|
||||
font-weight: bold;
|
||||
}
|
||||
60
twitch/public/labels.js
Normal file
60
twitch/public/labels.js
Normal 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();
|
||||
Loading…
Add table
Add a link
Reference in a new issue