Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save hackingbutlegal/1fb253f80c829daf5f5aaf6e8c5dc502 to your computer and use it in GitHub Desktop.

Select an option

Save hackingbutlegal/1fb253f80c829daf5f5aaf6e8c5dc502 to your computer and use it in GitHub Desktop.
// Function to scroll to bottom with configurable parameters
function scrollToBottom({
scrollInterval = 100, // How often to scroll in milliseconds
scrollStep = 150, // How many pixels to scroll each time
maxScrolls = 1000 // Safety limit to prevent infinite loops
} = {}) {
return new Promise((resolve, reject) => {
let scrollCount = 0;
const previousHeight = document.documentElement.scrollHeight;
const scroll = setInterval(() => {
// Scroll down by step size
window.scrollBy(0, scrollStep);
scrollCount++;
// Get current scroll position and page height
const scrollPosition = window.scrollY + window.innerHeight;
const currentHeight = document.documentElement.scrollHeight;
// Check if we've reached the bottom (no height change after scroll)
if (scrollPosition >= currentHeight && currentHeight === previousHeight) {
clearInterval(scroll);
resolve('Reached the bottom of the page');
}
// Stop if we hit the maximum number of scrolls
if (scrollCount >= maxScrolls) {
clearInterval(scroll);
resolve('Reached maximum scroll limit');
}
}, scrollInterval);
});
}
// Usage example
scrollToBottom({
scrollInterval: 100, // Scroll every 150ms
scrollStep: 150, // Scroll 150px each time
maxScrolls: 5000 // Maximum 5000 scroll attempts
}).then(result => console.log(result));
@hackingbutlegal
Copy link
Author

for (let id = setTimeout(function(){}, 0); id > 0; id--) {
clearInterval(id);
}

@hackingbutlegal
Copy link
Author

Avoid rate limit:

scrollInterval: 1500, // Scroll every 1500ms
scrollStep: 500, // Scroll 500px each time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment