-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
56 lines (44 loc) · 1.74 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
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.type === 'childList') {
const video = document.querySelector('video');
if (video) {
check(video);
}
}
});
});
observer.observe(document.body, { childList: true, subtree: true });
function check(video) {
observer.disconnect();
const ytTimerVideoTimerClass = "ytp-time-duration";
const durationElement = document.getElementsByClassName("ytp-time-duration")[0];
if (durationElement) {
// Create new span element
let newSpan = document.createElement("span");
newSpan.id = "grzojda_time";
newSpan.textContent = "";
// Append new span element
durationElement.insertAdjacentElement('afterend', newSpan);
updateTimer(video.duration, video.playbackRate);
video.addEventListener('ratechange', () => {
updateTimer(video.duration, video.playbackRate);
});
video.addEventListener('durationchange', () => {
updateTimer(video.duration, video.playbackRate);
});
}
}
function timeToStr(seconds) {
const date = new Date(0);
date.setSeconds(seconds);
const timeString = date.toISOString().substr(11, 8);
return timeString;
}
function updateTimer(duration, playbackRate) {
if (playbackRate == 1) {
document.getElementById('grzojda_time').textContent = "";
} else if (!isNaN(duration)) {
document.getElementById('grzojda_time').textContent = " [" + timeToStr(Math.round(duration / playbackRate)) + "]";
}
}