1const Apify = require('apify');
2
3Apify.main(async () => {
4 const input = await Apify.getValue('INPUT');
5
6 if (!input || !input.url) throw new Error('Invalid input, must be a JSON object with the "url" field!');
7
8 console.log('Launching Puppeteer...');
9 const browser = await Apify.launchPuppeteer();
10
11 console.log(`Opening URL: ${input.url}`);
12 const page = await browser.newPage();
13 await page.goto(input.url);
14
15
16 console.log('Determining page dimensions...');
17 const dimensions = await page.evaluate(() => ({
18 width: document.documentElement.clientWidth,
19 height: document.documentElement.clientHeight,
20 deviceScaleFactor: window.devicePixelRatio
21 }));
22 console.log(`Dimension: ${JSON.stringify(dimensions)}`);
23
24
25 console.log('Saving screenshot...');
26 const screenshotBuffer = await page.screenshot();
27 await Apify.setValue('screenshot.png', screenshotBuffer, { contentType: 'image/png' });
28
29 console.log('Closing Puppeteer...');
30 await browser.close();
31
32 console.log('Done.');
33 console.log('You can check the output in the key-value on the following URLs:');
34 const storeId = process.env.APIFY_DEFAULT_KEY_VALUE_STORE_ID;
35 console.log(`- https://api.apify.com/v2/key-value-stores/${storeId}/records/screenshot.png`);
36});