Spaces:
Running
Running
Update youtube.html
Browse files- youtube.html +101 -32
youtube.html
CHANGED
|
@@ -12,7 +12,7 @@
|
|
| 12 |
@import url('https://fonts.googleapis.com/css2?family=Quicksand:wght@400;600;700&display=swap');
|
| 13 |
|
| 14 |
* { box-sizing: border-box; }
|
| 15 |
-
body { font-family: 'Quicksand', sans-serif; margin: 0; padding: 0; background-color: #f0f8ff; color: #2c3e50; min-height: 100vh; overflow-x: hidden; }
|
| 16 |
|
| 17 |
/* SCALING TOP BAR */
|
| 18 |
.top-bar { position: sticky; top: 0; z-index: 100; background: #fff; padding: clamp(8px, 2vw, 15px) clamp(10px, 3vw, 20px); box-shadow: 0 2px 10px rgba(0,0,0,0.08); display: flex; gap: clamp(5px, 2vw, 10px); align-items: center; width: 100%; }
|
|
@@ -196,6 +196,17 @@
|
|
| 196 |
</div>
|
| 197 |
</div>
|
| 198 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
<div id="yt-playlist-modal" class="modal-overlay">
|
| 200 |
<div class="modal-content">
|
| 201 |
<span class="close-modal" onclick="closeModal('yt-playlist-modal')">✖</span>
|
|
@@ -287,7 +298,6 @@
|
|
| 287 |
let localHist = JSON.parse(localStorage.getItem('yt_search_history') || '[]').filter(q => q.toLowerCase().includes(val.toLowerCase()));
|
| 288 |
const aiSugs = await fetchAiSuggestions(val);
|
| 289 |
|
| 290 |
-
// Combine and limit exactly to 10
|
| 291 |
let combined = [...new Set([...localHist, ...aiSugs])].slice(0, 10);
|
| 292 |
|
| 293 |
searchDropdown.innerHTML = '';
|
|
@@ -314,7 +324,7 @@
|
|
| 314 |
let hist = JSON.parse(localStorage.getItem('yt_search_history') || '[]');
|
| 315 |
hist = hist.filter(q => q.toLowerCase() !== query.toLowerCase());
|
| 316 |
hist.unshift(query);
|
| 317 |
-
if(hist.length > 20) hist.pop();
|
| 318 |
localStorage.setItem('yt_search_history', JSON.stringify(hist));
|
| 319 |
}
|
| 320 |
|
|
@@ -322,7 +332,7 @@
|
|
| 322 |
let hist = JSON.parse(localStorage.getItem('yt_search_history') || '[]');
|
| 323 |
hist = hist.filter(q => q !== query);
|
| 324 |
localStorage.setItem('yt_search_history', JSON.stringify(hist));
|
| 325 |
-
onSearchInput();
|
| 326 |
}
|
| 327 |
|
| 328 |
async function executeSearch(isLoadMore = false) {
|
|
@@ -566,61 +576,111 @@
|
|
| 566 |
function closePlayer() {
|
| 567 |
document.getElementById('player-overlay').style.display = 'none';
|
| 568 |
if(ytPlayer) { try { ytPlayer.stopVideo(); ytPlayer.destroy(); } catch(e){} ytPlayer = null; }
|
| 569 |
-
if (saveInterval) clearTimeout(saveInterval);
|
| 570 |
if(document.fullscreenElement) { document.exitFullscreen().catch(e=>{}); }
|
| 571 |
if (screen.orientation && screen.orientation.unlock) { screen.orientation.unlock().catch(()=>{}); }
|
| 572 |
}
|
| 573 |
|
| 574 |
-
// --- 🚀 THE BULLETPROOF 5-SECOND SAVE ENGINE ---
|
| 575 |
function startSavingHistory() {
|
| 576 |
if (saveInterval) clearTimeout(saveInterval);
|
| 577 |
-
|
| 578 |
async function saveTick() {
|
| 579 |
-
// 1. Rigorous Safety Check: Is the player alive and playing?
|
| 580 |
if(ytPlayer && typeof ytPlayer.getPlayerState === 'function' && ytPlayer.getPlayerState() === YT.PlayerState.PLAYING) {
|
| 581 |
-
|
| 582 |
const ct = ytPlayer.getCurrentTime();
|
| 583 |
-
|
| 584 |
-
// 2. Secondary Safety Check: Did we get a valid time reading?
|
| 585 |
if (ct && ct > 0) {
|
| 586 |
currentVideoData.timeWatched = ct;
|
| 587 |
const dur = ytPlayer.getDuration() || cachedDurations[currentVideoData.id] || 0;
|
| 588 |
currentVideoData.total_secs = dur;
|
| 589 |
|
| 590 |
let cloudSuccess = false;
|
| 591 |
-
|
| 592 |
-
// 3. Supabase Sync with Fallback Protection
|
| 593 |
if(supabaseClient) {
|
| 594 |
try {
|
| 595 |
const { error } = await supabaseClient.from('watch_history').upsert({ id: currentVideoData.id, title: currentVideoData.title, thumb: currentVideoData.thumb, author: currentVideoData.author, time_watched: ct, total_secs: dur, last_watched: new Date().toISOString() });
|
| 596 |
-
if(!error) cloudSuccess = true;
|
| 597 |
-
|
| 598 |
-
} catch(e) {
|
| 599 |
-
console.log("Supabase unavailable, routing to local storage.");
|
| 600 |
-
}
|
| 601 |
}
|
| 602 |
-
|
| 603 |
-
// 4. Guaranteed Local Save if Cloud Fails
|
| 604 |
if(!cloudSuccess) {
|
| 605 |
try {
|
| 606 |
let hist = await localforage.getItem('watch_history') || {};
|
| 607 |
hist[currentVideoData.id] = { ...currentVideoData, lastWatched: new Date().toISOString() };
|
| 608 |
await localforage.setItem('watch_history', hist);
|
| 609 |
-
} catch(e) {
|
| 610 |
-
console.error("Critical storage failure.");
|
| 611 |
-
}
|
| 612 |
}
|
| 613 |
}
|
| 614 |
}
|
| 615 |
-
|
| 616 |
-
// 5. Recursive loop (Runs every 5 seconds)
|
| 617 |
saveInterval = setTimeout(saveTick, 5000);
|
| 618 |
}
|
| 619 |
-
|
| 620 |
-
// Initiate the loop
|
| 621 |
saveInterval = setTimeout(saveTick, 5000);
|
| 622 |
}
|
| 623 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 624 |
async function loadHistory() {
|
| 625 |
const content = document.getElementById('history-content'); content.innerHTML = "Loading database...";
|
| 626 |
let histObj = {};
|
|
@@ -636,7 +696,7 @@
|
|
| 636 |
|
| 637 |
if(Object.keys(histObj).length === 0) { histObj = await localforage.getItem('watch_history') || {}; }
|
| 638 |
let keys = Object.keys(histObj);
|
| 639 |
-
if(keys.length === 0) { content.innerHTML = "<p>History
|
| 640 |
content.innerHTML = ""; let animDelay = 0;
|
| 641 |
|
| 642 |
keys.sort((a,b) => new Date(histObj[b].lastWatched) - new Date(histObj[a].lastWatched)).forEach(key => {
|
|
@@ -647,17 +707,26 @@
|
|
| 647 |
let progressHtml = pct > 0 ? `<div class="watch-progress-bg"><div class="watch-progress-fill" style="width: ${pct}%;"></div></div>` : '';
|
| 648 |
let timeFmt = val.total_secs ? formatSecs(val.total_secs) : val.duration || '';
|
| 649 |
|
|
|
|
| 650 |
content.innerHTML += `
|
| 651 |
-
<div class="result-item" style="padding:10px; animation-delay: ${animDelay}s;"
|
| 652 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 653 |
<img class="thumb" src="${val.thumb}">
|
| 654 |
${progressHtml}
|
| 655 |
<div class="dur-badge" style="z-index:4;">${timeFmt}</div>
|
| 656 |
</div>
|
| 657 |
-
|
| 658 |
-
|
|
|
|
| 659 |
<div style="font-size:0.8em; color:#e53935; font-weight:bold;">▶ Resumes at: ${formatSecs(val.timeWatched)}</div>
|
| 660 |
</div>
|
|
|
|
|
|
|
| 661 |
</div>`;
|
| 662 |
animDelay += 0.05;
|
| 663 |
});
|
|
|
|
| 12 |
@import url('https://fonts.googleapis.com/css2?family=Quicksand:wght@400;600;700&display=swap');
|
| 13 |
|
| 14 |
* { box-sizing: border-box; }
|
| 15 |
+
body { font-family: 'Quicksand', sans-serif; margin: 0; padding: 0; background-color: #f0f8ff; color: #2c3e50; min-height: 100vh; overflow-x: hidden; user-select: none; -webkit-user-select: none; }
|
| 16 |
|
| 17 |
/* SCALING TOP BAR */
|
| 18 |
.top-bar { position: sticky; top: 0; z-index: 100; background: #fff; padding: clamp(8px, 2vw, 15px) clamp(10px, 3vw, 20px); box-shadow: 0 2px 10px rgba(0,0,0,0.08); display: flex; gap: clamp(5px, 2vw, 10px); align-items: center; width: 100%; }
|
|
|
|
| 196 |
</div>
|
| 197 |
</div>
|
| 198 |
|
| 199 |
+
<!-- NEW: HISTORY ACTION MODAL (Delete / Transfer) -->
|
| 200 |
+
<div id="history-action-modal" class="modal-overlay" style="z-index: 2500;">
|
| 201 |
+
<div class="modal-content" style="max-width: 400px; text-align: center;">
|
| 202 |
+
<span class="close-modal" onclick="closeModal('history-action-modal')">✖</span>
|
| 203 |
+
<h3 style="color:#ab47bc; margin-top:0;">History Options</h3>
|
| 204 |
+
<div id="history-action-details" style="margin-bottom: 20px;"></div>
|
| 205 |
+
<button class="btn-action" style="width:100%; background:#26a69a; margin-bottom:10px;" onclick="transferHistoryItem()">📁 Transfer to Playlist</button>
|
| 206 |
+
<button class="btn-action" style="width:100%; background:#e53935;" onclick="deleteHistoryItem()">🗑️ Delete from History</button>
|
| 207 |
+
</div>
|
| 208 |
+
</div>
|
| 209 |
+
|
| 210 |
<div id="yt-playlist-modal" class="modal-overlay">
|
| 211 |
<div class="modal-content">
|
| 212 |
<span class="close-modal" onclick="closeModal('yt-playlist-modal')">✖</span>
|
|
|
|
| 298 |
let localHist = JSON.parse(localStorage.getItem('yt_search_history') || '[]').filter(q => q.toLowerCase().includes(val.toLowerCase()));
|
| 299 |
const aiSugs = await fetchAiSuggestions(val);
|
| 300 |
|
|
|
|
| 301 |
let combined = [...new Set([...localHist, ...aiSugs])].slice(0, 10);
|
| 302 |
|
| 303 |
searchDropdown.innerHTML = '';
|
|
|
|
| 324 |
let hist = JSON.parse(localStorage.getItem('yt_search_history') || '[]');
|
| 325 |
hist = hist.filter(q => q.toLowerCase() !== query.toLowerCase());
|
| 326 |
hist.unshift(query);
|
| 327 |
+
if(hist.length > 20) hist.pop();
|
| 328 |
localStorage.setItem('yt_search_history', JSON.stringify(hist));
|
| 329 |
}
|
| 330 |
|
|
|
|
| 332 |
let hist = JSON.parse(localStorage.getItem('yt_search_history') || '[]');
|
| 333 |
hist = hist.filter(q => q !== query);
|
| 334 |
localStorage.setItem('yt_search_history', JSON.stringify(hist));
|
| 335 |
+
onSearchInput();
|
| 336 |
}
|
| 337 |
|
| 338 |
async function executeSearch(isLoadMore = false) {
|
|
|
|
| 576 |
function closePlayer() {
|
| 577 |
document.getElementById('player-overlay').style.display = 'none';
|
| 578 |
if(ytPlayer) { try { ytPlayer.stopVideo(); ytPlayer.destroy(); } catch(e){} ytPlayer = null; }
|
| 579 |
+
if (saveInterval) clearTimeout(saveInterval);
|
| 580 |
if(document.fullscreenElement) { document.exitFullscreen().catch(e=>{}); }
|
| 581 |
if (screen.orientation && screen.orientation.unlock) { screen.orientation.unlock().catch(()=>{}); }
|
| 582 |
}
|
| 583 |
|
|
|
|
| 584 |
function startSavingHistory() {
|
| 585 |
if (saveInterval) clearTimeout(saveInterval);
|
|
|
|
| 586 |
async function saveTick() {
|
|
|
|
| 587 |
if(ytPlayer && typeof ytPlayer.getPlayerState === 'function' && ytPlayer.getPlayerState() === YT.PlayerState.PLAYING) {
|
|
|
|
| 588 |
const ct = ytPlayer.getCurrentTime();
|
|
|
|
|
|
|
| 589 |
if (ct && ct > 0) {
|
| 590 |
currentVideoData.timeWatched = ct;
|
| 591 |
const dur = ytPlayer.getDuration() || cachedDurations[currentVideoData.id] || 0;
|
| 592 |
currentVideoData.total_secs = dur;
|
| 593 |
|
| 594 |
let cloudSuccess = false;
|
|
|
|
|
|
|
| 595 |
if(supabaseClient) {
|
| 596 |
try {
|
| 597 |
const { error } = await supabaseClient.from('watch_history').upsert({ id: currentVideoData.id, title: currentVideoData.title, thumb: currentVideoData.thumb, author: currentVideoData.author, time_watched: ct, total_secs: dur, last_watched: new Date().toISOString() });
|
| 598 |
+
if(!error) cloudSuccess = true; else throw error;
|
| 599 |
+
} catch(e) {}
|
|
|
|
|
|
|
|
|
|
| 600 |
}
|
|
|
|
|
|
|
| 601 |
if(!cloudSuccess) {
|
| 602 |
try {
|
| 603 |
let hist = await localforage.getItem('watch_history') || {};
|
| 604 |
hist[currentVideoData.id] = { ...currentVideoData, lastWatched: new Date().toISOString() };
|
| 605 |
await localforage.setItem('watch_history', hist);
|
| 606 |
+
} catch(e) {}
|
|
|
|
|
|
|
| 607 |
}
|
| 608 |
}
|
| 609 |
}
|
|
|
|
|
|
|
| 610 |
saveInterval = setTimeout(saveTick, 5000);
|
| 611 |
}
|
|
|
|
|
|
|
| 612 |
saveInterval = setTimeout(saveTick, 5000);
|
| 613 |
}
|
| 614 |
|
| 615 |
+
// --- 🚀 NEW: HISTORY LONG-PRESS & ACTION LOGIC ---
|
| 616 |
+
let pressTimer;
|
| 617 |
+
let isLongPressAction = false;
|
| 618 |
+
let activeHistoryVideo = null;
|
| 619 |
+
|
| 620 |
+
function startHistoryPress(event, safeData) {
|
| 621 |
+
isLongPressAction = false;
|
| 622 |
+
// Ignore right-clicks
|
| 623 |
+
if (event.type === 'mousedown' && event.button !== 0) return;
|
| 624 |
+
|
| 625 |
+
pressTimer = setTimeout(() => {
|
| 626 |
+
isLongPressAction = true;
|
| 627 |
+
openHistoryOptions(safeData);
|
| 628 |
+
}, 1200); // 1.2 second hold
|
| 629 |
+
}
|
| 630 |
+
|
| 631 |
+
function cancelHistoryPress() {
|
| 632 |
+
if (pressTimer) clearTimeout(pressTimer);
|
| 633 |
+
}
|
| 634 |
+
|
| 635 |
+
function handleHistoryClick(event, safeData) {
|
| 636 |
+
if(isLongPressAction) {
|
| 637 |
+
// Prevent playing the video if we just opened the modal
|
| 638 |
+
setTimeout(() => { isLongPressAction = false; }, 100);
|
| 639 |
+
return;
|
| 640 |
+
}
|
| 641 |
+
playVid(safeData);
|
| 642 |
+
}
|
| 643 |
+
|
| 644 |
+
function openHistoryOptions(safeData) {
|
| 645 |
+
activeHistoryVideo = JSON.parse(decodeURIComponent(safeData));
|
| 646 |
+
const detailsDiv = document.getElementById('history-action-details');
|
| 647 |
+
detailsDiv.innerHTML = `
|
| 648 |
+
<img src="${activeHistoryVideo.thumb}" style="width: 100%; border-radius: 10px; margin-bottom: 10px; aspect-ratio: 16/9; object-fit: cover; box-shadow: 0 4px 10px rgba(0,0,0,0.2);">
|
| 649 |
+
<div style="font-weight: 700; font-size: 15px; color: #2c3e50;">${activeHistoryVideo.title}</div>
|
| 650 |
+
`;
|
| 651 |
+
|
| 652 |
+
// Trigger haptic feedback if the device supports it
|
| 653 |
+
if (navigator.vibrate) { navigator.vibrate(50); }
|
| 654 |
+
openModal('history-action-modal');
|
| 655 |
+
}
|
| 656 |
+
|
| 657 |
+
async function deleteHistoryItem() {
|
| 658 |
+
if (!activeHistoryVideo) return;
|
| 659 |
+
const vidId = activeHistoryVideo.id;
|
| 660 |
+
|
| 661 |
+
// 1. Delete from Supabase Cloud
|
| 662 |
+
if (supabaseClient) {
|
| 663 |
+
try { await supabaseClient.from('watch_history').delete().eq('id', vidId); } catch(e) { console.error(e); }
|
| 664 |
+
}
|
| 665 |
+
|
| 666 |
+
// 2. Delete from Local Cache
|
| 667 |
+
try {
|
| 668 |
+
let hist = await localforage.getItem('watch_history') || {};
|
| 669 |
+
if (hist[vidId]) { delete hist[vidId]; }
|
| 670 |
+
await localforage.setItem('watch_history', hist);
|
| 671 |
+
} catch(e) {}
|
| 672 |
+
|
| 673 |
+
closeModal('history-action-modal');
|
| 674 |
+
loadHistory(); // Instantly refresh the UI
|
| 675 |
+
}
|
| 676 |
+
|
| 677 |
+
function transferHistoryItem() {
|
| 678 |
+
closeModal('history-action-modal');
|
| 679 |
+
// Overwrite selection queue with just this video
|
| 680 |
+
selectedVideos = [activeHistoryVideo];
|
| 681 |
+
initiateAddToPlaylist(); // Opens the destination target modal
|
| 682 |
+
}
|
| 683 |
+
|
| 684 |
async function loadHistory() {
|
| 685 |
const content = document.getElementById('history-content'); content.innerHTML = "Loading database...";
|
| 686 |
let histObj = {};
|
|
|
|
| 696 |
|
| 697 |
if(Object.keys(histObj).length === 0) { histObj = await localforage.getItem('watch_history') || {}; }
|
| 698 |
let keys = Object.keys(histObj);
|
| 699 |
+
if(keys.length === 0) { content.innerHTML = "<p style='text-align:center; color:#94a3b8;'>History is empty.</p>"; return; }
|
| 700 |
content.innerHTML = ""; let animDelay = 0;
|
| 701 |
|
| 702 |
keys.sort((a,b) => new Date(histObj[b].lastWatched) - new Date(histObj[a].lastWatched)).forEach(key => {
|
|
|
|
| 707 |
let progressHtml = pct > 0 ? `<div class="watch-progress-bg"><div class="watch-progress-fill" style="width: ${pct}%;"></div></div>` : '';
|
| 708 |
let timeFmt = val.total_secs ? formatSecs(val.total_secs) : val.duration || '';
|
| 709 |
|
| 710 |
+
// NEW: Injected Pointer Events for Long Press & 3-Dot Quick Action Button
|
| 711 |
content.innerHTML += `
|
| 712 |
+
<div class="result-item" style="padding:10px; animation-delay: ${animDelay}s; position: relative;"
|
| 713 |
+
onpointerdown="startHistoryPress(event, '${safeData}')"
|
| 714 |
+
onpointerup="cancelHistoryPress()"
|
| 715 |
+
onpointerleave="cancelHistoryPress()"
|
| 716 |
+
onpointercancel="cancelHistoryPress()">
|
| 717 |
+
|
| 718 |
+
<div class="thumb-wrapper" onclick="handleHistoryClick(event, '${safeData}')">
|
| 719 |
<img class="thumb" src="${val.thumb}">
|
| 720 |
${progressHtml}
|
| 721 |
<div class="dur-badge" style="z-index:4;">${timeFmt}</div>
|
| 722 |
</div>
|
| 723 |
+
|
| 724 |
+
<div class="info-col" onclick="handleHistoryClick(event, '${safeData}')" style="padding-right: 40px;">
|
| 725 |
+
<div class="title">${val.title}</div>
|
| 726 |
<div style="font-size:0.8em; color:#e53935; font-weight:bold;">▶ Resumes at: ${formatSecs(val.timeWatched)}</div>
|
| 727 |
</div>
|
| 728 |
+
|
| 729 |
+
<button onclick="openHistoryOptions('${safeData}')" style="position: absolute; right: 5px; top: 50%; transform: translateY(-50%); background: transparent; border: none; font-size: 24px; color: #b0bec5; cursor: pointer; padding: 15px 10px; z-index: 10;">⋮</button>
|
| 730 |
</div>`;
|
| 731 |
animDelay += 0.05;
|
| 732 |
});
|