All files / src/helpers/spa createSPARequest.js

100% Statements 9/9
0% Branches 0/1
100% Functions 2/2
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44              1x                                               2x 2x       2x 2x 2x 2x 2x 2x    
import puppeteer from "puppeteer"
 
/**
 * This auto scroll pages. It's really useful for any case, but mainly for lazy load pages.
 *
 * @param {PuppeteerPageClass} page https://github.com/puppeteer/puppeteer/blob/master/docs/api.md#class-pages
 */
const autoScroll = async (page) => {
  /* istanbul ignore next */ // TODO: Improve this test
  await page.evaluate(async () => {
    await new Promise((resolve) => {
      let totalHeight = 0
      let distance = 100
      let timer = setInterval(() => {
        let scrollHeight = document.body.scrollHeight
        window.scrollBy(0, distance)
        totalHeight += distance
        if (totalHeight >= scrollHeight) {
          clearInterval(timer)
          resolve()
        }
      }, 100)
    })
  })
}
 
/**
 * @param {string} url
 * @return {string} Return HTML content string
 */
export default async (url, config = {}) => {
  console.log(puppeteer.executablePath())
  const browser = await puppeteer.launch({
    executablePath: puppeteer.executablePath(),
    ...config.launch,
  })
  const page = await browser.newPage()
  await page.goto(url)
  await autoScroll(page)
  const content = await page.content()
  await browser.close()
  return content
}