'use strict'; const puppeteer = require('puppeteer'), { AppInputError } = require('./app_error'), debug = require('debug')('agm:web-util'); /** * Take screenshot of a screen using a headless webdriver * @param {*} ops screenshot options { url: url, type: 'jpeg', 'png' (default), quality: 1-100 (75% default, jpeg only), width: number, height: number, path: path to save the output image } */ async function webShot(ops, timeout = 30000) { const logTime = !!(ops && ops.logTime); if (logTime) console.time('webShot'); const type = ops.type || 'png'; const quality = ops.quality || 75; const width = ops.width || 800; const height = ops.height || 600; if (!ops || !ops.url || !ops.path) AppInputError.throw(); let browser; try { browser = await puppeteer.launch({ headless: 'new', args: ['--incognito'], ignoreHTTPSErrors: true, ignoreDefaultArgs: ['--disable-dev-shm-usage'], defaultViewport: { width: width, height: height }, fullPage: true }); const pages = await browser.pages(); const page = pages.length ? pages[0] : await browser.newPage(); await page.goto(ops.url); // const selector = 'div.gm-style-cc a'; // await page.waitForFunction(selector => !!document.querySelector(selector), { timeout: 10000 }, selector); // Generic wait condition when tiles all finished loading, the page set loaded can add some delay to make sure they all loaded visually perfect await page.waitForFunction('window.loaded == true', { timeout: timeout }); const shotOps = { type: type, clip: { x: 0, y: 0, width: width, height: height }, path: ops.path }; if (type == 'jpeg') shotOps['quality'] = quality; await page.screenshot(shotOps); } catch (err) { debug("input:", ops); throw err; } finally { if (browser) await browser.close(); if (logTime) console.timeEnd('webShot'); } } module.exports = { webShot, }