-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotator.js
197 lines (172 loc) · 6.18 KB
/
rotator.js
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
// Variables
let settings = {
"global": {
"internal_clock": 1000,
"stats_clock": 60000
},
"stats": {
"refresh_rate": 5 // Seconds
}
};
let state = {
"latest": {
"follower": {
"enabled": false,
"name": "",
"icon": "person",
"empty": ""
},
"subscriber": {
"enabled": false,
"name": "",
"amount": 0,
"icon": "person",
"empty": ""
},
"donation": {
"enabled": false,
"name": "",
"amount": 0,
"icon": "person",
"empty": ""
},
"cheer": {
"enabled": false,
"name": "",
"amount": 0,
"icon": "person",
"empty": ""
}}
};
let channelName = '';
let latest_keys;
let latest_pos = 0;
let currency = '';
// Start everything!
function start() {
// Generate stats_keys variable
latest_keys = Object.keys(state.latest);
// Live Viewers/Followers Refresh
// Start refresh clock
setInterval(function() {
// Latest
// Variables
let latestObject = state.latest[latest_keys[latest_pos]];
let latestType = latest_keys[latest_pos];
// fadeOut Current
$('.latest-container').fadeOut(function() {
// Genereate html
let text = "";
text += "<span class='text'>";
// Check if empty
if (latestObject.name == "") {
text += latestObject.empty + " <i class='fas fa-" + latestObject.icon + "'></i>";
} else {
// Check type
switch (latestType) {
case 'subscriber':
text += latestObject.name + " <span class='accent'>" + latestObject.amount + "" + ((latestObject.amount > 1) ? "x" : "x") + "</span>";
break;
case 'donation':
text += latestObject.name + " <span class='accent'>" + currency + latestObject.amount.toFixed(2) + "</span>";
break;
case 'cheer':
text += latestObject.name + " <span class='accent'> x" + latestObject.amount + "</span>";
break;
}
}
text += "</span>";
// Update
$('.latest-container').html(text);
// Fade in
$('.latest-container').fadeIn();
latest_pos++;
if (latest_pos == latest_keys.length) {
latest_pos = 0;
}
});
}, settings.stats.refresh_rate * 1000);
}
// On widget load, setup settings/state etc
window.addEventListener('onWidgetLoad', function(obj) {
console.log('ON WIDGET LOAD')
console.log(obj)
// Variables
let fieldData = obj.detail.fieldData;
currency = obj.detail.currency.symbol;
if (channelName == '') {
channelName = obj.detail.channel.username;
}
// Update settings
settings.stats.refresh_rate = fieldData.stats_refresh_rate;
// Latest
if (fieldData.latest_toggle_follower == "true") {
state.latest.follower.empty = fieldData.latest_follower_empty;
state.latest.follower.enabled = true;
state.latest.follower.icon = fieldData.followers_icon;
if (obj.detail.session.data["follower-latest"].name != "") {
state.latest.follower.name = obj.detail.session.data["follower-latest"].name;
}
} else {
delete state.latest.follower;
}
if (fieldData.latest_toggle_donation == "true") {
state.latest.donation.empty = fieldData.latest_donation_empty;
state.latest.donation.enabled = true;
state.latest.donation.icon = fieldData.donation_icon;
if (obj.detail.session.data["tip-session-top-donation"].name != "") {
state.latest.donation.name = obj.detail.session.data["tip-session-top-donation"].name;
state.latest.donation.amount = obj.detail.session.data["tip-session-top-donation"].amount;
}
} else {
delete state.latest.donation;
}
if (fieldData.latest_toggle_cheer == "true") {
state.latest.cheer.empty = fieldData.latest_cheer_empty;
state.latest.cheer.enabled = true;
state.latest.cheer.icon = fieldData.cheer_icon;
if (obj.detail.session.data["cheer-session-top-donation"].name != "") {
state.latest.cheer.name = obj.detail.session.data["cheer-session-top-donation"].name;
state.latest.cheer.amount = obj.detail.session.data["cheer-session-top-donation"].amount;
}
} else {
delete state.latest.cheer;
}
if (fieldData.latest_toggle_subscriber == "true") {
state.latest.subscriber.empty = fieldData.latest_subscriber_empty;
state.latest.subscriber.enabled = true;
state.latest.subscriber.icon = fieldData.subs_icon;
if (obj.detail.session.data["subscriber-latest"].name != "") {
state.latest.subscriber.name = obj.detail.session.data["subscriber-latest"].name;
state.latest.subscriber.amount = obj.detail.session.data["subscriber-latest"].amount;
}
} else {
delete state.latest.subscriber;
}
// Start Everything!
start();
});
// On session update
window.addEventListener('onSessionUpdate', function(obj) {
console.log('ON SESSION UPDATE')
console.log(obj)
// Latest - Donation
if (state.latest.donation) {
state.latest.donation.name = obj.detail.session["tip-session-top-donation"].name;
state.latest.donation.amount = obj.detail.session["tip-session-top-donation"].amount;
}
// Latest - Cheer
if (state.latest.cheer) {
state.latest.cheer.name = obj.detail.session["cheer-session-top-donation"].name;
state.latest.cheer.amount = obj.detail.session["cheer-session-top-donation"].amount;
}
// Latest - Subscriber
if (state.latest.subscriber) {
state.latest.subscriber.name = obj.detail.session["subscriber-latest"].name;
state.latest.subscriber.amount = obj.detail.session["subscriber-latest"].amount;
}
// Latest - Follower
if (state.latest.follower) {
state.latest.follower.name = obj.detail.session["follower-latest"].name;
}
});