-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimefy.js
54 lines (44 loc) · 2.06 KB
/
timefy.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
(function () {
const getTotalTime = async () => {
const timestamps = [],
getSeconds = (time) => {
// Get timestamps as mm:ss or hh:mm:ss and convert to seconds.
time = time.split(':');
return time[2] ? Number(time[0]) * 3600 + Number(time[1]) * 60 + Number(time[2]) : Number(time[0]) * 60 + Number(time[1])
};
// Waiting for page to be fully loaded
while ($('span.ytd-thumbnail-overlay-time-status-renderer').length === 0) {
await new Promise(res => setTimeout(res, 500));
}
$('span.ytd-thumbnail-overlay-time-status-renderer').each(function () {
timestamps.push(getSeconds(this.innerHTML));
});
const time = timestamps.reduce((acc, time) => acc += time, 0), // Summing all seconds up into one single final timestamp
tag = document.createElement('yt-formatted-string'),
prev = $('[data-time-custom]', '#stats'); // Looking for a previous sum-time element
let h = Math.floor(time / 3600), // Hours
m = Math.floor(time / 60), // Minutes
s = Math.floor(time % 60); // Seconds
while (m > 60) {
m -= 60;
// I thought I needed a "h++" here, but it seems I don't
}
// Prettyfying
h = new String(h).padStart(2, 0);
m = new String(m).padStart(2, 0);
s = new String(s).padStart(2, 0);
tag.className = 'style-scope ytd-playlist-sidebar-primary-info-renderer';
tag.setAttribute('data-time-custom', '');
tag.setAttribute('title', `or ${time} seconds long`);
if (prev.length) {
$(prev).html(`${h}:${m}:${s} long`).attr('title', `or ${time} seconds long`);
return;
}
// Need to insert document into DOM before setting its innerHTML to avoid "<!--css-build:shady-->"
$('#stats').append(tag);
tag.innerHTML = `${h}:${m}:${s}`;
};
// In case of playlist lazy-loading
$(window).on('scroll', getTotalTime);
getTotalTime();
})();