feat: move scrollDown & scrollUp inside window
This commit is contained in:
@@ -369,6 +369,8 @@ async function clickNextPagination(selector, scrapedData, limit) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add the record to the scrapedData array
|
||||||
scrapedData.push(record);
|
scrapedData.push(record);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -425,4 +427,43 @@ async function clickNextPagination(selector, scrapedData, limit) {
|
|||||||
return results;
|
return results;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
window.scrollDown = async(selector, limit) {
|
||||||
|
let previousHeight = 0;
|
||||||
|
let itemsLoaded = 0;
|
||||||
|
|
||||||
|
while (itemsLoaded < limit) {
|
||||||
|
window.scrollBy(0, window.innerHeight);
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||||
|
|
||||||
|
const currentHeight = document.body.scrollHeight;
|
||||||
|
|
||||||
|
if (currentHeight === previousHeight) {
|
||||||
|
break; // No more items to load
|
||||||
|
}
|
||||||
|
|
||||||
|
previousHeight = currentHeight;
|
||||||
|
itemsLoaded += document.querySelectorAll(selector).length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
window.scrollUp = async(selector, limit) {
|
||||||
|
let previousHeight = 0;
|
||||||
|
let itemsLoaded = 0;
|
||||||
|
|
||||||
|
while (itemsLoaded < limit) {
|
||||||
|
window.scrollBy(0, -window.innerHeight);
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||||
|
|
||||||
|
const currentHeight = document.body.scrollHeight;
|
||||||
|
|
||||||
|
if (currentHeight === previousHeight) {
|
||||||
|
break; // No more items to load
|
||||||
|
}
|
||||||
|
|
||||||
|
previousHeight = currentHeight;
|
||||||
|
itemsLoaded += document.querySelectorAll(selector).length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
})(window);
|
})(window);
|
||||||
Reference in New Issue
Block a user