|
| 1 | +import puppeteer from "puppeteer"; |
| 2 | + |
| 3 | +function parseArgs() { |
| 4 | + const args = process.argv.slice(2); |
| 5 | + const options = { |
| 6 | + type: "png", |
| 7 | + path: null, |
| 8 | + width: "800", |
| 9 | + height: "600", |
| 10 | + scaleFactor: "1", |
| 11 | + x: null, |
| 12 | + y: null, |
| 13 | + clipWidth: null, |
| 14 | + clipHeight: null, |
| 15 | + }; |
| 16 | + |
| 17 | + for (let i = 0; i < args.length; i++) { |
| 18 | + let arg = args[i]; |
| 19 | + let value = null; |
| 20 | + |
| 21 | + [arg, value] = arg.split("="); |
| 22 | + |
| 23 | + switch (arg) { |
| 24 | + case "-t": |
| 25 | + case "--type": |
| 26 | + options.type = value; |
| 27 | + break; |
| 28 | + case "-p": |
| 29 | + case "--path": |
| 30 | + options.path = value; |
| 31 | + break; |
| 32 | + case "-w": |
| 33 | + case "--width": |
| 34 | + options.width = value; |
| 35 | + break; |
| 36 | + case "-h": |
| 37 | + case "--height": |
| 38 | + options.height = value; |
| 39 | + break; |
| 40 | + case "-s": |
| 41 | + case "--scale-factor": |
| 42 | + options.scaleFactor = value; |
| 43 | + break; |
| 44 | + case "-x": |
| 45 | + case "--x": |
| 46 | + options.x = value; |
| 47 | + break; |
| 48 | + case "-y": |
| 49 | + case "--y": |
| 50 | + options.y = value; |
| 51 | + break; |
| 52 | + case "--clip-width": |
| 53 | + options.clipWidth = value; |
| 54 | + break; |
| 55 | + case "--clip-height": |
| 56 | + options.clipHeight = value; |
| 57 | + break; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // URL is the first non-option argument |
| 62 | + options.url = args.find((arg) => !arg.startsWith("-")); |
| 63 | + return options; |
| 64 | +} |
| 65 | + |
| 66 | +function _validateInteger(value) { |
| 67 | + const parsed = parseInt(value); |
| 68 | + if (value && !parsed) { |
| 69 | + console.error("Number values must be valid integer"); |
| 70 | + return null; |
| 71 | + } |
| 72 | + return parsed; |
| 73 | +} |
| 74 | + |
| 75 | +(async () => { |
| 76 | + const options = parseArgs(); |
| 77 | + let screenshotOptions = {}; |
| 78 | + let viewportOptions = {}; |
| 79 | + |
| 80 | + if (!options.url) { |
| 81 | + console.error("URL required"); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + viewportOptions.width = _validateInteger(options.width) || 800; |
| 86 | + viewportOptions.height = _validateInteger(options.height) || 600; |
| 87 | + viewportOptions.deviceScaleFactor = |
| 88 | + _validateInteger(options.scaleFactor) || 1; |
| 89 | + screenshotOptions.type = ["jpeg", "png"].includes(options.type) |
| 90 | + ? options.type |
| 91 | + : "png"; |
| 92 | + screenshotOptions.path = options.path || `./image.${screenshotOptions.type}`; |
| 93 | + |
| 94 | + const clipParams = { |
| 95 | + x: options.x, |
| 96 | + y: options.y, |
| 97 | + width: options.clipWidth, |
| 98 | + height: options.clipHeight, |
| 99 | + }; |
| 100 | + const hasClipParams = Object.values(clipParams).every((val) => val !== null); |
| 101 | + |
| 102 | + if (hasClipParams) { |
| 103 | + screenshotOptions.clip = {}; |
| 104 | + for (const [key, value] of Object.entries(clipParams)) { |
| 105 | + screenshotOptions.clip[key] = _validateInteger(value); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + puppeteer |
| 110 | + .launch({ |
| 111 | + devtools: false, |
| 112 | + args: ["--no-sandbox", "--disable-setuid-sandbox", "--single-process"], |
| 113 | + ignoreHTTPSErrors: true, |
| 114 | + }) |
| 115 | + .then(async function (browser) { |
| 116 | + const page = await browser.newPage(); |
| 117 | + await page.setViewport(viewportOptions); |
| 118 | + await page.goto(options.url, { waitUntil: ["networkidle2"] }); |
| 119 | + await page.focus("body"); |
| 120 | + await page.screenshot(screenshotOptions); |
| 121 | + |
| 122 | + await page.close(); |
| 123 | + await browser.close(); |
| 124 | + process.stdout.write(screenshotOptions.path); |
| 125 | + }); |
| 126 | +})(); |
0 commit comments