-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
565 lines (550 loc) · 20.3 KB
/
main.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
function loadButtons() {
const htmlFinal = `
<span style="color: white; font-size: 1.5rem">Para ver as próximas transmissões e participar das lives, é necessário autorizar nosso site com sua conta Google.<br><br>
Você também pode ver diretamente no YouTube</span><br><br>
<button onclick="authenticate().then(loadClient)">Autorizar Google/YouTube</button>
<button onclick="window.open('https://www.youtube.com/c/CaioSoutoConversa%C3%A7%C3%B5esfilos%C3%B3ficas/videos?view=2&live_view=502')">Ver no Youtube</button><br><br>
<small><a href="https://www.caiosouto.com/politica-de-privacidade">Leia nossa Política de Privacidade</a></small>
`;
if (window.location.href.includes("com/agenda")) {
document.querySelector("#result").innerHTML = htmlFinal;
} else {
document.querySelector("#liveWrapper").style.display = "none";
document.querySelector("#loginWrapper").style.display = "block";
document.querySelector("#loginWrapper").innerHTML = htmlFinal;
}
}
function errorMessage(func, err) {
document.querySelector("#result").innerHTML = `
<span style="color: white; font-size: 1.5rem">Sentimos muito. Ocorreu um erro ao ${func}. Tente recarregar a página ou volte mais tarde.</span><br><br>
<small><a href="https://www.caiosouto.com/politica-de-privacidade">Leia nossa Política de Privacidade</a></small><br><br>
<small>Mensagem do erro: ${err.message}</small>
`;
}
function authenticate() {
return gapi.auth2
.getAuthInstance()
.signIn({
scope: "https://www.googleapis.com/auth/youtube.force-ssl",
prompt: "select_account",
})
.then(
function () {
loadClient();
},
function (err) {
errorMessage("autenticar com a conta Google", err);
}
);
}
function loadClient() {
gapi.client.setApiKey("<YOUR_API_KEY>");
return gapi.client
.load("https://www.googleapis.com/discovery/v1/apis/youtube/v3/rest")
.then(
function () {
if (window.location.href.includes("agenda")) {
executeUpcoming();
}
executeLiveChecking();
},
function (err) {
errorMessage("carregar cliente do YouTube"), err;
}
);
}
function executeLiveChecking() {
return gapi.client.youtube.search
.list({
part: ["id"],
channelId: "<YOUR_CHANNEL_ID>",
eventType: "live",
maxResults: 1,
type: ["video"],
})
.then(
function (response) {
// Handle the results here (response.result has the parsed body).
const id =
response.result.items.length === 1
? response.result.items[0].id.videoId
: false;
// const id = 'I83XWCSBgSc'
if (id) {
gapi.client.youtube.videos
.list({
part: ["liveStreamingDetails", "id", "snippet"],
id,
})
.then(
function (response) {
let element;
const id = response.result.items[0].id;
const chatId =
response.result.items[0].liveStreamingDetails
.activeLiveChatId;
window.chatId = chatId;
if (window.location.href.includes("vivo")) {
loadLive(id, chatId);
} else {
element = document.querySelector("#live");
element.innerHTML = `
<span style="color: white; font-size: 1rem"><i class="fas fa-broadcast-tower" style="position: absolute;
left: 1rem;"></i> Ao Vivo! <a href="https://caiosouto.com/vivo" style="color: #ddd">Clique e participe!</a> <i style="cursor: pointer; position: absolute;
right: 1rem;" class="fas fa-close" onclick="document.querySelector('#live').style.display = 'none'"></i></span><br>
<iframe id="ytplayerHome" type="text/html" width="640" height="360"
src="https://www.youtube.com/embed/${id}?autoplay=1&mute=1&origin=http://example.com"
frameborder="0"/>`;
element.style.display = "flex";
}
document.querySelector("#liveMenu").style.display = "block";
},
function (err) {
console.log("erro ao carregar vídeo ao vivo");
}
);
} else {
let element;
if (window.location.href.includes("vivo")) {
document.querySelector("#liveWrapper").style.display = "none";
document.querySelector("#loginWrapper").style.display = "block";
element = document.querySelector("#loginWrapper");
element.innerHTML = `<span style="color: white; font-size: 1.5rem;">No momento não há nenhum vídeo sendo trasmitido. Veja as <a href="/agenda">próximas transmissões</a>.</span><br><br>`;
}
}
},
function (err) {
console.log("erro ao listar vídeos ao vivo");
}
);
}
function countMessageLength() {
console.log(document.querySelector("#commentText").value.length);
}
function loadLive(id, chatId) {
document.querySelector("#liveWrapper").style.display = "block";
document.querySelector("#loginWrapper").style.display = "none";
window.comments = [];
gapi.client.youtube.liveChatMessages
.list({
liveChatId: chatId,
part: ["id, snippet, authorDetails"],
maxResults: 20,
})
.then(
(response) => {
window.pageToken = response.result.nextPageToken;
// Handle the results here (response.result has the parsed body).
response.result.items.map((item) => {
window.comments.push({
id: item.id,
name: item.authorDetails.displayName,
img: item.authorDetails.profileImageUrl,
message: item.snippet.displayMessage,
date: item.snippet.publishedAt,
});
});
writeComments(window.comments);
document
.querySelector("#commentText")
.addEventListener("input", function () {
const tarea = document.querySelector("#commentText");
const span = document.querySelector("#commentLength");
if (tarea.value.length < 201) {
span.style.color = "white";
} else {
tarea.value = tarea.value.slice(0, 200);
span.style.color = "red";
}
span.innerHTML = tarea.value.length + "/200";
});
},
function (err) {
console.error("Execute error", err);
}
);
const video = document.querySelector("#liveAgenda");
video.innerHTML = `
<iframe id="ytplayer" type="text/html" width="775" height="435" style="width: 775px; height:435px"
src="https://www.youtube.com/embed/${id}?autoplay=1&mute=1&origin=http://example.com"
frameborder="0"/>`;
}
function loadNewComments(ico) {
if (ico.style.cursor === "pointer") {
ico.style.color = "#999";
ico.style.cursor = "not-allowed";
ico.classList.add("spinning");
gapi.client.youtube.liveChatMessages
.list({
liveChatId: chatId,
part: ["id, snippet, authorDetails"],
pageToken: window.pageToken,
maxResults: 200,
})
.then((response) => {
window.pageToken = response.result.nextPageToken;
response.result.items.map((item) => {
if (!window.comments.find((prev) => prev.id === item.id)) {
window.comments.push({
id: item.id,
name: item.authorDetails.displayName,
img: item.authorDetails.profileImageUrl,
message: item.snippet.displayMessage,
date: item.snippet.publishedAt,
});
}
});
writeComments(window.comments);
ico.style.color = "#e5a102";
ico.style.cursor = "pointer";
ico.classList.remove("spinning");
});
}
}
function writeComments(comments) {
const commentDiv = document.querySelector("#liveComments");
commentDiv.innerHTML = "";
comments.forEach((item) => {
item.date = new Date(item.date);
const finalDate =
item.date.getHours() +
":" +
((item.date.getMinutes() < 10 ? "0" : "") + item.date.getMinutes());
commentDiv.innerHTML += `
<div class='speechbubble'>
<p><img src="${item.img}" style="width: 2rem"> ${item.message}
<span class='username'> ${item.name} às ${finalDate}</span>
</p>
</div>
`;
/* commentDiv.innerHTML += `<li><img src="${item.img}" style="width: 2rem"> <small style="color:white"> ${item.name} comentou em ${finalDate} </small>
<br>
<span>${item.message}</span></li>`*/
});
commentDiv.scrollTop = commentDiv.scrollHeight;
}
function sendComment(ico) {
if (ico.style.cursor === "pointer") {
ico.style.color = "#999";
ico.style.cursor = "not-allowed";
ico.classList.add("spinning");
ico.classList.remove("fa-send");
ico.classList.add("fa-spinner");
const message = document.querySelector("#commentText");
gapi.client.youtube.liveChatMessages
.insert({
part: ["snippet"],
resource: {
snippet: {
liveChatId: window.chatId,
type: "textMessageEvent",
textMessageDetails: {
messageText: message.value,
},
},
},
})
.then(
function (response) {
// Handle the results here (response.result has the parsed body).
if (response.status === 200) {
ico.style.color = "green";
ico.classList.remove("spinning");
ico.classList.add("fa-check");
ico.classList.remove("fa-spinner");
} else {
ico.style.color = "red";
ico.classList.remove("spinning");
ico.classList.add("fa-times");
ico.classList.remove("fa-spinner");
}
const message = document.querySelector("#commentText");
const refresh = document.querySelector("#refreshBtn");
const span = document.querySelector("#commentLength");
span.innerHTML = "0/200";
span.style.color = "white";
message.value = "";
setTimeout(() => {
ico.style.color = "#e5a102";
ico.style.cursor = "pointer";
ico.classList.add("fa-send");
ico.classList.remove("fa-check");
ico.classList.remove("fa-times");
loadNewComments(refresh);
}, 2000);
},
function (err) {
console.error("Execute error", err);
}
);
}
}
// Make sure the client is loaded and sign-in is complete before calling this method.
function executeUpcoming() {
return gapi.client.youtube.search
.list({
part: ["id"],
channelId: "<YOUR_CHANNEL_ID>",
eventType: "upcoming",
maxResults: 25,
type: ["video"],
})
.then(
function (response) {
// Handle the results here (response.result has the parsed body).
const id = [];
response.result.items.map((item) => {
id.push(item.id.videoId);
});
gapi.client.youtube.videos
.list({
part: ["liveStreamingDetails", "id", "snippet"],
id,
})
.then(
function (response) {
// Handle the results here (response.result has the parsed body).
document.querySelector("#result").innerHTML = "";
let result = response.result.items;
result = result.sort(function (a, b) {
let dateA = new Date(a.liveStreamingDetails.scheduledStartTime);
let dateB = new Date(b.liveStreamingDetails.scheduledStartTime);
return dateA - dateB;
});
result.map((item) => {
const title = item.snippet.title;
const split = item.snippet.description.split("__________");
let tags = split[split.length - 1].split("Filosóficas");
tags = tags[tags.length - 1];
const description = split[0];
const dueDate = new Date(
item.liveStreamingDetails.scheduledStartTime
);
let weekDay = dueDate.getDay();
switch (weekDay) {
case 0:
weekDay = "Dom";
break;
case 1:
weekDay = "Seg";
break;
case 2:
weekDay = "Ter";
break;
case 3:
weekDay = "Qua";
break;
case 4:
weekDay = "Qui";
break;
case 5:
weekDay = "Sex";
break;
case 6:
weekDay = "Sab";
break;
}
const dueDateComposed = `${weekDay}, ${dueDate
.toLocaleDateString()
.slice(0, 5)}`;
const id = item.id;
document.querySelector("#result").innerHTML += render(
title,
description,
dueDateComposed,
id,
tags
);
});
},
function (err) {
errorMessage("carregar detalhes dos vídeos", err);
}
);
},
function (err) {
errorMessage("carregar lista de vídeos", err);
}
);
}
const render = (title, description, dueDate, id, tags) => {
return `<div class="elementor-widget-wrap">
<section
class="elementor-section elementor-inner-section elementor-element elementor-element-54035e3d elementor-section-boxed elementor-section-height-default elementor-section-height-default"
data-id="54035e3d"
data-element_type="section"
>
<div class="elementor-container elementor-column-gap-default">
<div class="elementor-row">
<div
class="elementor-column elementor-col-50 elementor-inner-column elementor-element elementor-element-2a99995d animated-slow animated fadeInLeft"
data-id="2a99995d"
data-element_type="column"
data-settings='{"animation":"fadeInLeft"}'
>
<div class="elementor-column-wrap elementor-element-populated">
<div class="elementor-widget-wrap">
<div
class="elementor-element elementor-element-70745d99 elementor-aspect-ratio-169 elementor-widget elementor-widget-video"
data-id="70745d99"
data-element_type="widget"
data-settings='{"aspect_ratio":"169"}'
data-widget_type="video.default"
>
<div class="elementor-widget-container">
<div
class="elementor-wrapper elementor-fit-aspect-ratio elementor-open-inline"
>
<iframe
class="elementor-video-iframe"
allowfullscreen=""
title="Player de Vídeo youtube"
src="https://www.youtube.com/embed/${id}?feature=oembed&start&end&wmode=opaque&loop=0&controls=1&mute=0&rel=0&modestbranding=0"
></iframe>
</div>
</div>
</div>
</div>
</div>
</div>
<div
class="elementor-column elementor-col-50 elementor-inner-column elementor-element elementor-element-66b8f60b digital-creative-section animated-slow animated fadeInRight"
data-id="66b8f60b"
data-element_type="column"
data-settings='{"animation":"fadeInRight"}'
>
<div class="elementor-column-wrap elementor-element-populated">
<div class="elementor-widget-wrap">
<div
class="elementor-element elementor-element-63c39ed1 who-we-are-box elementor-widget__width-auto elementor-absolute elementor-widget elementor-widget-heading"
data-id="63c39ed1"
data-element_type="widget"
data-settings='{"_position":"absolute"}'
data-widget_type="heading.default"
>
<div class="elementor-widget-container">
<h2 class="elementor-heading-title elementor-size-default">
${dueDate}
</h2>
</div>
</div>
<div
class="elementor-element elementor-element-2cd3a90e elementor-widget elementor-widget-heading"
data-id="2cd3a90e"
data-element_type="widget"
data-widget_type="heading.default"
>
<div class="elementor-widget-container">
<h2 class="elementor-heading-title elementor-size-default">
${title}
</h2>
</div>
</div>
<div
class="elementor-element elementor-element-4c79664f elementor-widget-divider--view-line elementor-widget elementor-widget-divider"
data-id="4c79664f"
data-element_type="widget"
data-widget_type="divider.default"
>
<div class="elementor-widget-container">
<div class="elementor-divider">
<span class="elementor-divider-separator"> </span>
</div>
</div>
</div>
<div
class="elementor-element elementor-element-f84dbc3 elementor-widget elementor-widget-text-editor"
data-id="f84dbc3"
data-element_type="widget"
data-widget_type="text-editor.default"
>
<div class="elementor-widget-container">
<div class="elementor-text-editor elementor-clearfix">
<p>${description}</p>
</div>
<div
class="elementor-element elementor-element-9af246d elementor-widget elementor-widget-heading"
data-id="9af246d"
data-element_type="widget"
data-widget_type="heading.default"
>
<div class="elementor-widget-container">
<h2 class="elementor-heading-title elementor-size-small">
${tags}
</h2>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
<div
class="elementor-element elementor-element-2fd3f989 animated-fast elementor-widget-divider--view-line elementor-widget elementor-widget-divider animated rotateInUpRight"
data-id="2fd3f989"
data-element_type="widget"
data-settings='{"_animation":"rotateInUpRight"}'
data-widget_type="divider.default"
>
<div class="elementor-widget-container">
<div class="elementor-divider">
<span class="elementor-divider-separator"> </span>
</div>
</div>
</div>
</div>
<br /><br />`;
};
function userMenu() {
var profile = gapi.auth2
.getAuthInstance()
.currentUser.get()
.getBasicProfile();
const menuMobi = document.querySelector("#gUserMenuMobi");
const menu = document.querySelector("#gUserMenu");
menu.innerHTML = `
<img class="userImage" src="${profile.getImageUrl()}"> <span style="
color: black !important;
font-weight: bold !important;">Olá, ${profile.getGivenName()} </span>
<a href="#" onclick="logout()"> [Sair] </a>
`;
menuMobi.innerHTML = `
<span style="
color: black !important;
font-weight: bold !important;">Olá, ${profile.getGivenName()} </span>
<a href="#" onclick="logout()"> [Sair] </a>
`;
}
function logout() {
const instance = gapi.auth2.getAuthInstance();
instance.signOut();
const menu = document.querySelector("#gUserMenu");
menu.innerHTML = `
<div id="gUserMenu">
<a onclick="authenticate().then(userMenu)"><i class="fas fa-user"></i> Entrar </a>
</div>
`;
loadButtons();
}
gapi.load("client:auth2", function () {
gapi.auth2
.init({
client_id: "<YOUR_CLIENT_ID>",
})
.then((res) => {
if (res.isSignedIn.get()) {
loadClient();
userMenu();
} else {
if (
window.location.href.includes("com/agenda") ||
window.location.href.includes("com/vivo")
) {
loadButtons();
}
}
});
});