-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.js
340 lines (279 loc) · 8.56 KB
/
script.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
////////////////
// PARAMETERS //
////////////////
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
let sbDebugMode = true;
const sbServerAddress = urlParams.get("address") || "127.0.0.1";
const sbServerPort = urlParams.get("port") || "8080";
const lineThickness = urlParams.get("lineThickness") || 4;
const barPosition = urlParams.get("barPosition") || "bottom"; // none, bottom, top, left, right
const timerPosition = urlParams.get("timerPosition") || "topLeft"; // none, topLeft, topRight, bottomLeft, bottomRight
const showMidRollCountdown = urlParams.get("timerPosition") || true;
/////////////////
// GLOBAL VARS //
/////////////////
let ws;
let singleAdLength = 30;
///////////////////////////////////
// SRTEAMER.BOT WEBSOCKET SERVER //
///////////////////////////////////
// This is the main function that connects to the Streamer.bot websocket server
function connectws() {
if ("WebSocket" in window) {
ws = new WebSocket("ws://" + sbServerAddress + ":" + sbServerPort + "/");
// Reconnect
ws.onclose = function () {
SetConnectionStatus(false);
setTimeout(connectws, 5000);
};
// Connect
ws.onopen = async function () {
SetConnectionStatus(true);
console.log("Subscribe to events");
ws.send(
JSON.stringify({
request: "Subscribe",
id: "subscribe-events-id",
events: {
// This is the list of Streamer.bot websocket events to subscribe to
// See full list of events here:
// https://wiki.streamer.bot/en/Servers-Clients/WebSocket-Server/Requests
twitch: [
"AdRun",
"AdMidRoll"
]
}
})
);
ws.onmessage = function (event) {
// Grab message and parse JSON
const msg = event.data;
const wsdata = JSON.parse(msg);
if (typeof wsdata.event == "undefined") {
return;
}
// Print data to log for debugging purposes
if (sbDebugMode)
{
console.log(wsdata.data);
console.log(wsdata.event.type);
}
// Check for events to trigger
// See documentation for all events here:
// https://wiki.streamer.bot/en/Servers-Clients/WebSocket-Server/Events
switch (wsdata.event.source) {
// Twitch Events
case 'Twitch':
switch (wsdata.event.type) {
case ('AdRun'):
AdRun(wsdata.data);
break;
case ('AdMidRoll'):
AdMidRoll(wsdata.data);
break;
}
break;
}
};
}
}
}
///////////////////////
// TWITCH AD OVERLAY //
///////////////////////
function AdRun(data) {
if (data.length_seconds <= 0)
{
TimerBarAnimation(singleAdLength);
HugeTittiesAnimation(singleAdLength);
}
else
{
TimerBarAnimation(data.length_seconds);
HugeTittiesAnimation(data.length_seconds);
}
}
function AdMidRoll(data) {
if (!showMidRollCountdown)
return;
//MidRollAnimation(data.length);
MidRollAnimation(5);
}
function TimerBarAnimation(adLength) {
let timerBar = document.getElementById("timerBar");
switch (barPosition) {
case "none":
timerBar.style.display = "none";
break;
case "bottom":
timerBar.style.height = lineThickness + "px";
timerBar.style.bottom = "0px";
timerBar.style.left = "0px";
// Start Animation
tl = new TimelineMax();
tl
.to(timerBar, 0.5, { width: window.innerWidth + "px", ease: Cubic.ease })
.to(timerBar, adLength, { width: "0px", ease: Linear.easeNone })
break;
case "top":
timerBar.style.height = lineThickness + "px";
timerBar.style.top = "0px";
timerBar.style.left = "0px";
// Start Animation
tl = new TimelineMax();
tl
.to(timerBar, 0.5, { width: window.innerWidth + "px", ease: Cubic.ease })
.to(timerBar, adLength, { width: "0px", ease: Linear.easeNone })
break;
case "left":
timerBar.style.width = lineThickness + "px";
timerBar.style.bottom = "0px";
timerBar.style.left = "0px";
// Start Animation
tl = new TimelineMax();
tl
.to(timerBar, 0.5, { height: window.innerHeight + "px", ease: Cubic.ease })
.to(timerBar, adLength, { height: "0px", ease: Linear.easeNone })
break;
case "right":
timerBar.style.width = lineThickness + "px";
timerBar.style.bottom = "0px";
timerBar.style.right = "0px";
// Start Animation
tl = new TimelineMax();
tl
.to(timerBar, 0.5, { height: window.innerHeight + "px", ease: Cubic.ease })
.to(timerBar, adLength, { height: "0px", ease: Linear.easeNone })
break;
}
}
function HugeTittiesAnimation(adLength) {
let hugeTittiesContainer = document.getElementById("hugeTittiesContainer");
switch (timerPosition) {
case "none":
hugeTittiesContainer.style.display = "none";
break;
case "topLeft":
hugeTittiesContainer.style.top = "0px";
hugeTittiesContainer.style.left = "0px";
break;
case "topRight":
hugeTittiesContainer.style.top = "0px";
hugeTittiesContainer.style.right = "0px";
break;
case "bottomLeft":
hugeTittiesContainer.style.bottom = "0px";
hugeTittiesContainer.style.left = "0px";
break;
case "bottomRight":
hugeTittiesContainer.style.bottom = "0px";
hugeTittiesContainer.style.right = "0px";
break;
}
// Calculate starting time
let startingTime = adLength % singleAdLength;
if (startingTime == 0)
startingTime = singleAdLength;
// Estimate how many ads there should be
let adsTotal = Math.ceil(adLength / singleAdLength);
let adsRemaining = 1;
// Start the countdown timer
let adsRemainingContainer = document.getElementById("adsRemainingContainer");
let timerContainer = document.getElementById("timerContainer");
var timerThingy = setInterval(function () {
startingTime--;
if (startingTime == 0 && adsRemaining < adsTotal) {
adsRemaining++;
startingTime = singleAdLength;
}
if (startingTime == 0 && adsRemaining == adsTotal) {
clearInterval(timerThingy);
SetVisibility(false);
return;
}
adsRemainingContainer.innerText = adsRemaining + " of " + adsTotal;
timerContainer.innerText = startingTime.toString().toHHMMSS();
// Show the widget
SetVisibility(true);
}, 1000)
}
function MidRollAnimation(countdownLength) {
let midRollContainer = document.getElementById("midRollContainer");
let midRollCountdownContainer = document.getElementById("midRollCountdownContainer");
let width = midRollContainer.getBoundingClientRect().width;
// Set the starting position of the countdown box
midRollContainer.style.right = -width + "px";
midRollCountdownContainer.innerHTML = countdownLength;
// Slide the countdown box on screen
ShowMidRollCountdown(true);
// Start the countdown timer
let startingTime = countdownLength;
var timerThingy = setInterval(function () {
startingTime--;
midRollCountdownContainer.innerText = startingTime;
if (startingTime == 0) {
clearInterval(timerThingy);
ShowMidRollCountdown(false);
return;
}
}, 1000)
}
function SetVisibility(isVisible) {
let hugeTittiesContainer = document.getElementById("hugeTittiesContainer");
var tl = new TimelineMax();
tl
.to(hugeTittiesContainer, 0.5, { opacity: isVisible, ease: Linear.easeNone });
}
function ShowMidRollCountdown(isVisible) {
let midRollContainer = document.getElementById("midRollContainer");
let width = midRollContainer.getBoundingClientRect().width;
var tl = new TimelineMax();
if (isVisible)
{
tl
.to(midRollContainer, 0.5, { right: "-10px", ease: Power1.easeInOut })
}
else
{
tl
.to(midRollContainer, 0.5, { right: -width + "px", ease: Power1.easeInOut })
}
}
//////////////////////
// HELPER FUNCTIONS //
//////////////////////
String.prototype.toHHMMSS = function () {
var sec_num = parseInt(this, 10); // don't forget the second param
var hours = Math.floor(sec_num / 3600);
var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
var seconds = sec_num - (hours * 3600) - (minutes * 60);
if (hours < 10) { hours = "0" + hours; }
//if (minutes < 10) {minutes = "0"+minutes;}
if (seconds < 10) { seconds = "0" + seconds; }
//return hours+':'+minutes+':'+seconds;
return minutes + ':' + seconds;
}
function IsNullOrWhitespace(str) {
return /^\s*$/.test(str);
}
///////////////////////////////////
// STREAMER.BOT WEBSOCKET STATUS //
///////////////////////////////////
function SetConnectionStatus(connected) {
let statusContainer = document.getElementById("statusContainer");
if (connected) {
statusContainer.style.background = "#2FB774";
statusContainer.innerText = "Connected!";
var tl = new TimelineMax();
tl
.to(statusContainer, 2, { opacity: 0, ease: Linear.easeNone })
//.call(removeElement, [div]);
}
else {
statusContainer.style.background = "#D12025";
statusContainer.innerText = "Connecting...";
statusContainer.style.opacity = 1;
}
}
connectws();