add new option --file to specify the name of the generate output image file

This commit is contained in:
Sven Paulus 2018-12-15 12:20:12 +01:00
parent 1d8dcda944
commit b2b8a6a035

View file

@ -16,6 +16,7 @@ program
.option('--auth, [auth]', 'Basic HTTP authentication') .option('--auth, [auth]', 'Basic HTTP authentication')
.option('--no, [no]', 'Exclude') .option('--no, [no]', 'Exclude')
.option('--click, [click]', 'Click') .option('--click, [click]', 'Click')
.option('--file, [file]', 'Output file')
.parse(process.argv); .parse(process.argv);
if (!program.url) { if (!program.url) {
@ -69,11 +70,12 @@ console.log(program.fullPage);
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.click) await page.click(program.click); if (program.click) await page.click(program.click);
const file = program.file ? program.file : `${title} ${program.emulate} ${program.el} ${timestamp}.png`;
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: file});
} else { } else {
await page.screenshot({path: `${title} ${program.emulate} ${timestamp}.png`, fullPage: program.fullPage}); await page.screenshot({path: file, 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`});