Advertisement
dereksir

Untitled

Mar 7th, 2024
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // function to automatically scroll to the bottom of the page
  2. async function autoScroll(page) {
  3.   await page.evaluate(async () => {
  4.     // use Promise to handle asynchronous scrolling
  5.     await new Promise((resolve, reject) => {
  6.       let totalHeight = 0; // initialize total height scrolled
  7.       const distance = 100; // set distance to scroll each time
  8.       // set interval to continuously scroll the page
  9.       const scrollInterval = setInterval(() => {
  10.         // calculate the total scroll height of the page
  11.         const scrollHeight = document.body.scrollHeight;
  12.         // scroll the page by the specified distance
  13.         window.scrollBy(0, distance);
  14.         // update the total height scrolled
  15.         totalHeight += distance;
  16.         // check if the total height scrolled is equal to or exceeds the scroll height of the page
  17.         if (totalHeight >= scrollHeight) {
  18.           // if so, clear the interval to stop scrolling
  19.           clearInterval(scrollInterval);
  20.           // resolve the Promise to indicate scrolling is complete
  21.           resolve();
  22.         }
  23.       }, 100); // set interval duration
  24.     });
  25.   });
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement