diff --git a/index.js b/index.js index 90e2ea3..d822bbb 100755 --- a/index.js +++ b/index.js @@ -1,10 +1,8 @@ #!/usr/bin/env node -const puppeteer = require('puppeteer') -const devices = require('puppeteer/DeviceDescriptors') -const program = require('commander') - -let urlvalue; +const puppeteer = require('puppeteer'); +const devices = require('puppeteer/DeviceDescriptors'); +const program = require('commander'); program .option('--url, [url]', 'The url') @@ -18,13 +16,16 @@ program .option('--auth, [auth]', 'Basic HTTP authentication') .parse(process.argv); -if (program.url) urlvalue = program.url -else process.exit(console.log("Please add --url parameter. Something like this: $ screenshoteer --url http:www.example.com")); +if (!program.url) { + console.log('Please add --url parameter.\n' + + 'Something like this: $ screenshoteer --url http:www.example.com'); + process.exit(); +} -!program.fullpage ? fullPage = true : fullPage = JSON.parse(program.fullpage); +!program.fullpage ? program.fullPage = true : program.fullPage = JSON.parse(program.fullpage); -console.log(urlvalue); -console.log(fullPage); +console.log(program.url); +console.log(program.fullPage); (async () => { @@ -36,31 +37,31 @@ console.log(fullPage); } async function execute() { - const browser = await puppeteer.launch() - const page = await browser.newPage() - const timestamp = new Date().getTime() - if (program.w && program.h) await page.setViewport({width: Number(program.w), height: Number(program.h)}) + const browser = await puppeteer.launch(); + const page = await browser.newPage(); + const timestamp = new Date().getTime(); + if (program.w && program.h) await page.setViewport({width: Number(program.w), height: Number(program.h)}); if (program.emulate) await page.emulate(devices[program.emulate]); else program.emulate = ''; if (program.auth) { const [username, password] = program.auth.split(';'); - await page.authenticate({username:username, password:password}); + await page.authenticate({ username, password }); } - await page.goto(urlvalue) - const title = (await page.title()).replace(/[/\\?%*:|"<>]/g, '-') - if (program.waitfor) await page.waitFor(Number(program.waitfor)) + await page.goto(program.url); + const title = (await page.title()).replace(/[/\\?%*:|"<>]/g, '-'); + if (program.waitfor) await page.waitFor(Number(program.waitfor)); if (program.el) { const el = await page.$(program.el); await el.screenshot({path: `${title} ${program.emulate} ${program.el} ${timestamp}.png`}); } else { - await page.screenshot({path: `${title} ${program.emulate} ${timestamp}.png`, fullPage: fullPage}) + await page.screenshot({path: `${title} ${program.emulate} ${timestamp}.png`, fullPage: program.fullPage}); } - await page.emulateMedia('screen') - if (program.pdf) await page.pdf({path: `${title} ${program.emulate} ${timestamp}.pdf`}) - console.log(title) - await browser.close() + await page.emulateMedia('screen'); + if (program.pdf) await page.pdf({path: `${title} ${program.emulate} ${timestamp}.pdf`}); + console.log(title); + await browser.close(); } })()