Thursday, 26 May 2022

My code does not run when the puppeteer is minimized

I have the following code, which works well if I put the maximized navigation window, but when I minimize it it stops working.

More details:

The "scrollDown & scrollTop" functions stop executing when the window is minimized.

'use strict'

const puppeteer = require('puppeteer-core');

const lauchpuppeteer = async () => {
  const browser = await puppeteer.launch({
    defaultViewport: null,
    headless: false,
    userDataDir: `${obj.id}`,
    args: [`--app=${url}`, 
    '--disable-background-timer-throttling',
    '--disable-backgrounding-occluded-windows',
    '--disable-renderer-backgrounding',
    '--no-sandbox']
  });
  const [page] = await browser.pages()
  await page.waitForNavigation();
  await scrollDown(page);
  await page.waitFor(5000);
  await scrollTop(page);

  if (await page.$('li.inline.t-24.t-black.t-normal.break-words') !== null)
    fullname = await page.$eval('li.inline.t-24.t-black.t-normal.break-words', el => el.innerText.replace(/\s+/g, ' ').trim());
  else console.log('The Full Name was not found.'.red);

  }

const scrollTop = async (page) => {
  await page.evaluate(_ => {
    window.scrollTo(0, 0);
  });
}

const scrollDown = async (page) => {
  await page.evaluate(async () => {
    await new Promise((resolve, reject) => {
      var totalHeight = 0;
      var distance = 50;
      var timer = setInterval(() => {
        var scrollHeight = document.body.scrollHeight;
        window.scrollBy(0, distance);
        totalHeight += distance;
        if (totalHeight >= scrollHeight) {
          clearInterval(timer);
          resolve();
        }
      }, 100);
    });
  });
}


from My code does not run when the puppeteer is minimized

No comments:

Post a Comment