-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathytchat.html
84 lines (81 loc) · 3.36 KB
/
ytchat.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
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
<script defer>
const channelName = 'alexankitty';//
const chatSocket = new WebSocket('ws://127.0.0.1:8081')
const forward = 'alexankitty'
const params = {channelName: channelName, forward: forward} //If you want to use a livestream ID or channel ID instead to test with, change channelName to Id. Remove forward if you don't want it sending messages to your twitch chat.
chatSocket.onmessage = function(message){
console.log(message)
if(message.data == 'Connected') {
console.log(message.data);
return;
}
switch(message.data){
case "400":
console.log("Bad request: Live stream may be unavailable")
//setTimeout(startChat, 5000) //Retrying after 5 seconds.
return;
break;
case "401":
console.log("Malformed data received by server.")
chatSocket.close();
return;
break;
case "404":
console.log("Livechat not found, server will retry in 5 seconds.")
return;
case "503":
console.log("Too many connections, closing");
chatSocket.close();
return;
break;
}
const parsedMsg = JSON.parse(message.data.toString());//I don't want to know why this has to be moved to a string even though it's already a string.
const author = parsedMsg.author;
const messageArr = parsedMsg.message;//this is wrapped not sure why
console.log(parsedMsg.message)
const chatDiv = document.createElement('div');
const meta = document.createElement('span');
chatDiv.appendChild(meta);
if(author.thumbnail) {
const pfp = document.createElement('img');
pfp.src = author.thumbnail.url;
meta.appendChild(pfp)
}
if(author.badge) {
const badge = document.createElement('img');
badge.src = author.badge.thumbnail.url;
meta.appendChild(badge)
}
meta.innerHTML += author.name
const messageContent = document.createElement('span');
for(let i = 0; i < messageArr.length; i++) {
for(key in messageArr[i]) {
if(key == 'text') {
let msgPart = document.createElement('span');
msgPart.innerText = messageArr[i][key]
messageContent.appendChild(msgPart);
}
if(key == 'url') {
let msgEmote = document.createElement('img');
msgEmote.src = messageArr[i][key];
messageContent.appendChild(msgEmote);
}
}
}
chatDiv.appendChild(messageContent);
document.body.appendChild(chatDiv);
}
chatSocket.onopen = function() {
startChat()
}
function startChat() {
chatSocket.send(JSON.stringify(params));
}
</script>
</html>