Merge pull request #16 from zys-contribs/clear

clear up the codebase
This commit is contained in:
Vladimir Carrer 2018-12-06 16:07:43 +01:00 committed by GitHub
commit 6c108c2397
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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();
}
})()