Skip to content

Instantly share code, notes, and snippets.

@FreePhoenix888
Last active February 25, 2024 16:09
Show Gist options
  • Save FreePhoenix888/e43f72912c5f1c13bfd4220c46196cca to your computer and use it in GitHub Desktop.
Save FreePhoenix888/e43f72912c5f1c13bfd4220c46196cca to your computer and use it in GitHub Desktop.
Find Repeating Chat Messages And Alarm
doAllWork();
/**
* Find repeating messages in the twitch chat every {@link checkIntervalInMs} milliseconds and play a sound at url {@link soundUrl} if there are more than {@link howManyMessagesMustBeUnique} unique messages in the last {@link howManyMessagesToCheck} and logs the messages if {@link shouldLog} is true and adds a stop button to the page
* @example
* ```
* workForMe()
* ```
*/
function doAllWork(
options = {
shouldLog: true,
soundUrl:
"https://cdn.pixabay.com/download/audio/2021/09/27/audio_91211934db.mp3?filename=clock-alarm-8761.mp3",
howManyMessagesToCheck: 50,
howManyMessagesMustBeUnique: 25,
checkIntervalInMs: 10 * 1000,
}
) {
const {
shouldLog,
soundUrl,
howManyMessagesToCheck,
howManyMessagesMustBeUnique,
checkIntervalInMs,
} = options;
checkUniqueMessages(options);
const intervalId = setInterval(checkUniqueMessages, checkIntervalInMs);
addStopButton({ intervalId });
}
function checkUniqueMessages(
options = {
shouldLog: true,
howManyMessagesToCheck: 50,
howManyMessagesMustBeUnique: 25,
soundUrl:
"https://cdn.pixabay.com/download/audio/2021/09/27/audio_91211934db.mp3?filename=clock-alarm-8761.mp3",
}
) {
const {
shouldLog,
howManyMessagesToCheck,
howManyMessagesMustBeUnique,
soundUrl,
} = options;
const rightBarElement = document.body.querySelector(".right-column");
shouldLog && console.log({ rightBarElement });
const chatMessageElements = [
...rightBarElement.querySelectorAll(".text-fragment"),
];
shouldLog && console.log({ chatMessageElements });
const last20ChatMessageElements = chatMessageElements.slice(
-howManyMessagesToCheck
);
shouldLog && console.log({ last20ChatMessageElements });
const last20ChatMessages = last20ChatMessageElements.map(
(chatMessage) => chatMessage.textContent
);
shouldLog && console.log({ last20ChatMessages });
const uniqueMessagesSet = new Set(last20ChatMessages);
shouldLog && console.log({ uniqueMessagesSet });
shouldLog && console.log("-------------------");
if (uniqueMessagesSet.size < howManyMessagesMustBeUnique) {
var audio = new Audio(soundUrl);
audio.play();
}
}
function addStopButton(options = { intervalId: 30 * 1000 }) {
const { intervalId } = options;
const stopButton = document.createElement("button");
stopButton.textContent = "Stop";
stopButton.style.position = "fixed";
stopButton.style.top = "0";
stopButton.style.right = "0";
stopButton.style.padding = "34px";
stopButton.style.backgroundColor = "red";
stopButton.style.zIndex = "1000";
stopButton.onclick = () => {
clearInterval(intervalId);
stopButton.remove();
};
document.body.appendChild(stopButton);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment