|
52 | 52 | let isStoppingNow = false; |
53 | 53 | let uiRefreshQueued = false; |
54 | 54 | let suppressObserverUntil = 0; |
| 55 | + let pendingClearAfterStop = false; |
| 56 | + let clearRetryTimer = null; |
55 | 57 |
|
56 | 58 | const seenReleaseLinks = new Set(); |
57 | 59 | const linkCache = new Map(); |
|
432 | 434 | return ''; |
433 | 435 | } |
434 | 436 |
|
435 | | - function getCategory(item) { |
436 | | - const links = Array.from(item.querySelectorAll('.calidad4 a')); |
437 | | - const hrefs = links.map(a => a.href || a.getAttribute('href') || ''); |
438 | | - if (hrefs.some(h => h.includes('tv-packs'))) return 'tv-packs'; |
439 | | - if (hrefs.some(h => h.includes('tv-shows'))) return 'tv-shows'; |
440 | | - return 'movies'; |
441 | | - } |
442 | | - |
443 | 437 | function getItemKey(item) { |
444 | 438 | return item.querySelector('h5 a')?.href || |
445 | 439 | item.querySelector('h5 a')?.textContent?.trim() || |
|
483 | 477 | } |
484 | 478 | } |
485 | 479 |
|
| 480 | + function syncCategorySelectToLocation() { |
| 481 | + const select = document.getElementById('f-category'); |
| 482 | + if (!select) return; |
| 483 | + |
| 484 | + const href = window.location.href.replace(/\/+$/, '/'); |
| 485 | + const options = [ |
| 486 | + 'https://hdencode.org/tag/movies/', |
| 487 | + 'https://hdencode.org/tag/tv-shows/', |
| 488 | + 'https://hdencode.org/tag/tv-packs/', |
| 489 | + 'https://hdencode.org/top-downloads/', |
| 490 | + 'https://hdencode.org/quality/2160p/' |
| 491 | + ]; |
| 492 | + |
| 493 | + const matched = options.find(url => href.startsWith(url)); |
| 494 | + select.value = matched || 'https://hdencode.org/tag/movies/'; |
| 495 | + } |
| 496 | + |
486 | 497 | function getFilterValues() { |
487 | 498 | return { |
488 | 499 | onlyDV: document.getElementById('f-dv')?.checked || false, |
489 | 500 | onlyHDR: document.getElementById('f-hdr')?.checked || false, |
490 | 501 | res: document.getElementById('f-res')?.value || '', |
491 | | - category: document.getElementById('f-category')?.value || '', |
492 | 502 | minRating: parseFloat(document.getElementById('f-rating')?.value) || 0, |
493 | 503 | minSize: parseFloat(document.getElementById('f-minsize')?.value) || 0, |
494 | 504 | maxSize: parseFloat(document.getElementById('f-maxsize')?.value) || Infinity, |
|
501 | 511 | if (f.onlyDV && !hasDV(item)) return false; |
502 | 512 | if (f.onlyHDR && !hasHDR(item)) return false; |
503 | 513 | if (f.res && getResolution(item) !== f.res) return false; |
504 | | - if (f.category && getCategory(item) !== f.category) return false; |
505 | 514 | if (getRating(item) < f.minRating) return false; |
506 | 515 |
|
507 | 516 | const size = getSize(item); |
|
514 | 523 | function saveFilters() { |
515 | 524 | const data = {}; |
516 | 525 | for (const el of document.querySelectorAll(`#${SCRIPT_ID}-bar input, #${SCRIPT_ID}-bar select`)) { |
517 | | - if (el.id === 'f-pagelimit') continue; |
| 526 | + if (el.id === 'f-pagelimit' || el.id === 'f-category') continue; |
518 | 527 | data[el.id] = el.type === 'checkbox' ? el.checked : el.value; |
519 | 528 | } |
520 | 529 | try { |
|
526 | 535 | try { |
527 | 536 | const data = JSON.parse(localStorage.getItem('hdencodeFilters') || '{}'); |
528 | 537 | for (const [id, val] of Object.entries(data)) { |
529 | | - if (id === 'f-pagelimit') continue; |
| 538 | + if (id === 'f-pagelimit' || id === 'f-category') continue; |
530 | 539 | const el = document.getElementById(id); |
531 | 540 | if (!el) continue; |
532 | 541 | if (el.type === 'checkbox') el.checked = val; |
|
684 | 693 |
|
685 | 694 | const stopBtn = document.getElementById('f-stop-loading'); |
686 | 695 | const loadBtn = document.getElementById('f-loadall'); |
| 696 | + |
687 | 697 | if (stopBtn) { |
688 | 698 | stopBtn.style.display = 'none'; |
689 | 699 | stopBtn.disabled = false; |
|
694 | 704 | } |
695 | 705 |
|
696 | 706 | function stopLoading() { |
697 | | - if (!isLoadingPages && !abortController) return; |
| 707 | + if (!isLoadingPages && !abortController) { |
| 708 | + if (pendingClearAfterStop && rootContainer) { |
| 709 | + pendingClearAfterStop = false; |
| 710 | + clearFilters(rootContainer); |
| 711 | + } |
| 712 | + return; |
| 713 | + } |
| 714 | + |
698 | 715 | if (isStoppingNow) return; |
699 | 716 |
|
700 | 717 | const didAbort = abortActiveLoading('user-stop'); |
701 | 718 |
|
702 | 719 | const stopBtn = document.getElementById('f-stop-loading'); |
703 | 720 | const loadBtn = document.getElementById('f-loadall'); |
| 721 | + |
704 | 722 | if (stopBtn) stopBtn.disabled = true; |
705 | 723 | if (loadBtn) loadBtn.disabled = true; |
706 | 724 |
|
|
710 | 728 | if (!didAbort) { |
711 | 729 | finishStopUI(); |
712 | 730 | queueUIRefresh(rootContainer); |
| 731 | + |
| 732 | + if (pendingClearAfterStop && rootContainer) { |
| 733 | + pendingClearAfterStop = false; |
| 734 | + clearFilters(rootContainer); |
| 735 | + } |
713 | 736 | } |
714 | 737 | } |
715 | 738 |
|
716 | 739 | function clearFilters(container) { |
717 | 740 | if (clearInProgress) return; |
| 741 | + |
| 742 | + if (isLoadingPages || abortController || isStoppingNow) { |
| 743 | + pendingClearAfterStop = true; |
| 744 | + stopLoading(); |
| 745 | + |
| 746 | + clearTimeout(clearRetryTimer); |
| 747 | + clearRetryTimer = setTimeout(function waitForStop() { |
| 748 | + if (isLoadingPages || abortController || isStoppingNow) { |
| 749 | + clearRetryTimer = setTimeout(waitForStop, 60); |
| 750 | + return; |
| 751 | + } |
| 752 | + |
| 753 | + pendingClearAfterStop = false; |
| 754 | + clearFilters(container); |
| 755 | + }, 60); |
| 756 | + |
| 757 | + return; |
| 758 | + } |
| 759 | + |
718 | 760 | clearInProgress = true; |
719 | 761 | suppressObserverUntil = Date.now() + 1000; |
720 | 762 |
|
721 | 763 | try { |
722 | 764 | clearTimeout(refreshTimer); |
| 765 | + clearTimeout(clearRetryTimer); |
723 | 766 | pauseObserver(); |
724 | 767 |
|
725 | | - abortActiveLoading('clear'); |
726 | | - |
727 | 768 | for (const el of document.querySelectorAll(`#${SCRIPT_ID}-bar input, #${SCRIPT_ID}-bar select`)) { |
728 | | - if (el.id === 'f-pagelimit') el.value = 'all'; |
729 | | - else if (el.type === 'checkbox') el.checked = false; |
730 | | - else el.value = ''; |
| 769 | + if (el.id === 'f-pagelimit') { |
| 770 | + el.value = 'all'; |
| 771 | + } else if (el.id === 'f-category') { |
| 772 | + continue; |
| 773 | + } else if (el.type === 'checkbox') { |
| 774 | + el.checked = false; |
| 775 | + } else { |
| 776 | + el.value = ''; |
| 777 | + } |
731 | 778 | } |
732 | 779 |
|
733 | | - try { localStorage.removeItem('hdencodeFilters'); } catch (_) {} |
| 780 | + try { |
| 781 | + localStorage.removeItem('hdencodeFilters'); |
| 782 | + } catch (_) {} |
| 783 | + |
| 784 | + syncCategorySelectToLocation(); |
734 | 785 |
|
735 | 786 | const stopBtn = document.getElementById('f-stop-loading'); |
736 | 787 | const loadBtn = document.getElementById('f-loadall'); |
| 788 | + |
737 | 789 | if (stopBtn) { |
738 | 790 | stopBtn.style.display = 'none'; |
739 | 791 | stopBtn.disabled = false; |
740 | 792 | } |
741 | | - if (loadBtn) loadBtn.disabled = false; |
| 793 | + |
| 794 | + if (loadBtn) { |
| 795 | + loadBtn.disabled = false; |
| 796 | + } |
| 797 | + |
| 798 | + abortController = null; |
| 799 | + isLoadingPages = false; |
| 800 | + isStoppingNow = false; |
742 | 801 |
|
743 | 802 | setStatus(''); |
| 803 | + |
744 | 804 | const searchStatus = document.getElementById('f-search-status'); |
745 | 805 | if (searchStatus) { |
746 | 806 | searchStatus.textContent = ''; |
747 | 807 | searchStatus.style.display = 'none'; |
748 | 808 | } |
749 | 809 |
|
750 | | - abortController = null; |
751 | | - isLoadingPages = false; |
752 | | - isStoppingNow = false; |
753 | | - |
754 | 810 | applyFilters(container); |
755 | 811 | styleVisibleResults(container); |
756 | 812 | hideNativePagination(); |
757 | 813 | renderCustomPagination(); |
758 | | - updateEmptyState(container, Array.from(container.querySelectorAll('.fit.item')).some(item => item.style.display !== 'none')); |
| 814 | + updateEmptyState( |
| 815 | + container, |
| 816 | + Array.from(container.querySelectorAll('.fit.item')).some(item => item.style.display !== 'none') |
| 817 | + ); |
759 | 818 | } finally { |
760 | 819 | setTimeout(() => { |
761 | 820 | resumeObserver(); |
762 | 821 | }, 50); |
| 822 | + |
763 | 823 | clearInProgress = false; |
764 | 824 | } |
765 | 825 | } |
|
849 | 909 |
|
850 | 910 | loaded = 1; |
851 | 911 |
|
852 | | - let firstState = applyFilters(container); |
| 912 | + const firstState = applyFilters(container); |
853 | 913 | if (getFilterValues().search) { |
854 | 914 | setStatus(`${firstState.searchMatches} result(s) found so far — scanned ${loaded} page(s)`); |
855 | 915 | } else { |
|
965 | 1025 | } |
966 | 1026 | } |
967 | 1027 |
|
| 1028 | + if (pendingClearAfterStop && !clearInProgress) { |
| 1029 | + setTimeout(() => { |
| 1030 | + if (rootContainer && !isLoadingPages && !abortController && !isStoppingNow) { |
| 1031 | + pendingClearAfterStop = false; |
| 1032 | + clearFilters(rootContainer); |
| 1033 | + } |
| 1034 | + }, 40); |
| 1035 | + } |
| 1036 | + |
968 | 1037 | setTimeout(() => { |
969 | 1038 | const t = document.getElementById('f-load-status')?.textContent || ''; |
970 | 1039 | if ( |
|
1278 | 1347 | </div> |
1279 | 1348 |
|
1280 | 1349 | <div class="fs-toolbar-right"> |
1281 | | - <select id="f-category" style="${INPUT_STYLE} width:110px;"> |
1282 | | - <option value="">All</option> |
1283 | | - <option value="movies">Movies</option> |
1284 | | - <option value="tv-shows">TV Shows</option> |
1285 | | - <option value="tv-packs">TV Packs</option> |
| 1350 | + <select id="f-category" style="${INPUT_STYLE} width:150px;"> |
| 1351 | + <option value="https://hdencode.org/tag/movies/">Movies</option> |
| 1352 | + <option value="https://hdencode.org/tag/tv-shows/">TV Shows</option> |
| 1353 | + <option value="https://hdencode.org/tag/tv-packs/">TV Packs</option> |
| 1354 | + <option value="https://hdencode.org/top-downloads/">Top Downloads</option> |
| 1355 | + <option value="https://hdencode.org/quality/2160p/">4K UHD</option> |
1286 | 1356 | </select> |
1287 | 1357 | </div> |
1288 | 1358 | </div> |
|
1491 | 1561 | }); |
1492 | 1562 |
|
1493 | 1563 | for (const el of bar.querySelectorAll('input, select')) { |
1494 | | - if (el.id === 'f-search') continue; |
| 1564 | + if (el.id === 'f-search' || el.id === 'f-category') continue; |
1495 | 1565 | el.addEventListener('input', () => applyFilters(container)); |
1496 | 1566 | } |
1497 | 1567 |
|
| 1568 | + bar.querySelector('#f-category').addEventListener('change', function () { |
| 1569 | + const targetUrl = this.value; |
| 1570 | + if (targetUrl && window.location.href.replace(/\/+$/, '/') !== targetUrl.replace(/\/+$/, '/')) { |
| 1571 | + window.location.href = targetUrl; |
| 1572 | + } |
| 1573 | + }); |
| 1574 | + |
1498 | 1575 | searchInput.addEventListener('input', () => applyFilters(container)); |
1499 | 1576 |
|
1500 | 1577 | buildGroupDropdown(container); |
1501 | 1578 | loadFilters(); |
| 1579 | + syncCategorySelectToLocation(); |
1502 | 1580 | applyFilters(container); |
1503 | 1581 | injectLinkButtons(container); |
1504 | 1582 | createObserver(); |
|
0 commit comments