laravel-rabbitmq/.docker/puppeteer/puppeteer.js

40 lines
1.0 KiB
JavaScript

const puppeteer = require('puppeteer');
module.exports = async (
config = { url: '', screenshotPath: '', executablePath: '', timeout: 1000 }
) => {
config = Object.assign({
url: 'https://scrapingbee.com',
screenshotPath: './screenshot.jpg',
executablePath: '/usr/bin/chromium',
timeout: 1000,
}, config)
const browser = await puppeteer.launch({
executablePath: config.executablePath,
args: ['--no-sandbox', '--headless', '--disable-gpu']
});
const page = await browser.newPage();
// Set the viewport's width and height
await page.setViewport({ width: 1920, height: 1080 });
// Open ScrapingBee's home page
await page.goto(config.url);
// Waiting Dom Ready
await new Promise(r => setTimeout(r, config.timeout));
try {
// Capture screenshot and save it in the current folder:
await page.screenshot({ path: config.screenshotPath });
console.log(`Screenshot has been captured successfully`);
} catch (err) {
console.log(`Error: ${err.message}`);
} finally {
await browser.close();
}
};