-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
157 lines (141 loc) · 5.14 KB
/
Copy pathscript.js
File metadata and controls
157 lines (141 loc) · 5.14 KB
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
// https://streamlabs.com/dashboard#/settings/api-settings API Settings -> API Tokens -> Your API Access Token
const token = "MY_API_TOKEN";
// https://streamlabs.com/dashboard#/settings/api-settings API Settings -> API Tokens -> Your Socket API Token
const socketToken = 'MY_SOCKET_API_TOKEN';
const labelsList = [
'most_recent_follower',
'most_recent_cheerer',
// 'most_recent_donator',
'most_recent_subscriber'
]
const imagesSRC = [
'images/follow.png',
'images/bits.png',
// 'images/donator.png',
'images/sub.png'
]
// Time in ms for timeOut
const TIMING = 5000;
//Connect to socket
const streamlabs = io(`https://sockets.streamlabs.com?token=${socketToken}`, {transports: ['websocket']});
//Perform Action on event
streamlabs.on("connect", () => {
console.log("Connected to socket");
});
streamlabs.on("disconnect", () => {
console.log("Disconnected from socket");
});
streamlabs.on("reconnect", () => {
console.log("Reconnected to socket");
refreshReconnect(0)
});
streamlabs.on('event', (eventData) => {
if (eventData.type == 'streamlabels') {
var json = eventData.message["data"];
console.log(json);
// Check if there is already an element with class
var labelNum = 0;
for (var i = 0; i < labelsList.length; i++) {
var labelName = labelsList[labelNum];
this[labelName] = json[`${labelName}`];
text = this[labelName];
if (text != "") {
// If Element have this class, modify the child element
if (document.querySelector("." + labelName) != null) {
console.log("Class already exist");
editLabel(text, labelName);
// Child element does not have same text
// if (document.querySelector("." + labelName).innerHTML.includes(text) == false) {
// document.querySelector("." + labelName).innerHTML = `<img src="${imagesSRC[labelNum]}">${text}`;
// }
}
else {
// We create the label
console.log("Creating label");
createLabel(text, labelNum, labelName);
}
}
labelNum++;
}
}
else {
console.log("Event without streamlabels type");
// console.log(eventData.type)
// console.log(eventData.message["data"])
}
});
function init(labelNum) {
fetch(`https://streamlabs.com/api/v5/stream-labels/files?token=${token}`)
.then(response=>response.json())
.then(data=> {
json = data['data'];
for (var i = 0; i < labelsList.length; i++) {
var labelName = labelsList[labelNum];
this[labelName] = json[`${labelName}`];
if (this[labelName] != "") {
text = this[labelName];
createLabel(text, labelNum, labelName);
}
labelNum++;
}
labelNum = 0;
setTimeout(loop, TIMING);
})
.catch(function (error) {
console.log("Error fetching data:", error);
// Restart
setTimeout(init,TIMING,labelNum)
});
}
function refreshReconnect(labelNum) {
fetch(`https://streamlabs.com/api/v5/stream-labels/files?token=${token}`)
.then(response=>response.json())
.then(data=> {
json = data['data'];
for (var i = 0; i < labelsList.length; i++) {
var labelName = labelsList[labelNum];
this[labelName] = json[`${labelName}`];
if (this[labelName] != "") {
text = this[labelName];
editLabel(text, labelName);
}
labelNum++;
}
labelNum = 0;
})
.catch(function (error) {
console.log("Error fetching data:", error);
// Restart
setTimeout(refreshReconnect, TIMING, labelNum);
});
}
function createLabel(text, labelNum, labelName){
// Create li element with labelName as class
let liItem = document.createElement("li");
liItem.classList.add(labelName);
// Create div container for text from StreamLabels
let textContainer = document.createElement("div");
textContainer.classList.add("textContainer")
textContainer.innerHTML = `${text}`;
liItem.appendChild(textContainer);
document.querySelector('ul').append(liItem);
// Create Images
addImages(labelNum, labelName);
}
function addImages(labelNum, labelName){
let digits = document.querySelector("." + labelName)
let imgItem = document.createElement('img');
imgItem.src = `${imagesSRC[labelNum]}`;
digits.prepend(imgItem);
}
function editLabel(text, labelName) {
let textContainer = document.querySelector("." + labelName + " .textContainer")
if (textContainer.innerHTML != `${text}`) {
textContainer.innerHTML = `${text}`;
}
}
function loop() {
document.querySelector("ul").append(document.querySelector("ul").firstElementChild)
setTimeout(loop, TIMING);
}
init(0);