-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatWatcher.js
48 lines (38 loc) · 1.27 KB
/
ChatWatcher.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* globals _ */
import logger from "./logger"
class ChatWatcher {
static processedMessageClass = "chat-sound-enchantment-processed"
static newMessageSelector = `.chat-line__message:not(.${ChatWatcher.processedMessageClass})`
static bellSoundUrl =
"https://raw.githubusercontent.com/ledestin/twitch-chat-sound-enchantment/main/sounds/bell-candle-damper.mp3"
static soundDelay = 3000
constructor(twitch) {
this.twitch = twitch
this.debouncedPlayBell = _.debounce(this.playBell, ChatWatcher.soundDelay, {
leading: true,
trailing: true,
})
this.bell = new Audio(ChatWatcher.bellSoundUrl)
}
watchChatAndPlayBellOnNewMessages() {
if (!this.twitch.isOnMyChannel()) return
const newMessages = this.fetchNewMessages()
if (newMessages.length === 0) return
this.markNewMessagesAsProcessed(newMessages)
logger.debug(`${newMessages.length} new message(s) found`)
this.debouncedPlayBell()
}
// private
fetchNewMessages() {
return document.querySelectorAll(ChatWatcher.newMessageSelector)
}
markNewMessagesAsProcessed(newMessages) {
newMessages.forEach((message) => {
message.classList.add(ChatWatcher.processedMessageClass)
})
}
playBell = () => {
this.bell.play()
}
}
export default ChatWatcher