infinite scroll auto-loader with javascript in broser console

 

You want to emulate an infinite scroll auto-loader directly from the browser console, i.e., force the page to keep fetching more content until it exhausts everything. This is a very common pattern on social feeds, e-commerce sites, etc.

Here’s the general approach in raw JavaScript you can paste into the console:

(function autoScrollAndLoad() {
    let lastHeight = 0;
    let sameCount = 0;

    function scrollStep() {
        window.scrollTo(0, document.body.scrollHeight);

        // Wait for network & rendering
        setTimeout(() => {
            const newHeight = document.body.scrollHeight;

            if (newHeight !== lastHeight) {
                lastHeight = newHeight;
                sameCount = 0; // reset stagnation counter
                scrollStep(); // keep going
            } else {
                sameCount++;
                if (sameCount < 5) {
                    // try a few more times in case of lazy load delays
                    scrollStep();
                } else {
                    console.log("No more content to load.");
                }
            }
        }, 1500); // adjust timeout depending on site speed
    }

    scrollStep();
})();

Explanation

  • window.scrollTo forces the page to scroll to the bottom.

  • setTimeout gives time for asynchronous content (AJAX, fetch, GraphQL, etc.) to load.

  • document.body.scrollHeight changes if new content is appended. If it doesn’t grow after several retries (sameCount), we assume no more data is being fetched.

  • The timeout and retry limit (sameCount) can be tweaked depending on how stubborn the site’s lazy loader is.

Variants

  • If the site uses a “Load More” button instead of auto-scroll, you can replace the scroll logic with:

(function autoClickLoadMore() {
    const interval = setInterval(() => {
        let btn = document.querySelector("button.load-more, .load-more");
        if (btn) {
            btn.click();
            console.log("Clicked Load More...");
        } else {
            clearInterval(interval);
            console.log("No more 'Load More' button found.");
        }
    }, 2000);
})();

Do you want me to tailor this so it dumps all the loaded content into a single variable (e.g., scrape text/images/links) once the scrolling finishes, instead of just visually scrolling?

 

posted @ 2025-09-15 09:14  profesor  阅读(8)  评论(0)    收藏  举报