-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
227 lines (197 loc) · 5.31 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Twitch Chat Display</title>
<style>
* { box-sizing: border-box; }
html {
font: 125%/1.25 'KievitOT', system-ui, sans-serif;
font-weight: 500;
font-style: normal;
}
body {
margin: 0;
min-height: 100vh;
overflow: hidden;
display: flex;
align-items: flex-end;
mask-image: linear-gradient(to top, black 90%, transparent);
}
#chat {
display: table;
table-layout: fixed;
width: 100%;
position: absolute;
bottom: 0;
}
#chat > div {
display: table-row;
opacity: 1;
transition: opacity 1s;
}
#chat > div > * {
outline: 0px solid #f0f;
padding: 0.25em;
}
#chat .name {
display: table-cell;
width: 40%;
max-width: 14ch;
text-align: right;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
#chat .message {
display: table-cell;
word-wrap: break-word;
}
#chat .emoticon {
display: inline-block;
width: 1.4em;
height: 1.4em;
vertical-align: -20%;
object-fit: contain;
}
.badges {
display: inline-flex;
gap: 0.2em;
margin-right: 0.2em;
vertical-align: -13%;
}
.badge {
display: inline-block;
width: 1em;
height: 1em;
}
.badge img {
display: block;
width: 100%;
height: auto;
}
</style>
</head>
<body>
<div id="chat">
<!--<div class="line">
<div class="name">x</div>
<div class="message">y</div>
</div>-->
</div>
<script src="./tmi.min.js"></script>
<script>
/* CONFIG */
const channel = 'amaz'; // the channel to read
const ignoreUsers = ['nightbot', 'streamelements'];
const hideAfter = 90; // hide messages after X seconds
/* END CONFIG */
// load badges
let badgeImages = [];
fetch('https://api.twitchinsights.net/v1/badges/global')
.then((response) => response.json())
.then(function (data) {
badgeImages = data.badges;
console.info('badge data loaded');
});
//connect
const username = 'justinfan' + Math.floor(Math.random()*998+1);
console.info('loggin in as', username);
client = new tmi.client({
'connection': {
'reconnect': true,
'secure': true
},
'identity': {
'username': username,
'password': ''
},
'channels': [channel]
});
// handle messages
const chatEle = document.querySelector('#chat');
client.on('message', function (channel, user, message, fromSelf) {
if (message.startsWith('!')) return false;
if (ignoreUsers.includes(user['username'])) return false;
const badges = getBadges(user);
const color = (user['color']) ? user['color'] : 'currentColor';
const msg = formatEmotes(message, user.emotes);
let line = '<div class="line" data-ts="'+user['tmi-sent-ts']+'">';
line += '<div class="name">';
line += badges;
line += '<span style="color:'+color+';">';
line += user['display-name'];
line += '</span>';
line += '</div>';
line += '<div class="message">'+msg+'</div>';
line += '</div>';
chatEle.insertAdjacentHTML('beforeend', line);
});
client.connect();
// periodically hide old messages
setInterval(function () {
const now = Date.now();
document.querySelectorAll('#chat .line').forEach(function (ele, i) {
const ts = parseInt(ele.getAttribute('data-ts'));
if (now > (ts+hideAfter*1000)) {
ele.style.opacity = '0'; // fade out
setTimeout(function (line) { // finally remove element
line.remove();
}, 5000, ele);
}
});
}, 2000);
function getBadges(user) {
let bdg = '';
if (user['badges'] && badgeImages.length > 0) {
for (const [key, value] of Object.entries(user['badges'])) {
const badgeData = badgeImages.find(function (o) {
return o.setID === key;
});
const url = badgeData.imageURL.slice(0,-2) + '/3';
bdg += '<span class="badge badge-'+key+'" data-v="'+value+'"><img src="'+url+'" /></span>';
}
}
return '<span class="badges">'+bdg+'</span>';
}
// https://gist.github.com/AlcaDesign/742d8cb82e3e93ad4205
function formatEmotes(text, emotes) {
var splitText = text.split('');
for(var i in emotes) {
var e = emotes[i];
for(var j in e) {
var mote = e[j];
if(typeof mote == 'string') {
mote = mote.split('-');
mote = [parseInt(mote[0]), parseInt(mote[1])];
var length = mote[1] - mote[0],
empty = Array.apply(null, new Array(length + 1)).map(function() { return '' });
splitText = splitText.slice(0, mote[0]).concat(empty).concat(splitText.slice(mote[1] + 1, splitText.length));
splitText.splice(mote[0], 1, '<img class="emoticon" src="http://static-cdn.jtvnw.net/emoticons/v1/' + i + '/3.0">');
}
}
}
return htmlEntities(splitText).join('')
}
function htmlEntities(html) {
function it() {
return html.map(function(n, i, arr) {
if(n.length == 1) {
return n.replace(/[\u00A0-\u9999<>\&]/gim, function(i) {
return '&#'+i.charCodeAt(0)+';';
});
}
return n;
});
}
var isArray = Array.isArray(html);
if(!isArray) {
html = html.split('');
}
html = it(html);
if(!isArray) html = html.join('');
return html;
}
</script>
</body>
</html>