cobalt/src/front/cobalt.js

474 lines
19 KiB
JavaScript
Raw Normal View History

2022-11-04 08:49:58 +00:00
let ua = navigator.userAgent.toLowerCase();
let isIOS = ua.match("iphone os");
let isMobile = ua.match("android") || ua.match("iphone os");
2023-03-24 19:31:53 +00:00
let version = 25;
2022-09-08 17:02:55 +01:00
let regex = new RegExp(/https:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()!@:%_\+.~#?&\/\/=]*)/);
2022-10-02 15:13:33 +01:00
let notification = `<div class="notification-dot"></div>`
2022-11-15 17:37:33 +00:00
let store = {}
let switchers = {
"theme": ["auto", "light", "dark"],
2023-02-26 16:49:25 +00:00
"vCodec": ["h264", "av1", "vp9"],
"vQuality": ["1080", "max", "2160", "1440", "720", "480", "360"],
"aFormat": ["mp3", "best", "ogg", "wav", "opus"],
"dubLang": ["original", "auto"],
"vimeoDash": ["false", "true"]
}
let checkboxes = ["disableTikTokWatermark", "fullTikTokAudio", "muteAudio", "leftHandedLayout"];
2022-11-04 08:49:58 +00:00
let exceptions = { // used for mobile devices
2023-02-26 16:49:25 +00:00
"vQuality": "720"
}
2022-07-08 19:17:56 +01:00
function eid(id) {
return document.getElementById(id)
}
2022-08-16 11:31:41 +01:00
function sGet(id) {
return localStorage.getItem(id)
}
function sSet(id, value) {
localStorage.setItem(id, value)
}
2022-07-08 19:17:56 +01:00
function enable(id) {
eid(id).dataset.enabled = "true";
}
function disable(id) {
eid(id).dataset.enabled = "false";
}
function vis(state) {
return (state === 1) ? "visible" : "hidden";
}
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 (sGet("alwaysVisibleButton") === "true") {
2022-07-08 19:17:56 +01:00
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-16 08:14:19 +01:00
document.addEventListener("keydown", (event) => {
if (event.key === "Tab") {
2022-07-08 19:17:56 +01:00
eid("download-button").value = '>>'
eid("download-button").style.padding = '0 1rem'
}
})
function button() {
2022-09-08 17:02:55 +01:00
let regexTest = regex.test(eid("url-input-area").value);
if ((eid("url-input-area").value).length > 0) {
eid("url-clear").style.display = "block";
} else {
eid("url-clear").style.display = "none";
}
regexTest ? changeDownloadButton(1, '>>') : changeDownloadButton(0, '>>');
}
function clearInput() {
eid("url-input-area").value = '';
button();
2022-07-08 19:17:56 +01:00
}
function copy(id, data) {
2022-07-08 19:17:56 +01:00
let e = document.getElementById(id);
e.classList.add("text-backdrop");
setTimeout(() => { e.classList.remove("text-backdrop") }, 600);
data ? navigator.clipboard.writeText(data) : navigator.clipboard.writeText(e.innerText);
2022-07-08 19:17:56 +01:00
}
function detectColorScheme() {
let theme = "auto";
2022-08-16 11:31:41 +01:00
let localTheme = sGet("theme");
2022-07-08 19:17:56 +01:00
if (localTheme) {
theme = localTheme;
} else if (!window.matchMedia) {
theme = "dark"
}
document.documentElement.setAttribute("data-theme", theme);
}
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";
if (tabId === "tab-about-changelog" && sGet("changelogStatus") !== `${version}`) notificationCheck("changelog");
if (tabId === "tab-about-about" && !sGet("seenAbout")) notificationCheck("about");
2022-10-02 15:13:33 +01:00
}
function expandCollapsible(evnt) {
let classlist = evnt.currentTarget.parentNode.classList;
let c = "expanded";
!classlist.contains(c) ? classlist.add(c) : classlist.remove(c);
}
2022-10-02 15:13:33 +01:00
function notificationCheck(type) {
let changed = true;
switch (type) {
case "about":
sSet("seenAbout", "true");
break;
case "changelog":
sSet("changelogStatus", version)
break;
default:
changed = false;
}
if (changed && sGet("changelogStatus") === `${version}` || type === "disable") {
2022-10-02 15:13:33 +01:00
setTimeout(() => {
eid("about-footer").innerHTML = eid("about-footer").innerHTML.replace(notification, '');
eid("tab-button-about-changelog").innerHTML = eid("tab-button-about-changelog").innerHTML.replace(notification, '')
}, 900)
}
if (sGet("disableChangelog") !== "true") {
2022-10-02 15:13:33 +01:00
if (!sGet("seenAbout") && !eid("about-footer").innerHTML.includes(notification)) eid("about-footer").innerHTML = `${notification}${eid("about-footer").innerHTML}`;
if (sGet("changelogStatus") !== `${version}`) {
2022-10-02 15:13:33 +01:00
if (!eid("about-footer").innerHTML.includes(notification)) eid("about-footer").innerHTML = `${notification}${eid("about-footer").innerHTML}`;
if (!eid("tab-button-about-changelog").innerHTML.includes(notification)) eid("tab-button-about-changelog").innerHTML = `${notification}${eid("tab-button-about-changelog").innerHTML}`;
}
}
}
function hideAllPopups() {
let filter = document.getElementsByClassName('popup');
for (let i = 0; i < filter.length; i++) {
filter[i].style.visibility = "hidden";
}
2022-10-09 18:44:00 +01:00
eid("picker-holder").innerHTML = '';
eid("picker-download").href = '/';
eid("picker-download").style.visibility = "hidden";
eid("popup-backdrop").style.visibility = "hidden";
}
2022-07-08 19:17:56 +01:00
function popup(type, action, text) {
if (action === 1) {
2022-10-02 15:13:33 +01:00
hideAllPopups(); // hide the previous popup before showing a new one
switch (type) {
case "about":
let tabId = sGet("seenAbout") ? "changelog" : "about";
if (text) tabId = text;
2022-10-02 15:13:33 +01:00
eid(`tab-button-${type}-${tabId}`).click();
break;
case "settings":
eid(`tab-button-${type}-video`).click();
break;
case "error":
eid("desc-error").innerHTML = text;
break;
case "download":
eid("pd-download").href = text;
eid("pd-copy").setAttribute("onClick", `copy('pd-copy', '${text}')`);
2022-10-02 15:13:33 +01:00
break;
2022-10-09 18:44:00 +01:00
case "picker":
switch (text.type) {
case "images":
eid("picker-title").innerHTML = loc.pickerImages;
eid("picker-subtitle").innerHTML = loc.pickerImagesExpl;
if (!eid("popup-picker").classList.contains("scrollable")) eid("popup-picker").classList.add("scrollable");
if (eid("picker-holder").classList.contains("various")) eid("picker-holder").classList.remove("various");
eid("picker-download").href = text.audio;
eid("picker-download").style.visibility = "visible"
for (let i in text.arr) {
eid("picker-holder").innerHTML += `<a class="picker-image-container"><img class="picker-image" src="${text.arr[i]["url"]}" onerror="this.parentNode.style.display='none'"></img></a>`
}
break;
default:
eid("picker-title").innerHTML = loc.pickerDefault;
eid("picker-subtitle").innerHTML = loc.pickerDefaultExpl;
if (eid("popup-picker").classList.contains("scrollable")) eid("popup-picker").classList.remove("scrollable");
if (!eid("picker-holder").classList.contains("various")) eid("picker-holder").classList.add("various");
for (let i in text.arr) {
let s = text.arr[i], item;
switch (s.type) {
case "video":
item = `<a class="picker-various-container" href="${text.arr[i]["url"]}" target="_blank"><div class="picker-element-name">VIDEO ${Number(i)+1}</div><div class="imageBlock"></div><img class="picker-image" src="${text.arr[i]["thumb"]}" onerror="this.style.display='none'"></img></a>`
break;
}
eid("picker-holder").innerHTML += item
}
eid("picker-download").style.visibility = "hidden";
break;
2022-10-02 15:13:33 +01:00
}
break;
default:
break;
}
} else {
if (type === "picker") {
2022-10-09 18:44:00 +01:00
eid("picker-download").href = '/';
eid("picker-download").style.visibility = "hidden"
2022-10-09 18:44:00 +01:00
eid("picker-holder").innerHTML = ''
2022-10-02 15:13:33 +01:00
}
2022-07-08 19:17:56 +01:00
}
2022-10-02 15:13:33 +01:00
eid("popup-backdrop").style.visibility = vis(action);
eid(`popup-${type}`).style.visibility = vis(action);
2022-07-08 19:17:56 +01:00
}
function changeSwitcher(li, b) {
2022-07-08 19:17:56 +01:00
if (b) {
2023-02-26 16:49:25 +00:00
if (!switchers[li].includes(b)) b = switchers[li][0];
2022-08-16 11:31:41 +01:00
sSet(li, b);
2022-09-01 15:02:37 +01:00
for (let i in switchers[li]) {
(switchers[li][i] === b) ? enable(`${li}-${b}`) : disable(`${li}-${switchers[li][i]}`)
2022-07-08 19:17:56 +01:00
}
if (li === "theme") detectColorScheme();
2022-07-08 19:17:56 +01:00
} else {
let pref = switchers[li][0];
2022-11-04 08:49:58 +00:00
if (isMobile && exceptions[li]) pref = exceptions[li];
sSet(li, pref);
2022-09-01 15:02:37 +01:00
for (let i in switchers[li]) {
(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
2022-09-08 17:02:55 +01:00
changeDownloadButton(2, '!!');
2022-07-08 19:17:56 +01:00
popup("error", 1, loc.noInternet);
}
function checkbox(action) {
sSet(action, !!eid(action).checked);
switch(action) {
case "alwaysVisibleButton": button(); break;
case "leftHandedLayout":
eid("bottom").setAttribute("data-lefthanded", sGet("leftHandedLayout"));
break;
2022-07-08 19:17:56 +01:00
}
sGet(action) === "true" ? notificationCheck("disable") : notificationCheck();
2022-07-08 19:17:56 +01:00
}
2022-09-01 15:22:59 +01:00
function updateToggle(toggl, state) {
2022-09-01 15:02:37 +01:00
switch(state) {
case "true":
2022-09-01 15:22:59 +01:00
eid(toggl).innerHTML = loc.toggleAudio;
2022-09-01 15:02:37 +01:00
break;
case "false":
2022-09-08 17:02:55 +01:00
eid(toggl).innerHTML = loc.toggleDefault;
2022-09-01 15:02:37 +01:00
break;
}
}
2022-09-01 15:22:59 +01:00
function toggle(toggl) {
let state = sGet(toggl);
2022-09-01 15:02:37 +01:00
if (state) {
2022-09-01 15:22:59 +01:00
sSet(toggl, opposite(state))
if (opposite(state) === "true") sSet(`${toggl}ToggledOnce`, "true");
2022-09-01 15:02:37 +01:00
} else {
2022-09-01 15:22:59 +01:00
sSet(toggl, "false")
2022-09-01 15:02:37 +01:00
}
2022-09-01 15:22:59 +01:00
updateToggle(toggl, sGet(toggl))
2022-09-01 15:02:37 +01:00
}
2022-07-08 19:17:56 +01:00
function loadSettings() {
2022-09-28 13:21:36 +01:00
try {
2022-09-28 13:24:44 +01:00
if (typeof(navigator.clipboard.readText) == "undefined") throw new Error();
2022-09-28 13:21:36 +01:00
} catch (err) {
eid("pasteFromClipboard").style.display = "none"
2022-09-28 13:21:36 +01:00
}
if (sGet("alwaysVisibleButton") === "true") {
eid("alwaysVisibleButton").checked = true;
2022-07-08 19:17:56 +01:00
eid("download-button").value = '>>'
eid("download-button").style.padding = '0 1rem';
}
if (sGet("downloadPopup") === "true" && !isIOS) {
eid("downloadPopup").checked = true;
}
2022-08-16 11:31:41 +01:00
if (!sGet("audioMode")) {
toggle("audioMode")
}
for (let i = 0; i < checkboxes.length; i++) {
if (sGet(checkboxes[i]) === "true") eid(checkboxes[i]).checked = true;
2022-08-16 11:31:41 +01:00
}
updateToggle("audioMode", sGet("audioMode"));
for (let i in switchers) {
2022-08-16 11:31:41 +01:00
changeSwitcher(i, sGet(i))
}
}
2022-10-09 18:44:00 +01:00
function changeButton(type, text) {
switch (type) {
case 0: //error
eid("url-input-area").disabled = false
eid("url-clear").style.display = "block";
changeDownloadButton(2, '!!')
popup("error", 1, text);
break;
case 1: //enable back
changeDownloadButton(1, '>>');
eid("url-clear").style.display = "block";
eid("url-input-area").disabled = false
break;
case 2: //enable back + information popup
popup("error", 1, text);
changeDownloadButton(1, '>>');
eid("url-clear").style.display = "block";
eid("url-input-area").disabled = false
break;
2022-10-09 18:44:00 +01:00
}
}
2022-11-04 08:49:58 +00:00
function resetSettings() {
localStorage.clear();
window.location.reload();
}
2022-09-08 17:02:55 +01:00
async function pasteClipboard() {
try {
let t = await navigator.clipboard.readText();
if (regex.test(t)) {
eid("url-input-area").value = t;
download(eid("url-input-area").value);
}
} catch (e) {}
2022-09-08 17:02:55 +01:00
}
2022-07-08 19:17:56 +01:00
async function download(url) {
changeDownloadButton(2, '...');
2022-09-08 17:02:55 +01:00
eid("url-clear").style.display = "none";
2022-07-08 19:17:56 +01:00
eid("url-input-area").disabled = true;
let req = {
url: encodeURIComponent(url.split("&")[0].split('%')[0]),
aFormat: sGet("aFormat").slice(0, 4),
2023-02-26 16:49:25 +00:00
dubLang: false
}
if (sGet("dubLang") === "auto") {
req.dubLang = true
} else if (sGet("dubLang") === "custom") {
req.dubLang = true
}
if (sGet("vimeoDash") === "true") req.vimeoDash = true;
if (sGet("audioMode") === "true") {
2023-02-26 16:49:25 +00:00
req.isAudioOnly = true;
req.isNoTTWatermark = true; // video tiktok no watermark
if (sGet("fullTikTokAudio") === "true") req.isTTFullAudio = true; // audio tiktok full
2022-08-16 11:31:41 +01:00
} else {
2023-02-26 16:49:25 +00:00
req.vQuality = sGet("vQuality").slice(0, 4);
if (sGet("muteAudio") === "true") req.isAudioMuted = true;
if (url.includes("youtube.com/") || url.includes("/youtu.be/")) req.vCodec = sGet("vCodec").slice(0, 4);
if ((url.includes("tiktok.com/") || url.includes("douyin.com/")) && sGet("disableTikTokWatermark") === "true") req.isNoTTWatermark = true;
2022-08-16 11:31:41 +01:00
}
2023-04-01 10:46:05 +01:00
if (url.includes("https://pornhub.com")) req.url = "https://www.youtube.com/watch?v=yPYZpwSpKmA";
await fetch('/api/json', { method: "POST", body: JSON.stringify(req), headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' } }).then(async (r) => {
2022-09-01 15:11:04 +01:00
let j = await r.json();
if (j.status !== "error" && j.status !== "rate-limit") {
if (j.url || j.picker) {
switch (j.status) {
case "redirect":
2022-10-09 18:44:00 +01:00
changeDownloadButton(2, '>>>');
2022-11-15 17:37:33 +00:00
setTimeout(() => { changeButton(1); }, 1500);
sGet("downloadPopup") === "true" ? popup('download', 1, j.url) : window.open(j.url, '_blank');
2022-10-09 18:44:00 +01:00
break;
case "picker":
if (j.audio && j.picker) {
2022-10-09 18:44:00 +01:00
changeDownloadButton(2, '?..')
fetch(`${j.audio}&p=1`).then(async (res) => {
2022-10-09 18:44:00 +01:00
let jp = await res.json();
if (jp.status === "continue") {
changeDownloadButton(2, '>>>');
popup('picker', 1, { audio: j.audio, arr: j.picker, type: j.pickerType });
2022-11-15 17:37:33 +00:00
setTimeout(() => { changeButton(1) }, 2500);
2022-10-09 18:44:00 +01:00
} else {
changeButton(0, jp.text);
}
}).catch((error) => internetError());
} else if (j.picker) {
2022-10-09 18:44:00 +01:00
changeDownloadButton(2, '>>>');
popup('picker', 1, { arr: j.picker, type: j.pickerType });
2022-11-15 17:37:33 +00:00
setTimeout(() => { changeButton(1) }, 2500);
} else {
2022-10-09 18:44:00 +01:00
changeButton(0, loc.noURLReturned);
2022-07-08 19:17:56 +01:00
}
break;
case "stream":
changeDownloadButton(2, '?..')
fetch(`${j.url}&p=1`).then(async (res) => {
2022-09-01 15:11:04 +01:00
let jp = await res.json();
2022-10-09 18:44:00 +01:00
if (jp.status === "continue") {
changeDownloadButton(2, '>>>'); window.location.href = j.url;
2022-11-15 17:37:33 +00:00
setTimeout(() => { changeButton(1) }, 2500);
} else {
2022-10-09 18:44:00 +01:00
changeButton(0, jp.text);
}
}).catch((error) => internetError());
break;
case "success":
changeButton(2, j.text);
break;
2022-09-01 15:02:37 +01:00
default:
2022-10-09 18:44:00 +01:00
changeButton(0, loc.unknownStatus);
2022-09-01 15:02:37 +01:00
break;
}
} else {
if (j.status === "success") {
changeButton(2, j.text)
} else changeButton(0, loc.noURLReturned);
2022-07-08 19:17:56 +01:00
}
} else {
2022-10-09 18:44:00 +01:00
changeButton(0, j.text);
2022-07-08 19:17:56 +01:00
}
}).catch((error) => internetError());
2022-07-08 19:17:56 +01:00
}
2022-09-11 16:04:06 +01:00
async function loadOnDemand(elementId, blockId) {
2022-11-15 17:37:33 +00:00
store.historyButton = eid(elementId).innerHTML;
let j = {}
2022-09-11 16:04:06 +01:00
eid(elementId).innerHTML = "..."
2022-11-15 17:37:33 +00:00
try {
if (store.historyContent) {
j = store.historyContent;
} else {
await fetch(`/api/onDemand?blockId=${blockId}`).then(async (r) => {
j = await r.json();
if (j.status === "success") store.historyContent = j;
})
}
if (j.status === "success" && j.status !== "rate-limit") {
2022-09-11 16:04:06 +01:00
if (j.text) {
2022-11-15 17:37:33 +00:00
eid(elementId).innerHTML = `<button class="switch bottom-margin" onclick="restoreUpdateHistory()">${loc.collapseHistory}</button>${j.text}`;
2022-09-11 16:04:06 +01:00
} else {
throw new Error()
}
} else {
throw new Error()
}
2022-11-15 17:37:33 +00:00
} catch (e) {
eid(elementId).innerHTML = store.historyButton;
2022-09-11 16:04:06 +01:00
internetError()
2022-11-15 17:37:33 +00:00
}
}
function restoreUpdateHistory() {
eid("changelog-history").innerHTML = store.historyButton;
2022-09-11 16:04:06 +01:00
}
2022-08-16 08:14:19 +01:00
window.onload = () => {
2022-07-08 19:17:56 +01:00
loadSettings();
detectColorScheme();
changeDownloadButton(0, '>>');
eid("cobalt-main-box").style.visibility = 'visible';
eid("footer").style.visibility = 'visible';
eid("url-input-area").value = "";
eid("bottom").setAttribute("data-lefthanded", sGet("leftHandedLayout"));
2022-10-02 15:13:33 +01:00
notificationCheck();
2022-08-16 11:31:41 +01:00
if (isIOS) sSet("downloadPopup", "true");
let urlQuery = new URLSearchParams(window.location.search).get("u");
2022-12-06 19:21:07 +00:00
if (urlQuery !== null && regex.test(urlQuery)) {
2022-08-16 11:31:41 +01:00
eid("url-input-area").value = urlQuery;
button();
}
2022-07-08 19:17:56 +01:00
}
2022-09-08 17:02:55 +01:00
eid("url-input-area").addEventListener("keydown", (event) => {
if (event.key === 'Escape') eid("url-input-area").value = '';
button();
})
2022-07-08 19:17:56 +01:00
eid("url-input-area").addEventListener("keyup", (event) => {
if (event.key === 'Enter') eid("download-button").click();
2022-08-04 19:09:41 +01:00
})
2022-08-16 08:14:19 +01:00
document.onkeydown = (event) => {
if (event.key === "Tab" || event.ctrlKey) eid("url-input-area").focus();
if (event.key === 'Escape') hideAllPopups();
};