-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.js
80 lines (61 loc) · 2.53 KB
/
time.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
var currentTimeSpan = document.createElement("span");
var totalTimeSpan = document.createElement("span");
currentTimeSpan.className = "currentTimeSpan";
totalTimeSpan.className = "totalTimeSpan";
var timeDiv = document.createElement("div");
timeDiv.style.float = "right";
timeDiv.style.display = "inline-block";
timeDiv.appendChild(currentTimeSpan);
timeDiv.appendChild(totalTimeSpan);
var progressBar = document.createElement("div");
progressBar.id = "progressBar";
var progress = document.createElement("div");
progress.id = "progress";
progressBar.appendChild(progress);
var titleElement;
var time;
var totalTime;
var currentTimeElement;
var streamTitle;
var duration;
var titleCheck = setInterval(() => {
videoElement = document.querySelector("video");
if(videoElement?.duration > 0) {
clearInterval(titleCheck);
titleElement = document.querySelector("#info-contents").getElementsByTagName("yt-formatted-string")[1];
titleElement.appendChild(timeDiv);
streamTitle = titleElement.textContent;
document.querySelector("#primary-inner #player").insertAdjacentElement("afterend", progressBar);
getElements();
setInterval(() => {
titleElement = document.querySelector("#info-contents").getElementsByTagName("yt-formatted-string")[1];
var tempTitle = titleElement.textContent.substring(0, titleElement.textContent.length - currentTimeSpan.textContent.length - totalTimeSpan.textContent.length);
if (streamTitle != tempTitle) {
streamTitle = tempTitle;
getElements();
}
}, 1000);
setInterval(() => {
updateCurrentTime();
}, 500);
}
}, 1000);
function getElements() {
videoElement = document.querySelector("video");
duration = videoElement.duration;
totalTimeSpan.textContent = " / " + getTimeString(duration);
updateCurrentTime();
}
function updateCurrentTime() {
var currentTimeSeconds = videoElement.currentTime;
currentTimeSpan.textContent = getTimeString(currentTimeSeconds);
progress.style.width = "" + (currentTimeSeconds * 100 / duration) + "%";
}
function getTimeString(time) {
var hours = Math.floor(time / 3600);
var res = "" + hours + ":" + getTwoDigitNumber(Math.floor((time - hours * 3600) / 60)) + ":" + getTwoDigitNumber(Math.floor(time % 60));
return res;
}
function getTwoDigitNumber(n) {
return n > 9 ? "" + n : "0" + n;
}