2018-12-02 12:06:38 +01:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
const puppeteer = require('puppeteer')
|
|
|
|
const devices = require('puppeteer/DeviceDescriptors')
|
|
|
|
const program = require('commander')
|
|
|
|
|
|
|
|
var urlvalue, emulate = "";
|
|
|
|
|
|
|
|
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-02 12:06:38 +01:00
|
|
|
.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.fullpage && program.fullpage == "true") fullPage = true
|
|
|
|
if (program.fullpage && program.fullpage == "false") fullPage = false
|
|
|
|
else fullPage = true;
|
|
|
|
|
|
|
|
console.log(urlvalue);
|
|
|
|
console.log(fullPage);
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
const browser = await puppeteer.launch()
|
|
|
|
const page = await browser.newPage()
|
|
|
|
const d = new Date()
|
|
|
|
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]);
|
|
|
|
await page.goto(urlvalue)
|
2018-12-04 13:38:32 +01:00
|
|
|
if (program.waitfor) await page.waitFor(Number(program.waitfor))
|
2018-12-04 16:23:06 +01:00
|
|
|
if (program.el) {
|
|
|
|
const el = await page.$(program.el);
|
|
|
|
await el.screenshot({
|
2018-12-04 16:35:24 +01:00
|
|
|
path: `${await page.title()} ${program.emulate} ${program.el} ${d.getTime()}.png`
|
2018-12-04 16:23:06 +01:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
await page.screenshot({path: await page.title() + " " + program.emulate + " " + d.getTime() + '.png', fullPage: fullPage})
|
|
|
|
}
|
2018-12-02 12:06:38 +01:00
|
|
|
await page.emulateMedia('screen')
|
|
|
|
if (program.pdf) await page.pdf({ path: await page.title() + " " + program.emulate + " " + d.getTime() + '.pdf' })
|
|
|
|
console.log(await page.title())
|
|
|
|
await browser.close()
|
|
|
|
})()
|