-
Notifications
You must be signed in to change notification settings - Fork 6
/
script.js
150 lines (145 loc) · 4.31 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
let darkMode = false;
const allLogos = Array.from(document.getElementsByClassName("vesper-logo"));
const textField = document.getElementsByClassName("input")[0];
const submit = document.getElementById("submit");
const downloader = document.getElementsByClassName("form")[0];
const loader = document.getElementsByClassName("loader")[0];
const render = document.getElementById("render-vids");
const another = document.getElementById("another");
const privacyBtn = document.getElementById("privacyBtn");
const privacyPanel = document.getElementById("privacy");
const closeBtn = document.getElementById("closePrivacy");
if (
window.matchMedia &&
window.matchMedia("(prefers-color-scheme: dark)").matches
) {
darkMode = true;
}
allLogos.forEach((logo) => {
logo.src = darkMode ? "/assets/Vesper-dark.png" : "/assets/Vesper-light.png";
});
const fakeButton = (url) => {
const a = document.createElement("a");
a.href = url;
a.download = url.split("/").pop();
a.style.display = "none";
a.target = "blank";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
};
const fancyTimeFormat = (duration) => {
var hrs = ~~(duration / 3600);
var mins = ~~((duration % 3600) / 60);
var secs = ~~duration % 60;
var ret = "";
if (hrs > 0) {
ret += "" + hrs + ":" + (mins < 10 ? "0" : "");
}
ret += "" + mins + ":" + (secs < 10 ? "0" : "");
ret += "" + secs;
return ret;
};
const formatBytes = (bytes, decimals = 2) => {
if (!bytes) return "Unknown size";
if (bytes === 0) return "0 Bytes";
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ["bytes", "KB", "MB", "GB"];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i];
};
const generateVideo = (obj, title, creator, thumb) => {
let dimensions;
if (obj.width && obj.height) {
dimensions = `${obj.width}x${obj.height}`;
} else {
dimensions = `Audio file`;
}
if (title.length > 30) {
title = title.slice(0, 20) + "...";
}
if (creator.length > 30) {
creator = creator.slice(0, 20) + "...";
}
if (dimensions == "Audio file") {
obj.container = obj.codecs;
if (obj.codecs.includes("mp4a")) {
obj.container = "M4A";
}
thumb = "/audio.png";
}
const template = `
<img src="${thumb}" alt="video thumbnail">
<div class="data">
<h2>${title}</h2>
<p class="author">by ${creator}</p>
<ul>
<li><span class="material-icons">watch_later</span> ${fancyTimeFormat(
obj.approxDurationMs / 1000
)}</li>
<li><span class="material-icons">video_settings</span> ${obj.container.toUpperCase()}</li>
<li><span class="material-icons">save</span> ${formatBytes(
obj.contentLength
)}</li>
<li><span class="material-icons">fit_screen</span>
${dimensions}</li>
</ul>
</div>`;
let newHTML = document.createElement("div");
newHTML.classList.add("video");
newHTML.title = "Download this format";
newHTML.innerHTML = template;
render.appendChild(newHTML);
newHTML.addEventListener("click", () => {
fakeButton(obj.url);
});
};
const download = (url) => {
downloader.style.display = "none";
loader.style.display = "inherit";
fetch(`/api/download?v=${url}`)
.then((response) => {
if (response.ok) {
return response.json();
} else {
return 1;
}
})
.then((data) => {
if (data == 1) {
alert("Error!");
return;
}
loader.style.display = "none";
data.formats.forEach((item) => {
if (item.hasVideo && !item.hasAudio) {
return;
}
generateVideo(
item,
data.videoDetails.title,
data.videoDetails.ownerChannelName,
data.videoDetails.thumbnails[1].url
);
});
another.style.display = "block";
});
};
textField.addEventListener("keyup", function (event) {
if (event.key === "Enter") {
download(textField.value);
}
});
submit.addEventListener("click", function () {
download(textField.value);
});
another.addEventListener("click", function () {
window.location.reload();
});
privacyBtn.addEventListener("click", function () {
privacyPanel.style.display = "block";
});
closeBtn.addEventListener("click", function () {
privacyPanel.style.display = "none";
});