Aichatbot12321 commited on
Commit
e8678ea
·
verified ·
1 Parent(s): d4ea436

Update youtube.html

Browse files
Files changed (1) hide show
  1. youtube.html +121 -71
youtube.html CHANGED
@@ -227,7 +227,7 @@
227
  <div class="search-dropdown" id="searchDropdown"></div>
228
  </div>
229
  <button class="btn-action" onclick="executeSearch(false)">Search</button>
230
- <button class="btn-action" style="background:#5c6bc0; padding: clamp(8px, 2vw, 8px);" onclick="openHomepageSettings()" title="Homepage Settings">⚙</button>
231
  </div>
232
 
233
  <div class="filter-container">
@@ -446,6 +446,33 @@
446
  document.querySelectorAll('#results .result-item').forEach(item => { item.style.display = (currentFilter === 'all' || currentFilter === item.getAttribute('data-type')) ? 'flex' : 'none'; });
447
  }
448
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
449
  // --- HOMEPAGE & VIDEO PLAYBACK SETTINGS ---
450
  function getVideoSettings() {
451
  try {
@@ -489,7 +516,11 @@
489
  saveVideoSettings(vSettings);
490
 
491
  closeModal('homepage-settings-modal');
492
- loadDefaultHomepageContent();
 
 
 
 
493
  }
494
 
495
  function loadDefaultHomepageContent() {
@@ -506,16 +537,7 @@
506
  async function loadHistoryToMainWorkspace() {
507
  const resultsDiv = document.getElementById('results');
508
  resultsDiv.innerHTML = "<h3 style='text-align: center; color: #26a69a;'>Loading Watch History...</h3>";
509
- let histObj = {};
510
- if(supabaseClient) {
511
- try {
512
- const { data, error } = await supabaseClient.from('watch_history').select('*').order('last_watched', { ascending: false });
513
- if(!error && data) {
514
- data.forEach(row => { histObj[row.id] = { id: row.id, title: row.title, thumb: row.thumb, author: row.author, timeWatched: row.time_watched, total_secs: row.total_secs, lastWatched: row.last_watched }; });
515
- }
516
- } catch(e) {}
517
- }
518
- if(Object.keys(histObj).length === 0) { histObj = await localforage.getItem('watch_history') || {}; }
519
  let keys = Object.keys(histObj);
520
  resultsDiv.innerHTML = "<h3 style='color:#26a69a; margin-bottom: 15px;'>🕒 Your Watch History</h3>";
521
  if(keys.length === 0) { resultsDiv.innerHTML += "<p style='color:#94a3b8;'>History is empty.</p>"; return; }
@@ -539,7 +561,7 @@
539
  </div>
540
  <div class="info-col" onclick="handleHistoryClick(event, '${safeData}')" style="padding-right: 40px;">
541
  <div class="title">${val.title}</div>
542
- <div class="hist-time-text" style="font-size:0.8em; color:#e53935; font-weight:bold;">▶ Resumes at: ${formatSecs(val.timeWatched)}</div>
543
  </div>
544
  <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>
545
  </div>`;
@@ -610,12 +632,9 @@
610
  const val = searchInput.value.trim();
611
  if(!val) { searchDropdown.style.display = 'none'; return; }
612
 
613
- // Instantly render local history suggestions (0ms latency!)
614
  renderSuggestions(val, [], true);
615
-
616
  clearTimeout(suggestionTimeout);
617
 
618
- // Fast 300ms debounce to fetch AI suggestions from /api/other
619
  suggestionTimeout = setTimeout(async () => {
620
  const aiSugs = await fetchAiSuggestions(val);
621
  renderSuggestions(val, aiSugs, false);
@@ -659,7 +678,6 @@
659
  searchDropdown.style.display = 'none';
660
  playVid(mockData);
661
 
662
- // Asynchronously resolve actual title so Watch History saves the real name!
663
  fetch(`https://noembed.com/embed?url=https://www.youtube.com/watch?v=${videoId}`)
664
  .then(res => res.json())
665
  .then(meta => {
@@ -680,23 +698,39 @@
680
  if(!isLoadMore) resultsDiv.innerHTML = "";
681
  if (!data.items || data.items.length === 0) { if(!isLoadMore) resultsDiv.innerHTML = "<h3>No results found.</h3>"; return; }
682
 
 
683
  let animDelay = 0;
 
684
  data.items.forEach(item => {
685
  if(item.total_secs) cachedDurations[item.id] = item.total_secs;
686
  const safeData = encodeURIComponent(JSON.stringify(item));
687
  const isVid = item.type === 'video';
688
  const clickAction = isVid ? `playVid('${safeData}')` : `openYoutubePlaylist('${item.id}', '${item.title.replace(/'/g, "\\'")}')`;
689
 
 
 
 
 
 
 
 
 
 
 
 
 
690
  resultsDiv.innerHTML += `
691
  <div class="result-item" data-type="${item.type}" style="border-left-color: ${isVid ? '#29b6f6' : '#ab47bc'}; animation-delay: ${animDelay}s;">
692
  <input type="checkbox" class="vid-checkbox" onchange="toggleSelect(this, '${safeData}')">
693
  <div class="thumb-wrapper" onclick="${clickAction}">
694
  <img class="thumb" src="${item.thumb}">
 
695
  ${isVid ? `<div class="dur-badge">${item.duration}</div>` : `<div class="dur-badge">PLAYLIST</div>`}
696
  </div>
697
  <div class="info-col">
698
  <div class="title" onclick="${clickAction}">${isVid ? '' : '🔀 '}${item.title}</div>
699
  <div style="color: #546e7a; font-size: clamp(12px, 3vw, 0.9em); margin-top:5px;">By <span class="author">${item.author}</span></div>
 
700
  </div>
701
  </div>`;
702
  animDelay += 0.05;
@@ -721,19 +755,22 @@
721
  if(!data.videos || data.videos.length === 0) { contentDiv.innerHTML = "<p>Private or Empty Array.</p>"; return; }
722
  currentYTPlaylistVids = data.videos;
723
 
724
- let hist = await localforage.getItem('watch_history') || {};
725
  let animDelay = 0;
726
 
727
  data.videos.forEach((item, index) => {
728
- if (hist[item.id] && hist[item.id].timeWatched) {
729
- item.timeWatched = hist[item.id].timeWatched;
730
- item.total_secs = hist[item.id].total_secs;
731
- }
732
-
733
  let pct = 0;
734
- if (item.total_secs && item.timeWatched) pct = Math.min(100, Math.max(0, (item.timeWatched / item.total_secs) * 100));
735
- let progressHtml = pct > 0 ? `<div class="watch-progress-bg"><div class="watch-progress-fill" style="width: ${pct}%;"></div></div>` : '';
736
- let resumeText = pct > 0 ? `<div class="hist-time-text" style="font-size:0.8em; color:#e53935; font-weight:bold; margin-top:4px;">▶ Resumes at: ${formatSecs(item.timeWatched)}</div>` : '';
 
 
 
 
 
 
 
737
 
738
  const safeData = encodeURIComponent(JSON.stringify(item));
739
  contentDiv.innerHTML += `
@@ -876,19 +913,22 @@
876
  </div>`;
877
  if(vids.length === 0) { html += "<p>Array empty.</p>"; }
878
 
879
- let hist = await localforage.getItem('watch_history') || {};
880
  let animDelay = 0;
881
 
882
  vids.forEach((item, index) => {
883
- if (hist[item.id] && hist[item.id].timeWatched) {
884
- item.timeWatched = hist[item.id].timeWatched;
885
- item.total_secs = hist[item.id].total_secs || item.total_secs || 0;
886
- }
887
-
888
  let pct = 0;
889
- if (item.total_secs && item.timeWatched) pct = Math.min(100, Math.max(0, (item.timeWatched / item.total_secs) * 100));
890
- let progressHtml = pct > 0 ? `<div class="watch-progress-bg"><div class="watch-progress-fill" style="width: ${pct}%;"></div></div>` : '';
891
- let resumeText = pct > 0 ? `<div class="hist-time-text" style="font-size:0.8em; color:#e53935; font-weight:bold; margin-top:4px;">▶ Resumes at: ${formatSecs(item.timeWatched)}</div>` : '';
 
 
 
 
 
 
 
892
 
893
  const safeData = encodeURIComponent(JSON.stringify(item));
894
  html += `
@@ -974,24 +1014,24 @@
974
  }
975
 
976
  let currentOrientationIsLandscape = true;
977
- async function toggleOrientation() {
 
 
 
 
 
978
  try {
979
- if (screen.orientation && screen.orientation.lock) {
980
- if (currentOrientationIsLandscape) {
981
- await screen.orientation.lock('portrait').catch(()=>{});
982
- currentOrientationIsLandscape = false;
983
- } else {
984
- await screen.orientation.lock('landscape').catch(()=>{});
985
- currentOrientationIsLandscape = true;
986
- }
987
- } else if (screen.orientation && screen.orientation.unlock) {
988
- screen.orientation.unlock();
989
- }
990
  } catch(e) {
991
- console.log("Orientation toggle notice:", e);
992
  }
993
  }
994
 
 
 
 
 
 
995
  async function applyVideoOrientationSettings() {
996
  const settings = getVideoSettings();
997
  const targetOrientation = settings.orientation || 'landscape';
@@ -1007,10 +1047,8 @@
1007
  } else {
1008
  const oppositeOrientation = targetOrientation === 'landscape' ? 'portrait' : 'landscape';
1009
 
1010
- // Lock to initial target
1011
  await screen.orientation.lock(targetOrientation).catch(()=>{});
1012
 
1013
- // Sequence toggles
1014
  for (let i = 1; i < sequenceCount; i++) {
1015
  await new Promise(r => setTimeout(r, 400));
1016
  const nextOrient = (i % 2 === 1) ? oppositeOrientation : targetOrientation;
@@ -1035,17 +1073,25 @@
1035
  document.addEventListener('fullscreenchange', () => {
1036
  if (!document.fullscreenElement && screen.orientation && screen.orientation.unlock) {
1037
  screen.orientation.unlock();
1038
- }
 
 
1039
  });
1040
 
1041
  document.addEventListener('visibilitychange', () => {
1042
- if (document.visibilityState === 'hidden' && screen.orientation && screen.orientation.unlock) {
1043
- screen.orientation.unlock();
1044
- } else if (document.visibilityState === 'visible' && document.getElementById('player-overlay').style.display === 'block') {
1045
- applyVideoOrientationSettings();
1046
  }
1047
  });
1048
 
 
 
 
 
 
 
 
 
1049
  function startMultiplePlayback(idArray) {
1050
  const overlay = document.getElementById('player-overlay');
1051
  overlay.style.display = 'block';
@@ -1233,10 +1279,10 @@
1233
  const thumbWrap = domItem.querySelector('.thumb-wrapper');
1234
  if(thumbWrap) thumbWrap.insertAdjacentHTML('beforeend', `<div class="watch-progress-bg"><div class="watch-progress-fill" style="width: ${pct}%;"></div></div>`);
1235
  const infoCol = domItem.querySelector('.info-col');
1236
- if(infoCol) infoCol.insertAdjacentHTML('beforeend', `<div class="hist-time-text" style="font-size:0.8em; color:#e53935; font-weight:bold; margin-top:4px;">▶ Resumes at: ${formatSecs(ct)}</div>`);
1237
  } else {
1238
  fill.style.width = `${pct}%`;
1239
- if (timeText) timeText.innerText = `▶ Resumes at: ${formatSecs(ct)}`;
1240
  }
1241
  } else if (id.startsWith('hist-item') && document.getElementById('history-modal').style.display === 'block') {
1242
  loadHistory();
@@ -1288,25 +1334,14 @@
1288
  } catch(e) {}
1289
  closeModal('history-action-modal');
1290
  if (document.getElementById('history-modal').style.display === 'block') loadHistory();
1291
- else loadDefaultHomepageContent();
1292
  }
1293
 
1294
  function transferHistoryItem() { closeModal('history-action-modal'); selectedVideos = [activeHistoryVideo]; initiateAddToPlaylist(); }
1295
 
1296
  async function loadHistory() {
1297
  const content = document.getElementById('history-content'); content.innerHTML = "Loading database...";
1298
- let histObj = {};
1299
-
1300
- if(supabaseClient) {
1301
- try {
1302
- const { data, error } = await supabaseClient.from('watch_history').select('*').order('last_watched', { ascending: false });
1303
- if(!error && data) {
1304
- data.forEach(row => { histObj[row.id] = { id: row.id, title: row.title, thumb: row.thumb, author: row.author, timeWatched: row.time_watched, total_secs: row.total_secs, lastWatched: row.last_watched }; });
1305
- }
1306
- } catch(e) { console.error("Supabase Load Failed, fetching Local."); }
1307
- }
1308
-
1309
- if(Object.keys(histObj).length === 0) { histObj = await localforage.getItem('watch_history') || {}; }
1310
  let keys = Object.keys(histObj);
1311
  if(keys.length === 0) { content.innerHTML = "<p style='text-align:center; color:#94a3b8;'>History is empty.</p>"; return; }
1312
  content.innerHTML = ""; let animDelay = 0;
@@ -1331,7 +1366,7 @@
1331
  </div>
1332
  <div class="info-col" onclick="handleHistoryClick(event, '${safeData}')" style="padding-right: 40px;">
1333
  <div class="title">${val.title}</div>
1334
- <div class="hist-time-text" style="font-size:0.8em; color:#e53935; font-weight:bold;">▶ Resumes at: ${formatSecs(val.timeWatched)}</div>
1335
  </div>
1336
  <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>
1337
  </div>`;
@@ -1363,22 +1398,37 @@
1363
  const data = await res.json();
1364
  resultsDiv.innerHTML = "<h3 style='color:#ab47bc; margin-bottom: 15px;'>Recommended for You</h3>";
1365
  if (data.items && data.items.length > 0) {
 
1366
  let animDelay = 0;
1367
  data.items.forEach(item => {
1368
  const safeData = encodeURIComponent(JSON.stringify(item));
1369
  const isVid = item.type === 'video';
1370
  const clickAction = isVid ? `playVid('${safeData}')` : `openYoutubePlaylist('${item.id}', '${item.title.replace(/'/g, "\\'")}')`;
1371
 
 
 
 
 
 
 
 
 
 
 
 
 
1372
  resultsDiv.innerHTML += `
1373
  <div class="result-item" data-type="${item.type}" style="border-left-color: ${isVid ? '#29b6f6' : '#ab47bc'}; animation-delay: ${animDelay}s;">
1374
  <input type="checkbox" class="vid-checkbox" onchange="toggleSelect(this, '${safeData}')">
1375
  <div class="thumb-wrapper" onclick="${clickAction}">
1376
  <img class="thumb" src="${item.thumb}">
 
1377
  ${isVid ? `<div class="dur-badge">${item.duration}</div>` : `<div class="dur-badge">PLAYLIST</div>`}
1378
  </div>
1379
  <div class="info-col">
1380
  <div class="title" onclick="${clickAction}">${isVid ? '' : '🔀 '}${item.title}</div>
1381
  <div style="color: #546e7a; font-size: clamp(12px, 3vw, 0.9em); margin-top:5px;">By <span class="author">${item.author}</span></div>
 
1382
  </div>
1383
  </div>`;
1384
  animDelay += 0.05;
 
227
  <div class="search-dropdown" id="searchDropdown"></div>
228
  </div>
229
  <button class="btn-action" onclick="executeSearch(false)">Search</button>
230
+ <button class="btn-action" style="background:#5c6bc0; padding: clamp(8px, 2vw, 12px);" onclick="openHomepageSettings()" title="Settings">⚙</button>
231
  </div>
232
 
233
  <div class="filter-container">
 
446
  document.querySelectorAll('#results .result-item').forEach(item => { item.style.display = (currentFilter === 'all' || currentFilter === item.getAttribute('data-type')) ? 'flex' : 'none'; });
447
  }
448
 
449
+ // --- WATCH HISTORY MAP HELPER FOR PROGRESS DISPLAY ---
450
+ async function getHistoryMap() {
451
+ let histObj = {};
452
+ if (supabaseClient) {
453
+ try {
454
+ const { data, error } = await supabaseClient.from('watch_history').select('*');
455
+ if (!error && data) {
456
+ data.forEach(row => {
457
+ histObj[row.id] = {
458
+ id: row.id,
459
+ title: row.title,
460
+ thumb: row.thumb,
461
+ author: row.author,
462
+ timeWatched: row.time_watched,
463
+ total_secs: row.total_secs,
464
+ lastWatched: row.last_watched
465
+ };
466
+ });
467
+ }
468
+ } catch(e) {}
469
+ }
470
+ if (Object.keys(histObj).length === 0) {
471
+ histObj = await localforage.getItem('watch_history') || {};
472
+ }
473
+ return histObj;
474
+ }
475
+
476
  // --- HOMEPAGE & VIDEO PLAYBACK SETTINGS ---
477
  function getVideoSettings() {
478
  try {
 
516
  saveVideoSettings(vSettings);
517
 
518
  closeModal('homepage-settings-modal');
519
+
520
+ // FIX: Only reload homepage if user is not actively viewing a search result!
521
+ if (!currentSearchQuery) {
522
+ loadDefaultHomepageContent();
523
+ }
524
  }
525
 
526
  function loadDefaultHomepageContent() {
 
537
  async function loadHistoryToMainWorkspace() {
538
  const resultsDiv = document.getElementById('results');
539
  resultsDiv.innerHTML = "<h3 style='text-align: center; color: #26a69a;'>Loading Watch History...</h3>";
540
+ let histObj = await getHistoryMap();
 
 
 
 
 
 
 
 
 
541
  let keys = Object.keys(histObj);
542
  resultsDiv.innerHTML = "<h3 style='color:#26a69a; margin-bottom: 15px;'>🕒 Your Watch History</h3>";
543
  if(keys.length === 0) { resultsDiv.innerHTML += "<p style='color:#94a3b8;'>History is empty.</p>"; return; }
 
561
  </div>
562
  <div class="info-col" onclick="handleHistoryClick(event, '${safeData}')" style="padding-right: 40px;">
563
  <div class="title">${val.title}</div>
564
+ <div class="hist-time-text" style="font-size:0.8em; color:#e53935; font-weight:bold; margin-top:4px;">▶ Resumes at: ${formatSecs(val.timeWatched)}</div>
565
  </div>
566
  <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>
567
  </div>`;
 
632
  const val = searchInput.value.trim();
633
  if(!val) { searchDropdown.style.display = 'none'; return; }
634
 
 
635
  renderSuggestions(val, [], true);
 
636
  clearTimeout(suggestionTimeout);
637
 
 
638
  suggestionTimeout = setTimeout(async () => {
639
  const aiSugs = await fetchAiSuggestions(val);
640
  renderSuggestions(val, aiSugs, false);
 
678
  searchDropdown.style.display = 'none';
679
  playVid(mockData);
680
 
 
681
  fetch(`https://noembed.com/embed?url=https://www.youtube.com/watch?v=${videoId}`)
682
  .then(res => res.json())
683
  .then(meta => {
 
698
  if(!isLoadMore) resultsDiv.innerHTML = "";
699
  if (!data.items || data.items.length === 0) { if(!isLoadMore) resultsDiv.innerHTML = "<h3>No results found.</h3>"; return; }
700
 
701
+ let histMap = await getHistoryMap();
702
  let animDelay = 0;
703
+
704
  data.items.forEach(item => {
705
  if(item.total_secs) cachedDurations[item.id] = item.total_secs;
706
  const safeData = encodeURIComponent(JSON.stringify(item));
707
  const isVid = item.type === 'video';
708
  const clickAction = isVid ? `playVid('${safeData}')` : `openYoutubePlaylist('${item.id}', '${item.title.replace(/'/g, "\\'")}')`;
709
 
710
+ let hist = histMap[item.id];
711
+ let pct = 0;
712
+ let progressHtml = '';
713
+ let resumeText = '';
714
+
715
+ if (hist && hist.timeWatched && hist.timeWatched > 0) {
716
+ const totalSecs = hist.total_secs || item.total_secs || 1;
717
+ pct = Math.min(100, Math.max(0, (hist.timeWatched / totalSecs) * 100));
718
+ progressHtml = `<div class="watch-progress-bg"><div class="watch-progress-fill" style="width: ${pct}%;"></div></div>`;
719
+ resumeText = `<div class="hist-time-text" style="font-size:0.8em; color:#e53935; font-weight:bold; margin-top:4px;">▶ Watched: ${formatSecs(hist.timeWatched)} / ${formatSecs(totalSecs)}</div>`;
720
+ }
721
+
722
  resultsDiv.innerHTML += `
723
  <div class="result-item" data-type="${item.type}" style="border-left-color: ${isVid ? '#29b6f6' : '#ab47bc'}; animation-delay: ${animDelay}s;">
724
  <input type="checkbox" class="vid-checkbox" onchange="toggleSelect(this, '${safeData}')">
725
  <div class="thumb-wrapper" onclick="${clickAction}">
726
  <img class="thumb" src="${item.thumb}">
727
+ ${progressHtml}
728
  ${isVid ? `<div class="dur-badge">${item.duration}</div>` : `<div class="dur-badge">PLAYLIST</div>`}
729
  </div>
730
  <div class="info-col">
731
  <div class="title" onclick="${clickAction}">${isVid ? '' : '🔀 '}${item.title}</div>
732
  <div style="color: #546e7a; font-size: clamp(12px, 3vw, 0.9em); margin-top:5px;">By <span class="author">${item.author}</span></div>
733
+ ${resumeText}
734
  </div>
735
  </div>`;
736
  animDelay += 0.05;
 
755
  if(!data.videos || data.videos.length === 0) { contentDiv.innerHTML = "<p>Private or Empty Array.</p>"; return; }
756
  currentYTPlaylistVids = data.videos;
757
 
758
+ let histMap = await getHistoryMap();
759
  let animDelay = 0;
760
 
761
  data.videos.forEach((item, index) => {
762
+ let hist = histMap[item.id];
 
 
 
 
763
  let pct = 0;
764
+ let progressHtml = '';
765
+ let resumeText = '';
766
+
767
+ if (hist && hist.timeWatched && hist.timeWatched > 0) {
768
+ item.timeWatched = hist.timeWatched;
769
+ item.total_secs = hist.total_secs || item.total_secs || 1;
770
+ pct = Math.min(100, Math.max(0, (hist.timeWatched / item.total_secs) * 100));
771
+ progressHtml = `<div class="watch-progress-bg"><div class="watch-progress-fill" style="width: ${pct}%;"></div></div>`;
772
+ resumeText = `<div class="hist-time-text" style="font-size:0.8em; color:#e53935; font-weight:bold; margin-top:4px;">▶ Watched: ${formatSecs(hist.timeWatched)} / ${formatSecs(item.total_secs)}</div>`;
773
+ }
774
 
775
  const safeData = encodeURIComponent(JSON.stringify(item));
776
  contentDiv.innerHTML += `
 
913
  </div>`;
914
  if(vids.length === 0) { html += "<p>Array empty.</p>"; }
915
 
916
+ let histMap = await getHistoryMap();
917
  let animDelay = 0;
918
 
919
  vids.forEach((item, index) => {
920
+ let hist = histMap[item.id];
 
 
 
 
921
  let pct = 0;
922
+ let progressHtml = '';
923
+ let resumeText = '';
924
+
925
+ if (hist && hist.timeWatched && hist.timeWatched > 0) {
926
+ item.timeWatched = hist.timeWatched;
927
+ item.total_secs = hist.total_secs || item.total_secs || 1;
928
+ pct = Math.min(100, Math.max(0, (hist.timeWatched / item.total_secs) * 100));
929
+ progressHtml = `<div class="watch-progress-bg"><div class="watch-progress-fill" style="width: ${pct}%;"></div></div>`;
930
+ resumeText = `<div class="hist-time-text" style="font-size:0.8em; color:#e53935; font-weight:bold; margin-top:4px;">▶ Watched: ${formatSecs(hist.timeWatched)} / ${formatSecs(item.total_secs)}</div>`;
931
+ }
932
 
933
  const safeData = encodeURIComponent(JSON.stringify(item));
934
  html += `
 
1014
  }
1015
 
1016
  let currentOrientationIsLandscape = true;
1017
+
1018
+ async function syncPlayerOrientation() {
1019
+ if (document.getElementById('player-overlay').style.display !== 'block') return;
1020
+ if (!screen.orientation || !screen.orientation.lock) return;
1021
+
1022
+ const target = currentOrientationIsLandscape ? 'landscape' : 'portrait';
1023
  try {
1024
+ await screen.orientation.lock(target).catch(()=>{});
 
 
 
 
 
 
 
 
 
 
1025
  } catch(e) {
1026
+ console.log("Re-lock orientation note:", e);
1027
  }
1028
  }
1029
 
1030
+ async function toggleOrientation() {
1031
+ currentOrientationIsLandscape = !currentOrientationIsLandscape;
1032
+ await syncPlayerOrientation();
1033
+ }
1034
+
1035
  async function applyVideoOrientationSettings() {
1036
  const settings = getVideoSettings();
1037
  const targetOrientation = settings.orientation || 'landscape';
 
1047
  } else {
1048
  const oppositeOrientation = targetOrientation === 'landscape' ? 'portrait' : 'landscape';
1049
 
 
1050
  await screen.orientation.lock(targetOrientation).catch(()=>{});
1051
 
 
1052
  for (let i = 1; i < sequenceCount; i++) {
1053
  await new Promise(r => setTimeout(r, 400));
1054
  const nextOrient = (i % 2 === 1) ? oppositeOrientation : targetOrientation;
 
1073
  document.addEventListener('fullscreenchange', () => {
1074
  if (!document.fullscreenElement && screen.orientation && screen.orientation.unlock) {
1075
  screen.orientation.unlock();
1076
+ } else if (document.fullscreenElement) {
1077
+ syncPlayerOrientation();
1078
+ }
1079
  });
1080
 
1081
  document.addEventListener('visibilitychange', () => {
1082
+ if (document.visibilityState === 'visible' && document.getElementById('player-overlay').style.display === 'block') {
1083
+ syncPlayerOrientation();
 
 
1084
  }
1085
  });
1086
 
1087
+ window.addEventListener('pageshow', () => {
1088
+ syncPlayerOrientation();
1089
+ });
1090
+
1091
+ window.addEventListener('focus', () => {
1092
+ syncPlayerOrientation();
1093
+ });
1094
+
1095
  function startMultiplePlayback(idArray) {
1096
  const overlay = document.getElementById('player-overlay');
1097
  overlay.style.display = 'block';
 
1279
  const thumbWrap = domItem.querySelector('.thumb-wrapper');
1280
  if(thumbWrap) thumbWrap.insertAdjacentHTML('beforeend', `<div class="watch-progress-bg"><div class="watch-progress-fill" style="width: ${pct}%;"></div></div>`);
1281
  const infoCol = domItem.querySelector('.info-col');
1282
+ if(infoCol) infoCol.insertAdjacentHTML('beforeend', `<div class="hist-time-text" style="font-size:0.8em; color:#e53935; font-weight:bold; margin-top:4px;">▶ Watched: ${formatSecs(ct)} / ${formatSecs(dur)}</div>`);
1283
  } else {
1284
  fill.style.width = `${pct}%`;
1285
+ if (timeText) timeText.innerText = `▶ Watched: ${formatSecs(ct)} / ${formatSecs(dur)}`;
1286
  }
1287
  } else if (id.startsWith('hist-item') && document.getElementById('history-modal').style.display === 'block') {
1288
  loadHistory();
 
1334
  } catch(e) {}
1335
  closeModal('history-action-modal');
1336
  if (document.getElementById('history-modal').style.display === 'block') loadHistory();
1337
+ else if (!currentSearchQuery) loadDefaultHomepageContent();
1338
  }
1339
 
1340
  function transferHistoryItem() { closeModal('history-action-modal'); selectedVideos = [activeHistoryVideo]; initiateAddToPlaylist(); }
1341
 
1342
  async function loadHistory() {
1343
  const content = document.getElementById('history-content'); content.innerHTML = "Loading database...";
1344
+ let histObj = await getHistoryMap();
 
 
 
 
 
 
 
 
 
 
 
1345
  let keys = Object.keys(histObj);
1346
  if(keys.length === 0) { content.innerHTML = "<p style='text-align:center; color:#94a3b8;'>History is empty.</p>"; return; }
1347
  content.innerHTML = ""; let animDelay = 0;
 
1366
  </div>
1367
  <div class="info-col" onclick="handleHistoryClick(event, '${safeData}')" style="padding-right: 40px;">
1368
  <div class="title">${val.title}</div>
1369
+ <div class="hist-time-text" style="font-size:0.8em; color:#e53935; font-weight:bold; margin-top:4px;">▶ Watched: ${formatSecs(val.timeWatched)} / ${formatSecs(val.total_secs || 0)}</div>
1370
  </div>
1371
  <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>
1372
  </div>`;
 
1398
  const data = await res.json();
1399
  resultsDiv.innerHTML = "<h3 style='color:#ab47bc; margin-bottom: 15px;'>Recommended for You</h3>";
1400
  if (data.items && data.items.length > 0) {
1401
+ let histMap = await getHistoryMap();
1402
  let animDelay = 0;
1403
  data.items.forEach(item => {
1404
  const safeData = encodeURIComponent(JSON.stringify(item));
1405
  const isVid = item.type === 'video';
1406
  const clickAction = isVid ? `playVid('${safeData}')` : `openYoutubePlaylist('${item.id}', '${item.title.replace(/'/g, "\\'")}')`;
1407
 
1408
+ let hist = histMap[item.id];
1409
+ let pct = 0;
1410
+ let progressHtml = '';
1411
+ let resumeText = '';
1412
+
1413
+ if (hist && hist.timeWatched && hist.timeWatched > 0) {
1414
+ const totalSecs = hist.total_secs || item.total_secs || 1;
1415
+ pct = Math.min(100, Math.max(0, (hist.timeWatched / totalSecs) * 100));
1416
+ progressHtml = `<div class="watch-progress-bg"><div class="watch-progress-fill" style="width: ${pct}%;"></div></div>`;
1417
+ resumeText = `<div class="hist-time-text" style="font-size:0.8em; color:#e53935; font-weight:bold; margin-top:4px;">▶ Watched: ${formatSecs(hist.timeWatched)} / ${formatSecs(totalSecs)}</div>`;
1418
+ }
1419
+
1420
  resultsDiv.innerHTML += `
1421
  <div class="result-item" data-type="${item.type}" style="border-left-color: ${isVid ? '#29b6f6' : '#ab47bc'}; animation-delay: ${animDelay}s;">
1422
  <input type="checkbox" class="vid-checkbox" onchange="toggleSelect(this, '${safeData}')">
1423
  <div class="thumb-wrapper" onclick="${clickAction}">
1424
  <img class="thumb" src="${item.thumb}">
1425
+ ${progressHtml}
1426
  ${isVid ? `<div class="dur-badge">${item.duration}</div>` : `<div class="dur-badge">PLAYLIST</div>`}
1427
  </div>
1428
  <div class="info-col">
1429
  <div class="title" onclick="${clickAction}">${isVid ? '' : '🔀 '}${item.title}</div>
1430
  <div style="color: #546e7a; font-size: clamp(12px, 3vw, 0.9em); margin-top:5px;">By <span class="author">${item.author}</span></div>
1431
+ ${resumeText}
1432
  </div>
1433
  </div>`;
1434
  animDelay += 0.05;