Skip to content

Commit 954e452

Browse files
Update imagefap_autopager.user.js
1 parent 18cde8a commit 954e452

1 file changed

Lines changed: 46 additions & 25 deletions

File tree

NSFW/imagefap_autopager.user.js

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ==UserScript==
22
// @name ImageFap Auto-Pagination
33
// @namespace imagefap.autopager
4-
// @version 1.0
4+
// @version 1.1
55
// @description Load ImageFap gallery pages in batches.
66
// @match http*://www.imagefap.com/pictures/*
77
// @grant none
@@ -17,19 +17,21 @@
1717
const mainGallery = document.querySelector('#gallery');
1818
if (!mainGallery) return;
1919

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);
2323

24+
let maxPage = 0;
25+
const pageLinks = [...mainGallery.querySelectorAll('a[href*="page="]')];
2426
pageLinks.forEach(a => {
25-
const m = a.href.match(/page=(\d+)/);
27+
const m = a.href.match(/[\?&]page=(\d+)/);
2628
if (m) maxPage = Math.max(maxPage, parseInt(m[1], 10));
2729
});
2830

29-
console.log('[ImageFap] Pages detected:', maxPage + 1);
31+
console.log('[ImageFap] Initial pages detected:', maxPage + 1);
3032

31-
const loadedPages = new Set([0]);
32-
let nextPageToLoad = 1;
33+
const loadedPages = new Set([currentPage]);
34+
let nextPageToLoad = currentPage + 1;
3335
const BATCH_SIZE = 10;
3436
let loading = false;
3537

@@ -57,6 +59,19 @@
5759
const gallery = doc.querySelector('#gallery');
5860
if (!gallery) return;
5961

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+
6075
const wrapper = document.createElement('div');
6176
wrapper.style.marginTop = '30px';
6277

@@ -72,24 +87,28 @@
7287
mainGallery.parentNode.appendChild(wrapper);
7388
}
7489

75-
async function loadNextBatch() {
90+
async function loadNextBatch(size = BATCH_SIZE) {
7691
if (loading) return;
7792
loading = true;
7893

7994
let loadedThisBatch = 0;
8095

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();
89111
}
90-
91-
loading = false;
92-
updateButton();
93112
}
94113

95114
function updateButton() {
@@ -99,12 +118,11 @@
99118
button.style.opacity = '0.7';
100119
} else {
101120
button.textContent = 'Continue loading 👇';
121+
button.disabled = false;
102122
}
103123
}
104124

105-
// Create control button
106125
const button = document.createElement('button');
107-
button.textContent = 'Continue loading 👇';
108126
button.style.cssText = `
109127
display: block;
110128
margin: 50px auto;
@@ -118,11 +136,14 @@
118136
cursor: pointer;
119137
`;
120138

121-
button.addEventListener('click', loadNextBatch);
139+
button.addEventListener('click', () => loadNextBatch(BATCH_SIZE));
122140

123141
document.body.appendChild(button);
124142

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);
127148

128149
})();

0 commit comments

Comments
 (0)