refactor code :3

This commit is contained in:
Ashley 2023-09-09 16:37:04 +00:00
parent 23d1021ff0
commit 18edb4b110

View file

@ -105,51 +105,7 @@ timeLinks.forEach(link => {
} }
}); });
// Fetch URLs and handle progress saving
const urls = document.querySelectorAll('a[href*="/watch?v="]');
const spinner = document.createElement('div');
spinner.id = 'fetch-spinner';
spinner.classList.add('hide');
document.body.appendChild(spinner);
const text = document.createElement('div');
text.id = 'fetch-text';
text.classList.add('hide');
document.body.appendChild(text);
document.body.classList.add('blur');
let fetchedCount = 0;
urls.forEach(link => {
const url = new URL(link.href);
if (url.host !== 'www.youtube.com' && url.host !== 'youtube.com' && url.host !== "redirect.poketube.fun") {
console.log(`Fetching ${url.origin}`);
spinner.classList.remove('hide');
text.classList.remove('hide');
fetch(url.href)
.then(response => {
if (response.status === 500) {
// do nothing
}
console.log(`Fetched ${url.origin}`);
fetchedCount++;
console.clear()
if (fetchedCount === urls.length) {
spinner.classList.add('hide');
text.classList.add('hide');
document.body.classList.remove('blur');
}
})
.catch(error => {
spinner.classList.add('hide');
text.classList.add('hide');
console.clear()
if (!(error instanceof TypeError && error.message.includes('Failed to fetch'))) {
console.error(`Error fetching ${url.origin}: ${error}`);
}
});
}
});
// Save and resume video progress // Save and resume video progress
const videoId = new URLSearchParams(window.location.search).get('v'); const videoId = new URLSearchParams(window.location.search).get('v');
@ -190,11 +146,10 @@ videoElement.addEventListener("fullscreenchange", () => {
videoElement.style.borderRadius = document.fullscreenElement === videoElement ? "0em" : "16px"; videoElement.style.borderRadius = document.fullscreenElement === videoElement ? "0em" : "16px";
}); });
// Fetch channel URLs function fetchUrls(urls) {
const channelUrls = document.querySelectorAll('a[href*="/channel?id="]'); let fetchedCount = 0;
let fetchedCountChannel = 0;
channelUrls.forEach(link => { urls.forEach(link => {
const url = new URL(link.href); const url = new URL(link.href);
if (url.host !== 'www.youtube.com' && url.host !== 'youtube.com' && url.host !== "redirect.poketube.fun") { if (url.host !== 'www.youtube.com' && url.host !== 'youtube.com' && url.host !== "redirect.poketube.fun") {
console.log(`Fetching ${url.origin}`); console.log(`Fetching ${url.origin}`);
@ -204,43 +159,33 @@ channelUrls.forEach(link => {
// do nothing // do nothing
} }
console.log(`Fetched ${url.origin}`); console.log(`Fetched ${url.origin}`);
fetchedCountChannel++; fetchedCount++;
console.clear() console.clear();
if (fetchedCountChannel === channelUrls.length) { if (fetchedCount === urls.length) {
document.body.classList.remove('blur'); document.body.classList.remove('blur');
} }
}) })
.catch(error => { .catch(error => {
console.clear() console.clear();
if (!(error instanceof TypeError && error.message.includes('Failed to fetch'))) { if (!(error instanceof TypeError && error.message.includes('Failed to fetch'))) {
console.error(`Error fetching ${url.origin}: ${error}`); console.error(`Error fetching ${url.origin}: ${error}`);
} }
}); });
} }
}); });
}
// Fetch channel URLs
const channelUrls = document.querySelectorAll('a[href*="/channel?id="]');
fetchUrls(channelUrls);
// Fetch download URLs // Fetch download URLs
const downloadUrls = document.querySelectorAll('a[href*="/download?v="]'); const downloadUrls = document.querySelectorAll('a[href*="/download?v="]');
downloadUrls.forEach(link => { fetchUrls(downloadUrls);
const url = new URL(link.href);
if (url.host !== 'www.youtube.com' && url.host !== 'youtube.com' && url.host !== "redirect.poketube.fun") { // fetch videos urls
console.log(`Fetching ${url.origin}`); const urls = document.querySelectorAll('a[href*="/watch?v="]');
fetch(url.href) fetchUrls(urls);
.then(response => {
if (response.status === 500) {
// do nothing
}
console.log(`Fetched ${url.origin}`);
console.clear()
})
.catch(error => {
console.clear()
if (!(error instanceof TypeError && error.message.includes('Failed to fetch'))) {
console.error(`Error fetching ${url.origin}: ${error}`);
}
});
}
});
var popupMenu = document.getElementById("popupMenu"); var popupMenu = document.getElementById("popupMenu");
var loopOption = document.getElementById("loopOption"); var loopOption = document.getElementById("loopOption");