-
Notifications
You must be signed in to change notification settings - Fork 3
/
ChatboxBot.html
330 lines (282 loc) · 9.25 KB
/
ChatboxBot.html
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<title>Twitch-Chatbox-Bot</title>
<style>
@import url(https://fonts.googleapis.com/css?family=Roboto:700);
* {
box-sizing: border-box;
}
/*****************************************************
* *
* Change this to make the animation faster or slower *
* *
******************************************************/
:root {
--fade-in-duration: .3s;
--fade-out-delay: 15s;
--fade-out-duration: .5s;
}
@keyframes fadeInRight {
0% {
opacity: 0;
transform: translateX(600px);
}
100% {
opacity: 1;
transform: translateX(0);
}
}
@keyframes fadeOut {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}
html {
height: 100%;
overflow: hidden;
text-shadow: 0 0 1px #000, 0 0 2px #000;
background: none;
font-family: 'Roboto';
font-weight: 700;
font-size: 1em;
line-height: 1.5em;
color: white;
}
#chatlog {
display: table;
position: absolute;
bottom: 0;
left: 0;
padding: 0 10px 10px;
width: 100%;
table-layout: fixed;
}
#chatlog>div {
display: table-row;
}
#chatlog>div {
animation: fadeInRight var(--fade-in-duration) ease forwards, fadeOut var(--fade-out-duration) ease var(--fade-out-delay) forwards;
}
#chatlog>div.deleted {
visibility: hidden;
}
#chatlog .emote {
background-repeat: no-repeat;
background-position: center;
background-size: contain;
padding: 0.4em 0.2em;
position: relative;
}
#chatlog .emote img {
display: inline-block;
height: 1em;
}
#chatlog .message,
#chatlog .meta {
vertical-align: top;
display: table-cell;
padding-bottom: 0.1em;
}
#chatlog .meta {
width: 35%;
text-align: right;
padding-right: 0.5em;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
#chatlog .message {
word-wrap: break-word;
width: 65%;
}
#chatlog .badge {
display: inline-block;
margin-right: 0.2em;
position: relative;
height: 1em;
vertical-align: middle;
top: -0.1em;
}
#chatlog .name {
margin-left: 0.2em;
}
</style>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="chatlog"></div>
<template id="chatmessage">
<div>
<span class="meta">
<span class="badges"></span>
<span class="name"></span>
</span>
<span class="message"></span>
</div>
</template>
<script src="https://github.com/tmijs/tmi.js/releases/download/v1.8.4/tmi.min.js"></script>
<script type="module">
// Define configuration options
const opts = {
/****************************************************
* *
* Change this to your channel name *
* *
*****************************************************/
channels: ["CHANNEL_NAME"]
}
// Use Channel name from HASH
const hash = window.location.hash
if(hash) opts.channels[0] = hash
/*
* CSS Time to Milliseconds
* by Jake Bellacera (http://jakebellacera.com)
* ============================================
*/
function css_time_to_milliseconds(time_string) {
const num = parseFloat(time_string, 10)
let unit = time_string.match(/m?s/)
if (unit) unit = unit[0]
switch (unit) {
case "s": // seconds
return num * 1000
case "ms": // milliseconds
return num
default:
return 0
}
}
/*function escapeRegex(string) { //Unneeded, keeping for preservation
return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
}*/
const chatlogNode = document.querySelector("#chatlog")
const msgTemplate = document.querySelector("#chatmessage")
const css = getComputedStyle(document.documentElement);
const messageFadeOutDelay = getComputedStyle(document.documentElement).getPropertyValue('--fade-out-delay')
const messageFadeOutDuration = getComputedStyle(document.documentElement).getPropertyValue('--fade-out-duration')
const messageRemovalTimeMs = css_time_to_milliseconds(messageFadeOutDelay) + css_time_to_milliseconds(messageFadeOutDuration) + 100
const namecolors = [
"Blue",
"Coral",
"DodgerBlue",
"SpringGreen",
"YellowGreen",
"Green",
"OrangeRed",
"Red",
"GoldenRod",
"HotPink",
"CadetBlue",
"SeaGreen",
"Chocolate",
"BlueViolet",
"Firebrick"
]
let badgeSetsChannel = null
let badgeSetsGlobal = null
fetch("https://badges.twitch.tv/v1/badges/global/display")
.then(res => res.json())
.then(json => badgeSetsGlobal = json.badge_sets)
// TMI Handlers:
const onConnectedHandler = (host, port) => console.info(`Connected to ${host}:${port}`)
const onConnectErrorHandler = error => chatlog.innerHTML = `Failed to connect! Error: ${error}`
const onMessageHandler = (channel, context, msg, self) => {
const messageNode = msgTemplate.content.cloneNode(true)
/*
* USERNAME
*/
const nameNode = messageNode.querySelector(".name")
const displayname = context["display-name"]
nameNode.innerHTML = displayname
if (context["color"] !== null) {
nameNode.style.color = context["color"]
} else {
nameNode.style.color = namecolors[displayname.charCodeAt(displayname.length - 1) % namecolors.length]
}
/*
* BADGES
*/
const badgesNode = messageNode.querySelector(".badges")
const badges = context["badges"]
// HACK: get channel-id w/o AUTH (Thx Twitch API)
if (badgeSetsChannel === null)
fetch(`https://badges.twitch.tv/v1/badges/channels/${context['room-id']}/display`)
.then(res => res.json())
.then(json => badgeSetsChannel = json.badge_sets)
for (const badge in badges) {
try {
// Prepare badge stuff
const badgeVersion = badges[badge]
let urls = badgeSetsGlobal[badge].versions[badgeVersion]
if (!urls) urls = badgeSetsChannel[badge].versions[badgeVersion]
// Add badge to message
const badgeImg = document.createElement("img")
badgeImg.className = "badge"
badgeImg.src = urls.image_url_1x
badgesNode.appendChild(badgeImg)
} catch (error) {
console.error('Failed to ADD badge', badge, 'to message', msg, '! Error:', error)
}
}
/*
* EMOTES
*/
try {
let msgWithEmotes = msg.split(" ");
let fragment = "";
let messageNodeClass = messageNode.querySelector(".message")
const emotes = context["emotes"]
for(let i = 0; i < msgWithEmotes.length; i++) {
let emoteChecked = false;
for (const emote in emotes) {
const firstEmoteOccurance = emotes[emote][0].split("-")
const emoteString = msg.substring(parseInt(firstEmoteOccurance[0]), parseInt(firstEmoteOccurance[1]) + 1)
if(emoteString == msgWithEmotes[i]){
const emoteImgSrc = `https://static-cdn.jtvnw.net/emoticons/v2/${emote}/default/dark/1.0`
if(fragment !== ""){
let text = document.createElement('span');
text.innerText = fragment;
messageNodeClass.appendChild(text);
fragment = ""; //dump the text fragment because we need to append a new span
}
const emoteImg = `<img src="${emoteImgSrc}" alt="${emoteString}">`
const emoteSpan = document.createElement('span');
emoteSpan.className = "emote"
emoteSpan.innerHTML = emoteImg;
messageNodeClass.appendChild(emoteSpan);
emoteChecked = true;
}
}
if(!emoteChecked){
fragment += `${msgWithEmotes[i]} `
}
if(i == msgWithEmotes.length - 1) { // arrays start at 0
let text = document.createElement('span');
text.innerText = fragment;
messageNodeClass.appendChild(text);
}
}
} catch (error) {
messageNodeClass.innerText = msg
}
// Add message
chatlogNode.appendChild(messageNode)
// Remove message after fade out animation
setTimeout(() => chatlogNode.firstChild.remove(), messageRemovalTimeMs)
}
const client = new tmi.Client(opts)
// Register event handlers:
client.on('message', onMessageHandler)
client.on('connected', onConnectedHandler)
// Connect to Twitch:
client.connect().catch(onConnectErrorHandler)
</script>
</body>
</html>