mirror of
https://github.com/vladocar/screenshoteer.git
synced 2024-11-19 02:10:42 +01:00
53 lines
1.9 KiB
JavaScript
Executable file
53 lines
1.9 KiB
JavaScript
Executable file
#!/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')
|
|
.option('--waitfor, [waitfor]', 'Wait time in milliseconds')
|
|
.option('--el, [el]', 'element css selector')
|
|
.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)
|
|
const title = await page.title()
|
|
const t = 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: `${t} ${program.emulate} ${program.el} ${d.getTime()}.png`
|
|
});
|
|
} else {
|
|
await page.screenshot({path: t + " " + program.emulate + " " + d.getTime() + '.png', fullPage: fullPage})
|
|
}
|
|
await page.emulateMedia('screen')
|
|
if (program.pdf) await page.pdf({ path: t + " " + program.emulate + " " + d.getTime() + '.pdf' })
|
|
console.log(t)
|
|
await browser.close()
|
|
})()
|