2022-07-13 21:32:00 +01:00
|
|
|
let isIOS = navigator.userAgent.toLowerCase().match("iphone os");
|
2022-08-12 14:36:19 +01:00
|
|
|
let version = 3
|
|
|
|
|
2022-07-13 21:32:00 +01:00
|
|
|
let switchers = {
|
|
|
|
"theme": ["auto", "light", "dark"],
|
2022-08-12 14:36:19 +01:00
|
|
|
"ytFormat": ["webm", "mp4"],
|
|
|
|
"quality": ["max", "hig", "mid", "low"],
|
|
|
|
"audioFormat": ["best", "mp3", "ogg", "opus"]
|
2022-07-13 21:32:00 +01:00
|
|
|
}
|
2022-08-12 14:36:19 +01:00
|
|
|
let exceptions = { // fuck you apple
|
|
|
|
"ytFormat": "mp4",
|
|
|
|
"audioFormat": "mp3"
|
2022-08-06 16:21:48 +01:00
|
|
|
}
|
2022-07-13 21:32:00 +01:00
|
|
|
|
2022-07-08 19:17:56 +01:00
|
|
|
function eid(id) {
|
|
|
|
return document.getElementById(id)
|
|
|
|
}
|
|
|
|
function enable(id) {
|
|
|
|
eid(id).dataset.enabled = "true";
|
|
|
|
}
|
|
|
|
function disable(id) {
|
|
|
|
eid(id).dataset.enabled = "false";
|
|
|
|
}
|
|
|
|
function vis(state) {
|
|
|
|
return (state === 1) ? "visible" : "hidden";
|
|
|
|
}
|
2022-08-12 14:36:19 +01:00
|
|
|
function opposite(state) {
|
|
|
|
return state == "true" ? "false" : "true";
|
|
|
|
}
|
2022-07-08 19:17:56 +01:00
|
|
|
function changeDownloadButton(action, text) {
|
|
|
|
switch (action) {
|
|
|
|
case 0:
|
|
|
|
eid("download-button").disabled = true
|
|
|
|
if (localStorage.getItem("alwaysVisibleButton") == "true") {
|
|
|
|
eid("download-button").value = text
|
|
|
|
eid("download-button").style.padding = '0 1rem'
|
|
|
|
} else {
|
|
|
|
eid("download-button").value = ''
|
|
|
|
eid("download-button").style.padding = '0'
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
eid("download-button").disabled = false
|
|
|
|
eid("download-button").value = text
|
|
|
|
eid("download-button").style.padding = '0 1rem'
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
eid("download-button").disabled = true
|
|
|
|
eid("download-button").value = text
|
|
|
|
eid("download-button").style.padding = '0 1rem'
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-08-04 12:22:40 +01:00
|
|
|
document.addEventListener("keydown", function (event) {
|
2022-07-08 19:17:56 +01:00
|
|
|
if (event.key == "Tab") {
|
|
|
|
eid("download-button").value = '>>'
|
|
|
|
eid("download-button").style.padding = '0 1rem'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
function button() {
|
|
|
|
let regex = /https:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()!@:%_\+.~#?&\/\/=]*)/.test(eid("url-input-area").value);
|
|
|
|
regex ? changeDownloadButton(1, '>>') : changeDownloadButton(0, '>>');
|
|
|
|
}
|
2022-07-13 21:32:00 +01:00
|
|
|
function copy(id, data) {
|
2022-07-08 19:17:56 +01:00
|
|
|
let e = document.getElementById(id);
|
|
|
|
e.classList.add("text-backdrop");
|
2022-07-13 21:32:00 +01:00
|
|
|
data ? navigator.clipboard.writeText(data) : navigator.clipboard.writeText(e.innerText);
|
2022-07-08 19:17:56 +01:00
|
|
|
setTimeout(() => { e.classList.remove("text-backdrop") }, 600);
|
|
|
|
}
|
|
|
|
function detectColorScheme() {
|
|
|
|
let theme = "auto";
|
|
|
|
let localTheme = localStorage.getItem("theme");
|
|
|
|
if (localTheme) {
|
|
|
|
theme = localTheme;
|
|
|
|
} else if (!window.matchMedia) {
|
|
|
|
theme = "dark"
|
|
|
|
}
|
|
|
|
document.documentElement.setAttribute("data-theme", theme);
|
|
|
|
}
|
2022-08-12 14:36:19 +01:00
|
|
|
function changeTab(evnt, tabId, tabClass) {
|
|
|
|
let tabcontent = document.getElementsByClassName(`tab-content-${tabClass}`);
|
|
|
|
let tablinks = document.getElementsByClassName(`tab-${tabClass}`);
|
|
|
|
for (let i = 0; i < tabcontent.length; i++) {
|
|
|
|
tabcontent[i].style.display = "none";
|
|
|
|
}
|
|
|
|
for (let i = 0; i < tablinks.length; i++) {
|
|
|
|
tablinks[i].dataset.enabled = "false";
|
|
|
|
}
|
|
|
|
eid(tabId).style.display = "block";
|
|
|
|
evnt.currentTarget.dataset.enabled = "true";
|
|
|
|
}
|
|
|
|
function hideAllPopups() {
|
|
|
|
let filter = document.getElementsByClassName('popup');
|
|
|
|
for (let i = 0; i < filter.length; i++) {
|
|
|
|
filter[i].style.visibility = "hidden";
|
|
|
|
}
|
|
|
|
eid("popup-backdrop").style.visibility = "hidden";
|
|
|
|
}
|
2022-07-08 19:17:56 +01:00
|
|
|
function popup(type, action, text) {
|
|
|
|
eid("popup-backdrop").style.visibility = vis(action);
|
|
|
|
switch (type) {
|
|
|
|
case "about":
|
2022-08-12 14:36:19 +01:00
|
|
|
let tabId = text ? text : "changelog";
|
|
|
|
if (tabId == "changelog") {
|
|
|
|
localStorage.setItem("changelogStatus", version)
|
|
|
|
}
|
|
|
|
eid(`tab-button-${type}-${tabId}`).click();
|
2022-07-08 19:17:56 +01:00
|
|
|
eid("popup-about").style.visibility = vis(action);
|
2022-07-13 21:32:00 +01:00
|
|
|
if (!localStorage.getItem("seenAbout")) localStorage.setItem("seenAbout", "true");
|
2022-07-08 19:17:56 +01:00
|
|
|
break;
|
2022-08-12 14:36:19 +01:00
|
|
|
case "settings":
|
|
|
|
eid(`tab-button-${type}-video`).click();
|
|
|
|
eid("popup-settings").style.visibility = vis(action);
|
|
|
|
break;
|
2022-07-08 19:17:56 +01:00
|
|
|
case "error":
|
|
|
|
eid("desc-error").innerHTML = text;
|
|
|
|
eid("popup-error").style.visibility = vis(action);
|
|
|
|
break;
|
2022-07-13 21:32:00 +01:00
|
|
|
case "download":
|
|
|
|
if (action == 1) {
|
|
|
|
eid("pd-download").href = text;
|
2022-08-04 12:22:40 +01:00
|
|
|
eid("pd-copy").setAttribute("onClick", `copy('pd-copy', '${text}')`);
|
2022-07-13 21:32:00 +01:00
|
|
|
}
|
|
|
|
eid("popup-download").style.visibility = vis(action);
|
2022-07-08 19:17:56 +01:00
|
|
|
break;
|
2022-07-13 21:32:00 +01:00
|
|
|
default:
|
|
|
|
eid(`popup-${type}`).style.visibility = vis(action);
|
2022-07-08 19:17:56 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-08-12 14:36:19 +01:00
|
|
|
function changeSwitcher(li, b) {
|
2022-07-08 19:17:56 +01:00
|
|
|
if (b) {
|
2022-08-12 14:36:19 +01:00
|
|
|
localStorage.setItem(li, b);
|
2022-07-13 21:32:00 +01:00
|
|
|
for (i in switchers[li]) {
|
|
|
|
(switchers[li][i] == b) ? enable(`${li}-${b}`) : disable(`${li}-${switchers[li][i]}`)
|
2022-07-08 19:17:56 +01:00
|
|
|
}
|
2022-07-13 21:32:00 +01:00
|
|
|
if (li == "theme") detectColorScheme();
|
2022-07-08 19:17:56 +01:00
|
|
|
} else {
|
2022-08-06 16:21:48 +01:00
|
|
|
let pref = switchers[li][0];
|
|
|
|
localStorage.setItem(li, pref);
|
2022-08-12 14:36:19 +01:00
|
|
|
if (isIOS && exceptions[li]) pref = exceptions[li];
|
2022-07-13 21:32:00 +01:00
|
|
|
for (i in switchers[li]) {
|
2022-08-06 16:21:48 +01:00
|
|
|
(switchers[li][i] == pref) ? enable(`${li}-${pref}`) : disable(`${li}-${switchers[li][i]}`)
|
2022-07-08 19:17:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function internetError() {
|
|
|
|
eid("url-input-area").disabled = false
|
|
|
|
changeDownloadButton(2, '!!')
|
|
|
|
popup("error", 1, loc.noInternet);
|
|
|
|
}
|
|
|
|
function checkbox(action) {
|
2022-07-13 21:32:00 +01:00
|
|
|
if (eid(action).checked) {
|
|
|
|
localStorage.setItem(action, "true");
|
|
|
|
if (action == "alwaysVisibleButton") button();
|
|
|
|
} else {
|
|
|
|
localStorage.setItem(action, "false");
|
|
|
|
if (action == "alwaysVisibleButton") button();
|
2022-07-08 19:17:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
function loadSettings() {
|
|
|
|
if (localStorage.getItem("alwaysVisibleButton") == "true") {
|
2022-07-13 21:32:00 +01:00
|
|
|
eid("alwaysVisibleButton").checked = true;
|
2022-07-08 19:17:56 +01:00
|
|
|
eid("download-button").value = '>>'
|
|
|
|
eid("download-button").style.padding = '0 1rem';
|
|
|
|
}
|
2022-07-13 21:32:00 +01:00
|
|
|
if (localStorage.getItem("downloadPopup") == "true" && !isIOS) {
|
|
|
|
eid("downloadPopup").checked = true;
|
|
|
|
}
|
2022-08-12 14:36:19 +01:00
|
|
|
if (!localStorage.getItem("audioMode")) {
|
|
|
|
toggle("audioMode")
|
|
|
|
}
|
|
|
|
updateToggle("audioMode", localStorage.getItem("audioMode"))
|
|
|
|
for (let i in switchers) {
|
|
|
|
changeSwitcher(i, localStorage.getItem(i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function checkbox(action) {
|
|
|
|
if (eid(action).checked) {
|
|
|
|
localStorage.setItem(action, "true");
|
|
|
|
if (action == "alwaysVisibleButton") button();
|
|
|
|
} else {
|
|
|
|
localStorage.setItem(action, "false");
|
|
|
|
if (action == "alwaysVisibleButton") button();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function toggle(toggle) {
|
|
|
|
let state = localStorage.getItem(toggle);
|
|
|
|
if (state) {
|
|
|
|
localStorage.setItem(toggle, opposite(state))
|
|
|
|
} else {
|
|
|
|
localStorage.setItem(toggle, "false")
|
|
|
|
}
|
|
|
|
updateToggle(toggle, localStorage.getItem(toggle))
|
|
|
|
}
|
|
|
|
function updateToggle(toggle, state) {
|
|
|
|
switch(state) {
|
|
|
|
case "true":
|
|
|
|
eid(toggle).innerHTML = loc.toggleAudio;
|
|
|
|
break;
|
|
|
|
case "false":
|
|
|
|
eid(toggle).innerHTML = loc.toggleDefault;
|
|
|
|
break;
|
|
|
|
}
|
2022-07-08 19:17:56 +01:00
|
|
|
}
|
|
|
|
async function download(url) {
|
|
|
|
changeDownloadButton(2, '...');
|
|
|
|
eid("url-input-area").disabled = true;
|
2022-08-12 14:36:19 +01:00
|
|
|
let audioMode = localStorage.getItem("audioMode");
|
|
|
|
let format = (url.includes("youtube.com/") && audioMode == "false" || url.includes("/youtu.be/") && audioMode == "false") ? `&format=${localStorage.getItem("ytFormat")}` : '';
|
|
|
|
let mode = (localStorage.getItem("audioMode") == "true") ? `audio=true` : `quality=${localStorage.getItem("quality")}`;
|
|
|
|
fetch(`/api/json?audioFormat=${localStorage.getItem("audioFormat")}&${mode}${format}&url=${encodeURIComponent(url)}`).then(async (response) => {
|
2022-07-08 19:17:56 +01:00
|
|
|
let j = await response.json();
|
|
|
|
if (j.status != "error" && j.status != "rate-limit") {
|
2022-07-22 09:05:36 +01:00
|
|
|
if (j.url) {
|
|
|
|
switch (j.status) {
|
|
|
|
case "redirect":
|
|
|
|
changeDownloadButton(2, '>>>')
|
|
|
|
setTimeout(() => {
|
|
|
|
changeDownloadButton(1, '>>')
|
2022-07-08 19:17:56 +01:00
|
|
|
eid("url-input-area").disabled = false
|
2022-07-22 09:05:36 +01:00
|
|
|
}, 3000)
|
|
|
|
if (localStorage.getItem("downloadPopup") == "true") {
|
|
|
|
popup('download', 1, j.url)
|
|
|
|
} else {
|
|
|
|
window.open(j.url, '_blank');
|
2022-07-08 19:17:56 +01:00
|
|
|
}
|
2022-07-22 09:05:36 +01:00
|
|
|
break;
|
|
|
|
case "stream":
|
|
|
|
changeDownloadButton(2, '?..')
|
|
|
|
fetch(`${j.url}&p=1&origin=front`).then(async (response) => {
|
|
|
|
let jp = await response.json();
|
|
|
|
if (jp.status == "continue") {
|
|
|
|
changeDownloadButton(2, '>>>')
|
|
|
|
window.location.href = j.url
|
|
|
|
setTimeout(() => {
|
|
|
|
changeDownloadButton(1, '>>')
|
|
|
|
eid("url-input-area").disabled = false
|
|
|
|
}, 5000)
|
|
|
|
} else {
|
|
|
|
eid("url-input-area").disabled = false
|
|
|
|
changeDownloadButton(2, '!!')
|
|
|
|
popup("error", 1, jp.text);
|
|
|
|
}
|
|
|
|
}).catch((error) => internetError());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
eid("url-input-area").disabled = false
|
|
|
|
changeDownloadButton(2, '!!')
|
|
|
|
popup("error", 1, loc.noURLReturned);
|
2022-07-08 19:17:56 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
eid("url-input-area").disabled = false
|
|
|
|
changeDownloadButton(2, '!!')
|
|
|
|
popup("error", 1, j.text);
|
|
|
|
}
|
2022-07-13 21:32:00 +01:00
|
|
|
}).catch((error) => internetError());
|
2022-07-08 19:17:56 +01:00
|
|
|
}
|
|
|
|
window.onload = function () {
|
|
|
|
loadSettings();
|
|
|
|
detectColorScheme();
|
|
|
|
changeDownloadButton(0, '>>');
|
|
|
|
eid("cobalt-main-box").style.visibility = 'visible';
|
|
|
|
eid("footer").style.visibility = 'visible';
|
|
|
|
eid("url-input-area").value = "";
|
2022-08-12 14:36:19 +01:00
|
|
|
if (!localStorage.getItem("seenAbout")) {
|
|
|
|
popup('about', 1, "about");
|
|
|
|
} else if (localStorage.getItem("changelogStatus") != `${version}` && localStorage.getItem("disableChangelog") != "true") {
|
|
|
|
popup('about', 1, "changelog");
|
|
|
|
}
|
2022-07-13 21:32:00 +01:00
|
|
|
if (isIOS) localStorage.setItem("downloadPopup", "true");
|
2022-07-08 19:17:56 +01:00
|
|
|
}
|
|
|
|
eid("url-input-area").addEventListener("keyup", (event) => {
|
2022-08-12 14:36:19 +01:00
|
|
|
if (event.key === 'Enter') eid("download-button").click();
|
2022-08-04 19:09:41 +01:00
|
|
|
})
|
2022-08-12 14:36:19 +01:00
|
|
|
document.onkeydown = function(event) {
|
|
|
|
if (event.key === 'Escape') hideAllPopups();
|
|
|
|
};
|