Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
109 lines (97 loc) · 3.52 KB
/
Copy pathscript.js
File metadata and controls
109 lines (97 loc) · 3.52 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
// https://streamlabs.com/dashboard#/settings/api-settings API Settings -> API Tokens -> Your Socket API Token
const socketToken = 'MY_SOCKET_API_TOKEN';
// https://streamlabs.com/dashboard#/settings/api-settings API Settings -> API Tokens -> Your API Access Token
const token = "MY_API_TOKEN";
// Use any of the filenames that Streamlabels would normally create
const labelName = 'session_top_donator';
//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()
});
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
this[labelName] = json[`${labelName}`];
text = this[labelName];
if (text != null) {
// If Element have this class, modify the child element
if (document.querySelector("." + labelName) != null) {
console.log("Class already exist");
editLabel(text, labelName);
}
else {
// We create the label
console.log("Creating label");
createLabel(text, labelName);
}
}
}
else {
console.log("Event without streamlabels type");
// console.log(eventData.type)
// console.log(eventData.message["data"])
}
});
function init() {
fetch(`https://streamlabs.com/api/v5/stream-labels/files?token=${token}`)
.then(response=>response.json())
.then(data=> {
json = data['data'];
this[labelName] = json[`${labelName}`];
if (this[labelName] != null) {
text = this[labelName];
createLabel(text, labelName);
}
})
.catch(function (error) {
console.log("Error fetching data:", error);
// Restart
setTimeout(init, 5000);
});
}
function refreshReconnect() {
fetch(`https://streamlabs.com/api/v5/stream-labels/files?token=${token}`)
.then(response=>response.json())
.then(data=> {
json = data['data'];
this[labelName] = json[`${labelName}`];
if (this[labelName] != null) {
text = this[labelName];
editLabel(text, labelName);
}
})
.catch(function (error) {
console.log("Error fetching data:", error);
// Restart
setTimeout(refreshReconnect, 5000);
});
}
function createLabel(text, 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);
}
function editLabel(text, labelName) {
let textContainer = document.querySelector("." + labelName + " .textContainer")
if (textContainer.innerHTML != `${text}`) {
textContainer.innerHTML = `${text}`;
}
}
init();