screenshoteer/index.js

86 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-12-02 12:06:38 +01:00
#!/usr/bin/env node
2018-12-06 15:41:47 +01:00
const puppeteer = require('puppeteer');
const devices = require('puppeteer/DeviceDescriptors');
const program = require('commander');
2018-12-02 12:06:38 +01:00
program
.option('--url, [url]', 'The url')
.option('--emulate, [emulate]', 'emulate device')
.option('--fullpage, [fullpage]', 'Full Page')
.option('--pdf, [pdf]', 'Generate PDF')
.option('--w, [w]', 'width')
.option('--h, [h]', 'height')
2018-12-04 13:38:32 +01:00
.option('--waitfor, [waitfor]', 'Wait time in milliseconds')
2018-12-04 16:23:06 +01:00
.option('--el, [el]', 'element css selector')
2018-12-06 11:32:21 +01:00
.option('--auth, [auth]', 'Basic HTTP authentication')
2018-12-14 12:23:49 +01:00
.option('--no, [no]', 'Exclude')
2018-12-14 16:48:05 +01:00
.option('--click, [click]', 'Click')
.option('--file, [file]', 'Output file')
2018-12-02 12:06:38 +01:00
.parse(process.argv);
2018-12-06 15:41:47 +01:00
if (!program.url) {
console.log('Please add --url parameter.\n' +
2018-12-06 17:45:38 +01:00
'Something like this: $ screenshoteer --url http://www.example.com');
2018-12-06 15:41:47 +01:00
process.exit();
}
2018-12-02 12:06:38 +01:00
2018-12-06 15:41:47 +01:00
!program.fullpage ? program.fullPage = true : program.fullPage = JSON.parse(program.fullpage);
2018-12-02 12:06:38 +01:00
2018-12-06 15:41:47 +01:00
console.log(program.url);
console.log(program.fullPage);
2018-12-02 12:06:38 +01:00
(async () => {
2018-12-05 17:00:18 +01:00
try {
await execute();
} catch(e) {
console.error(e);
process.exit(1);
}
async function execute() {
2018-12-06 15:41:47 +01:00
const browser = await puppeteer.launch();
const page = await browser.newPage();
2018-12-14 12:23:49 +01:00
if (program.no) {
await page.setRequestInterception(true);
page.on('request', request => {
if (request.resourceType() === program.no)
request.abort();
else
request.continue();
});
}
2018-12-06 15:41:47 +01:00
const timestamp = new Date().getTime();
if (program.w || program.h) {
const newWidth = !program.w?600:program.w
const newHeight = !program.h?'0':program.h
2018-12-08 10:09:45 +01:00
if (program.h && !program.fullpage) program.fullPage = false;
await page.setViewport({width: Number(newWidth), height: Number(newHeight)})
}
2018-12-06 15:14:16 +01:00
if (program.emulate)
await page.emulate(devices[program.emulate]);
else
program.emulate = '';
2018-12-14 16:48:05 +01:00
2018-12-06 11:32:21 +01:00
if (program.auth) {
2018-12-06 12:39:27 +01:00
const [username, password] = program.auth.split(';');
2018-12-06 15:41:47 +01:00
await page.authenticate({ username, password });
2018-12-14 12:23:49 +01:00
}
2018-12-06 15:41:47 +01:00
await page.goto(program.url);
const title = (await page.title()).replace(/[/\\?%*:|"<>]/g, '-');
if (program.waitfor) await page.waitFor(Number(program.waitfor));
2018-12-14 16:48:05 +01:00
if (program.click) await page.click(program.click);
const file = program.file ? program.file : `${title} ${program.emulate} ${program.el} ${timestamp}.png`;
2018-12-05 17:00:18 +01:00
if (program.el) {
const el = await page.$(program.el);
await el.screenshot({path: file});
2018-12-05 17:00:18 +01:00
} else {
await page.screenshot({path: file, fullPage: program.fullPage});
2018-12-05 17:00:18 +01:00
}
2018-12-06 15:41:47 +01:00
await page.emulateMedia('screen');
if (program.pdf) await page.pdf({path: `${title} ${program.emulate} ${timestamp}.pdf`});
console.log(title);
await browser.close();
2018-12-04 16:23:06 +01:00
}
2018-12-02 12:06:38 +01:00
})()