const ua = navigator.userAgent.toLowerCase();
const isIOS = ua.includes("iphone os") || (ua.includes("mac os") && navigator.maxTouchPoints > 0);
const isMobile = ua.includes("android") || ua.includes("iphone os");
const isSafari = ua.includes("safari/");
const isFirefox = ua.includes("firefox/");
const isOldFirefox = ua.includes("firefox/") && ua.split("firefox/")[1].split('.')[0] < 103;
const switchers = {
"theme": ["auto", "light", "dark"],
"vCodec": ["h264", "av1", "vp9"],
"vQuality": ["720", "max", "2160", "1440", "1080", "480", "360", "240", "144"],
"aFormat": ["mp3", "best", "ogg", "wav", "opus"],
"audioMode": ["false", "true"],
"filenamePattern": ["classic", "pretty", "basic", "nerdy"]
}
const checkboxes = [
"alwaysVisibleButton",
"downloadPopup",
"fullTikTokAudio",
"muteAudio",
"reduceTransparency",
"disableAnimations",
"disableMetadata",
"twitterGif",
"plausible_ignore",
"ytDub",
"tiktokH265"
]
const bottomPopups = ["error", "download"]
let store = {};
const validLink = (link) => {
try {
return /^https:/i.test(new URL(link).protocol);
} catch {
return false
}
}
const fixApiUrl = (url) => {
return url.endsWith('/') ? url.slice(0, -1) : url
}
let apiURL = fixApiUrl(defaultApiUrl);
const changeApi = (url) => {
apiURL = fixApiUrl(url);
return true
}
const eid = (id) => {
return document.getElementById(id)
}
const sGet = (id) =>{
return localStorage.getItem(id)
}
const sSet = (id, value) => {
localStorage.setItem(id, value)
}
const enable = (id) => {
eid(id).dataset.enabled = "true";
}
const disable = (id) => {
eid(id).dataset.enabled = "false";
}
const opposite = (state) => {
return state === "true" ? "false" : "true";
}
const lazyGet = (key) => {
const value = sGet(key);
if (key in switchers) {
if (switchers[key][0] !== value)
return value;
} else if (checkboxes.includes(key)) {
if (value === 'true')
return true;
}
}
const changeDownloadButton = (action, text) => {
switch (action) {
case "hidden": // hidden, but only visible when alwaysVisibleButton is true
eid("download-button").disabled = true
if (sGet("alwaysVisibleButton") === "true") {
eid("download-button").value = '>>'
eid("download-button").style.padding = '0 1rem'
} else {
eid("download-button").value = ''
eid("download-button").style.padding = '0'
}
break;
case "disabled":
eid("download-button").disabled = true
eid("download-button").value = text
eid("download-button").style.padding = '0 1rem'
break;
default:
eid("download-button").disabled = false
eid("download-button").value = '>>'
eid("download-button").style.padding = '0 1rem'
break;
}
}
const button = () => {
let regexTest = validLink(eid("url-input-area").value);
eid("url-clear").style.display = "none";
if ((eid("url-input-area").value).length > 0) {
eid("url-clear").style.display = "block";
}
if (regexTest) {
changeDownloadButton()
} else {
changeDownloadButton("hidden")
}
}
const clearInput = () => {
eid("url-input-area").value = '';
button();
}
const copy = (id, data) => {
let target = document.getElementById(id);
target.classList.add("text-backdrop");
setTimeout(() => {
target.classList.remove("text-backdrop")
}, 600);
if (data) {
navigator.clipboard.writeText(data)
} else {
navigator.clipboard.writeText(e.innerText)
}
}
const share = url => navigator?.share({ url }).catch(() => {});
const detectColorScheme = () => {
let theme = "auto";
let localTheme = sGet("theme");
if (localTheme) {
theme = localTheme;
} else if (!window.matchMedia) {
theme = "dark"
}
document.documentElement.setAttribute("data-theme", theme);
}
const updateFilenamePreview = () => {
let videoFilePreview = ``;
let audioFilePreview = ``;
let resMatch = {
"max": "3840x2160",
"2160": "3840x2160",
"1440": "2560x1440",
"1080": "1920x1080",
"720": "1280x720",
"480": "854x480",
"360": "640x360",
}
switch(sGet("filenamePattern")) {
case "classic":
videoFilePreview = `youtube_dQw4w9WgXcQ_${resMatch[sGet('vQuality')]}_${sGet('vCodec')}`
+ `${sGet("muteAudio") === "true" ? "_mute" : ""}`
+ `.${sGet('vCodec') === "vp9" ? 'webm' : 'mp4'}`;
audioFilePreview = `youtube_dQw4w9WgXcQ_audio`
+ `.${sGet('aFormat') !== "best" ? sGet('aFormat') : 'opus'}`;
break;
case "basic":
videoFilePreview = `${loc.FilenamePreviewVideoTitle} `
+ `(${sGet('vQuality') === "max" ? "2160p" : `${sGet('vQuality')}p`}, `
+ `${sGet('vCodec')}${sGet("muteAudio") === "true" ? ", mute" : ""})`
+ `.${sGet('vCodec') === "vp9" ? 'webm' : 'mp4'}`;
audioFilePreview = `${loc.FilenamePreviewAudioTitle} - ${loc.FilenamePreviewAudioAuthor}`
+ `.${sGet('aFormat') !== "best" ? sGet('aFormat') : 'opus'}`;
break;
case "pretty":
videoFilePreview = `${loc.FilenamePreviewVideoTitle} `
+ `(${sGet('vQuality') === "max" ? "2160p" : `${sGet('vQuality')}p`}, ${sGet('vCodec')}, `
+ `${sGet("muteAudio") === "true" ? "mute, " : ""}youtube)`
+ `.${sGet('vCodec') === "vp9" ? 'webm' : 'mp4'}`;
audioFilePreview = `${loc.FilenamePreviewAudioTitle} - ${loc.FilenamePreviewAudioAuthor} (soundcloud)`
+ `.${sGet('aFormat') !== "best" ? sGet('aFormat') : 'opus'}`;
break;
case "nerdy":
videoFilePreview = `${loc.FilenamePreviewVideoTitle} `
+ `(${sGet('vQuality') === "max" ? "2160p" : `${sGet('vQuality')}p`}, ${sGet('vCodec')}, `
+ `${sGet("muteAudio") === "true" ? "mute, " : ""}youtube, dQw4w9WgXcQ)`
+ `.${sGet('vCodec') === "vp9" ? 'webm' : 'mp4'}`;
audioFilePreview = `${loc.FilenamePreviewAudioTitle} - ${loc.FilenamePreviewAudioAuthor} `
+ `(soundcloud, 1242868615)`
+ `.${sGet('aFormat') !== "best" ? sGet('aFormat') : 'opus'}`;
break;
}
eid("video-filename-text").innerHTML = videoFilePreview
eid("audio-filename-text").innerHTML = audioFilePreview
}
const changeTab = (evnt, tabId, tabClass) => {
if (tabId === "tab-settings-other") updateFilenamePreview();
let tabcontent = document.getElementsByClassName(`tab-content-${tabClass}`);
let tablinks = document.getElementsByClassName(`tab-${tabClass}`);
for (let i = 0; i < tabcontent.length; i++) {
tabcontent[i].dataset.enabled = "false";
}
for (let i = 0; i < tablinks.length; i++) {
tablinks[i].dataset.enabled = "false";
}
evnt.currentTarget.dataset.enabled = "true";
eid(tabId).dataset.enabled = "true";
eid(tabId).parentElement.scrollTop = 0;
}
const expandCollapsible = (evnt) => {
let classlist = evnt.currentTarget.parentNode.classList;
let c = "expanded";
!classlist.contains(c) ? classlist.add(c) : classlist.remove(c);
}
const hideAllPopups = () => {
let filter = document.getElementsByClassName('popup');
for (let i = 0; i < filter.length; i++) {
filter[i].classList.remove("visible");
}
eid("popup-backdrop").classList.remove("visible");
store.isPopupOpen = false;
// clear the picker
eid("picker-holder").innerHTML = '';
eid("picker-download").href = '/';
eid("picker-download").classList.remove("visible");
}
const popup = (type, action, text) => {
if (action === 1) {
hideAllPopups(); // hide the previous popup before showing a new one
store.isPopupOpen = true;
switch (type) {
case "about":
let tabId = "about";
if (text) tabId = text;
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}')`);
eid("pd-share").setAttribute("onClick", `share('${text}')`);
if (navigator.canShare) eid("pd-share").style.display = "flex";
break;
case "picker":
eid("picker-title").innerHTML = loc.MediaPickerTitle;
eid("picker-subtitle").innerHTML = isMobile ? loc.MediaPickerExplanationPhone : loc.MediaPickerExplanationPC;
switch (text.type) {
case "images":
eid("picker-holder").classList.remove("various");
eid("picker-download").href = text.audio;
eid("picker-download").classList.add("visible");
for (let i in text.arr) {
eid("picker-holder").innerHTML +=
`` +
`` +
``
}
break;
default:
eid("picker-holder").classList.add("various");
for (let i in text.arr) {
eid("picker-holder").innerHTML +=
`` +
`