poke/css/app.js

245 lines
7.5 KiB
JavaScript
Raw Normal View History

2023-04-12 22:40:06 +02:00
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-3.0-or-later
// Retrieve volume from local storage or set to max if not available
const initialVolume = localStorage.getItem('playerVolume') || 1;
const video = document.getElementById('video');
video.volume = initialVolume;
// Save volume to local storage whenever it changes
video.addEventListener('volumechange', function() {
localStorage.setItem('playerVolume', this.volume);
});
// Get the progress bar and container elements
2023-06-10 10:07:07 +02:00
const progressBar = document.querySelector(".progress-bar");
const progressContainer = document.querySelector(".progress-container");
2023-04-12 22:40:06 +02:00
// Set the initial width of the progress bar to 0%
2023-06-10 10:07:07 +02:00
progressBar.style.width = "0%";
progressContainer.style.display = 'block';
2023-04-12 22:40:06 +02:00
// Attach an event listener to the window object to listen for the 'load' event
window.addEventListener("load", () => {
2023-06-10 10:07:07 +02:00
progressBar.style.width = "100%";
2023-04-12 22:40:06 +02:00
setTimeout(() => {
2023-06-10 10:07:07 +02:00
progressContainer.style.display = 'none';
2023-04-12 22:40:06 +02:00
}, 500);
});
2023-06-10 10:07:07 +02:00
// Lazy load background images
2023-04-12 22:40:06 +02:00
document.addEventListener('DOMContentLoaded', function() {
2023-06-10 10:07:07 +02:00
const bgs = document.querySelectorAll('[data-bg]');
let bgCount = bgs.length;
function loadBg(index) {
const bg = bgs[index];
const bgUrl = bg.getAttribute('data-bg');
bg.style.backgroundImage = `url(${bgUrl})`;
2023-04-12 22:40:06 +02:00
bg.removeAttribute('data-bg');
bg.classList.add('loaded');
}
function lazyLoadBg() {
for (let i = 0; i < bgCount; i++) {
2023-06-10 10:07:07 +02:00
const bg = bgs[i];
const bgRect = bg.getBoundingClientRect();
2023-04-12 22:40:06 +02:00
if (bgRect.top < window.innerHeight && bgRect.bottom > 0) {
loadBg(i);
}
}
}
2023-06-10 10:07:07 +02:00
2023-04-12 22:40:06 +02:00
lazyLoadBg();
2023-06-10 10:07:07 +02:00
2023-04-12 22:40:06 +02:00
window.addEventListener('scroll', lazyLoadBg);
window.addEventListener('resize', lazyLoadBg);
});
2023-06-10 10:07:07 +02:00
// Fade in elements on scroll or fullscreen change
function fadeInElements() {
2023-04-12 22:40:06 +02:00
const elements = document.querySelectorAll('.fade-in');
const viewportHeight = window.innerHeight;
elements.forEach(element => {
const elementTop = element.getBoundingClientRect().top;
const elementBottom = element.getBoundingClientRect().bottom;
const isVisible = (elementTop < viewportHeight && elementBottom > 0);
if (isVisible || document.fullscreenElement) {
element.classList.add('fade-in-active');
}
});
2023-06-10 10:07:07 +02:00
}
2023-04-12 22:40:06 +02:00
window.addEventListener('scroll', fadeInElements);
2023-06-10 10:07:07 +02:00
document.addEventListener('fullscreenchange', fadeInElements);
2023-04-12 22:40:06 +02:00
setInterval(fadeInElements, 500);
2023-06-10 10:07:07 +02:00
// Handle click events on links
2023-04-12 22:40:06 +02:00
const links = document.querySelectorAll('a:not([data-onclick="jump_to_time"])');
links.forEach(link => {
link.addEventListener('click', e => {
if (!link.href.includes('#')) {
2023-06-10 10:07:07 +02:00
e.preventDefault();
2023-04-12 22:40:06 +02:00
const spinner = document.createElement('div');
spinner.classList.add('spinner');
const loading = document.createElement('div');
loading.classList.add('loading');
loading.appendChild(spinner);
document.body.appendChild(loading);
setTimeout(() => {
window.location.href = link.href;
}, 100);
}
});
});
2023-06-10 10:07:07 +02:00
// Handle click events for time-based links
const timeLinks = document.querySelectorAll('a[data-onclick="jump_to_time"]');
timeLinks.forEach(link => {
2023-04-12 22:40:06 +02:00
const href = link.getAttribute('href');
2023-06-10 10:07:07 +02:00
if (link.dataset.jumpTime && href && href.includes('&t=')) {
2023-04-12 22:40:06 +02:00
const params = new URLSearchParams(href.split('?')[1]);
const time = params.get('t');
if (time) {
link.dataset.jumpTime = time;
link.addEventListener('click', jumpToTime);
link.removeAttribute('href');
}
}
});
2023-06-10 10:07:07 +02:00
// Fetch URLs and handle progress saving
const urls = document.querySelectorAll('a[href*="/watch?v="]');
2023-04-12 22:40:06 +02:00
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';
2023-06-10 10:07:07 +02:00
text.classList.add('hide');
2023-04-12 22:40:06 +02:00
document.body.appendChild(text);
2023-06-10 10:07:07 +02:00
document.body.classList.add('blur');
2023-04-12 22:40:06 +02:00
let fetchedCount = 0;
urls.forEach(link => {
const url = new URL(link.href);
2023-06-10 10:07:07 +02:00
if (url.host !== 'www.youtube.com' && url.host !== 'youtube.com' && url.host !== "redirect.poketube.fun") {
2023-04-12 22:40:06 +02:00
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');
2023-06-10 10:07:07 +02:00
document.body.classList.remove('blur');
2023-04-12 22:40:06 +02:00
}
})
.catch(error => {
spinner.classList.add('hide');
text.classList.add('hide');
2023-06-10 10:07:07 +02:00
console.clear()
2023-04-12 22:40:06 +02:00
if (!(error instanceof TypeError && error.message.includes('Failed to fetch'))) {
console.error(`Error fetching ${url.origin}: ${error}`);
}
});
}
});
2023-06-10 10:07:07 +02:00
// Save and resume video progress
2023-04-12 22:40:06 +02:00
const videoId = new URLSearchParams(window.location.search).get('v');
const localStorageKey = `progress-${videoId}`;
function saveProgress() {
localStorage.setItem(localStorageKey, video.currentTime);
}
function removeProgress() {
localStorage.removeItem(localStorageKey);
}
function resumeProgress() {
const progress = localStorage.getItem(localStorageKey);
if (progress) {
video.currentTime = progress;
}
}
video.addEventListener('timeupdate', () => {
2023-06-10 10:07:07 +02:00
if (Math.floor(video.currentTime) % 1 === 0) {
2023-04-12 22:40:06 +02:00
saveProgress();
}
});
video.addEventListener('ended', () => {
removeProgress();
});
window.addEventListener('load', () => {
resumeProgress();
});
2023-06-10 10:07:07 +02:00
// Adjust video element style on fullscreen change
2023-04-12 22:40:06 +02:00
const videoElement = document.getElementById("video");
videoElement.addEventListener("fullscreenchange", () => {
2023-06-10 10:07:07 +02:00
videoElement.style.borderRadius = document.fullscreenElement === videoElement ? "0em" : "16px";
});
2023-04-28 22:36:16 +02:00
2023-06-10 10:07:07 +02:00
// Fetch channel URLs
const channelUrls = document.querySelectorAll('a[href*="/channel?id="]');
2023-04-28 22:36:16 +02:00
let fetchedCountChannel = 0;
2023-06-10 10:07:07 +02:00
channelUrls.forEach(link => {
2023-04-28 22:36:16 +02:00
const url = new URL(link.href);
2023-06-10 10:07:07 +02:00
if (url.host !== 'www.youtube.com' && url.host !== 'youtube.com' && url.host !== "redirect.poketube.fun") {
2023-04-28 22:36:16 +02:00
console.log(`Fetching ${url.origin}`);
fetch(url.href)
.then(response => {
if (response.status === 500) {
// do nothing
}
console.log(`Fetched ${url.origin}`);
fetchedCountChannel++;
console.clear()
2023-06-10 10:07:07 +02:00
if (fetchedCountChannel === channelUrls.length) {
document.body.classList.remove('blur');
2023-04-28 22:36:16 +02:00
}
})
.catch(error => {
2023-06-10 10:07:07 +02:00
console.clear()
2023-04-28 22:36:16 +02:00
if (!(error instanceof TypeError && error.message.includes('Failed to fetch'))) {
console.error(`Error fetching ${url.origin}: ${error}`);
}
});
}
});
2023-06-10 10:07:07 +02:00
// Fetch download URLs
const downloadUrls = document.querySelectorAll('a[href*="/download?v="]');
downloadUrls.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}`);
fetch(url.href)
.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}`);
}
});
}
});
// @license-end