|
1 | 1 | // ==UserScript== |
2 | 2 | // @name ImageFap Auto-Pagination |
3 | 3 | // @namespace imagefap.autopager |
4 | | -// @version 1.0 |
| 4 | +// @version 1.1 |
5 | 5 | // @description Load ImageFap gallery pages in batches. |
6 | 6 | // @match http*://www.imagefap.com/pictures/* |
7 | 7 | // @grant none |
|
17 | 17 | const mainGallery = document.querySelector('#gallery'); |
18 | 18 | if (!mainGallery) return; |
19 | 19 |
|
20 | | - // Detect total pages |
21 | | - const pageLinks = [...mainGallery.querySelectorAll('a[href*="&page="]')]; |
22 | | - let maxPage = 0; |
| 20 | + // Detect current page |
| 21 | + const url = new URL(location.href); |
| 22 | + const currentPage = parseInt(url.searchParams.get('page') || '0', 10); |
23 | 23 |
|
| 24 | + let maxPage = 0; |
| 25 | + const pageLinks = [...mainGallery.querySelectorAll('a[href*="page="]')]; |
24 | 26 | pageLinks.forEach(a => { |
25 | | - const m = a.href.match(/page=(\d+)/); |
| 27 | + const m = a.href.match(/[\?&]page=(\d+)/); |
26 | 28 | if (m) maxPage = Math.max(maxPage, parseInt(m[1], 10)); |
27 | 29 | }); |
28 | 30 |
|
29 | | - console.log('[ImageFap] Pages detected:', maxPage + 1); |
| 31 | + console.log('[ImageFap] Initial pages detected:', maxPage + 1); |
30 | 32 |
|
31 | | - const loadedPages = new Set([0]); |
32 | | - let nextPageToLoad = 1; |
| 33 | + const loadedPages = new Set([currentPage]); |
| 34 | + let nextPageToLoad = currentPage + 1; |
33 | 35 | const BATCH_SIZE = 10; |
34 | 36 | let loading = false; |
35 | 37 |
|
|
57 | 59 | const gallery = doc.querySelector('#gallery'); |
58 | 60 | if (!gallery) return; |
59 | 61 |
|
| 62 | + // Update maxPage from newly loaded page |
| 63 | + const newLinks = gallery.querySelectorAll('a[href*="page="]'); |
| 64 | + [...newLinks].forEach(a => { |
| 65 | + const m = a.href.match(/[\?&]page=(\d+)/); |
| 66 | + if (m) { |
| 67 | + const p = parseInt(m[1], 10); |
| 68 | + if (p > maxPage) { |
| 69 | + maxPage = p; |
| 70 | + console.log('[ImageFap] Updated maxPage to', maxPage + 1); |
| 71 | + } |
| 72 | + } |
| 73 | + }); |
| 74 | + |
60 | 75 | const wrapper = document.createElement('div'); |
61 | 76 | wrapper.style.marginTop = '30px'; |
62 | 77 |
|
|
72 | 87 | mainGallery.parentNode.appendChild(wrapper); |
73 | 88 | } |
74 | 89 |
|
75 | | - async function loadNextBatch() { |
| 90 | + async function loadNextBatch(size = BATCH_SIZE) { |
76 | 91 | if (loading) return; |
77 | 92 | loading = true; |
78 | 93 |
|
79 | 94 | let loadedThisBatch = 0; |
80 | 95 |
|
81 | | - while ( |
82 | | - loadedThisBatch < BATCH_SIZE && |
83 | | - nextPageToLoad <= maxPage |
84 | | - ) { |
85 | | - await loadPage(nextPageToLoad); |
86 | | - nextPageToLoad++; |
87 | | - loadedThisBatch++; |
88 | | - await new Promise(r => setTimeout(r, 500)); // throttle |
| 96 | + try { |
| 97 | + while ( |
| 98 | + loadedThisBatch < size && |
| 99 | + nextPageToLoad <= maxPage |
| 100 | + ) { |
| 101 | + await loadPage(nextPageToLoad); |
| 102 | + nextPageToLoad++; |
| 103 | + loadedThisBatch++; |
| 104 | + await new Promise(r => setTimeout(r, 500)); |
| 105 | + } |
| 106 | + } catch (err) { |
| 107 | + console.error('[ImageFap] Error during batch load:', err); |
| 108 | + } finally { |
| 109 | + loading = false; |
| 110 | + updateButton(); |
89 | 111 | } |
90 | | - |
91 | | - loading = false; |
92 | | - updateButton(); |
93 | 112 | } |
94 | 113 |
|
95 | 114 | function updateButton() { |
|
99 | 118 | button.style.opacity = '0.7'; |
100 | 119 | } else { |
101 | 120 | button.textContent = 'Continue loading 👇'; |
| 121 | + button.disabled = false; |
102 | 122 | } |
103 | 123 | } |
104 | 124 |
|
105 | | - // Create control button |
106 | 125 | const button = document.createElement('button'); |
107 | | - button.textContent = 'Continue loading 👇'; |
108 | 126 | button.style.cssText = ` |
109 | 127 | display: block; |
110 | 128 | margin: 50px auto; |
|
118 | 136 | cursor: pointer; |
119 | 137 | `; |
120 | 138 |
|
121 | | - button.addEventListener('click', loadNextBatch); |
| 139 | + button.addEventListener('click', () => loadNextBatch(BATCH_SIZE)); |
122 | 140 |
|
123 | 141 | document.body.appendChild(button); |
124 | 142 |
|
125 | | - // Auto-load first batch |
126 | | - setTimeout(loadNextBatch, 1500); |
| 143 | + // Initial auto-load |
| 144 | + setTimeout(() => { |
| 145 | + const initialSize = currentPage === 0 ? 9 : BATCH_SIZE; |
| 146 | + loadNextBatch(initialSize); |
| 147 | + }, 1500); |
127 | 148 |
|
128 | 149 | })(); |
0 commit comments