clear up the codebase

This commit is contained in:
ZYSzys 2018-12-06 22:41:47 +08:00
parent ba97ec3105
commit b6b664d993

View file

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