|
| 1 | +const express = require('express'); |
| 2 | +const bodyParser = require('body-parser'); |
| 3 | +const fileUpload = require('express-fileupload'); |
| 4 | +const fs = require('fs'); |
| 5 | +const tempy = require('tempy'); |
| 6 | +const CDP = require('chrome-remote-interface'); |
| 7 | + |
| 8 | +function getScreenshot({ |
| 9 | + url, |
| 10 | + format = 'png', |
| 11 | + width = 1920, |
| 12 | + height = 1080, |
| 13 | + delay = 300, |
| 14 | + userAgent = null, |
| 15 | + full = false |
| 16 | +}) { |
| 17 | + return new Promise((resolve, reject) => { |
| 18 | + |
| 19 | + // Start the Chrome Debugging Protocol |
| 20 | + CDP(function(client) { |
| 21 | + // Extract used DevTools domains. |
| 22 | + const {DOM, Emulation, Network, Page, Runtime} = client; |
| 23 | + |
| 24 | + // Set up viewport resolution, etc. |
| 25 | + const deviceMetrics = { |
| 26 | + width, |
| 27 | + height, |
| 28 | + deviceScaleFactor: 0, |
| 29 | + mobile: false, |
| 30 | + fitWindow: false, |
| 31 | + }; |
| 32 | + |
| 33 | + // Enable events on domains we are interested in. |
| 34 | + Promise.all([ |
| 35 | + Page.enable(), |
| 36 | + DOM.enable(), |
| 37 | + Network.enable(), |
| 38 | + ]).then(() => { |
| 39 | + Emulation.setDeviceMetricsOverride(deviceMetrics).then(() => { |
| 40 | + Emulation.setVisibleSize({width, height}).then(() => { |
| 41 | + // Navigate to target page |
| 42 | + Page.navigate({url}).then(() => { |
| 43 | + }); |
| 44 | + }); |
| 45 | + }).catch((e) => reject(e)); |
| 46 | + }).catch((e) => reject(e)); |
| 47 | + |
| 48 | + |
| 49 | + // Wait for page load event to take screenshot |
| 50 | + Page.loadEventFired(() => { |
| 51 | + setTimeout(() => { |
| 52 | + Page.captureScreenshot({format}).then((screenshot) => { |
| 53 | + const buffer = new Buffer(screenshot.data, 'base64'); |
| 54 | + client.close(); |
| 55 | + resolve(buffer); |
| 56 | + }).catch((e) => reject(e)); |
| 57 | + }, delay); |
| 58 | + }); |
| 59 | + }).on('error', err => { |
| 60 | + reject(err); |
| 61 | + }); |
| 62 | + |
| 63 | + }); |
| 64 | +} |
| 65 | + |
| 66 | +const app = express(); |
| 67 | + |
| 68 | +app.use(bodyParser.urlencoded({extended: true})); |
| 69 | +app.use(bodyParser.json()); |
| 70 | +app.use(fileUpload()); |
| 71 | + |
| 72 | +app.get('/', (req, res) => { |
| 73 | + res.type('text/plain').send(`POST to this URI in this format: |
| 74 | +{html: "<html></html>", width: 100, height: 100}`); |
| 75 | +}); |
| 76 | + |
| 77 | +app.post('/', (req, res) => { |
| 78 | + const file = req.files.htmlFile; |
| 79 | + const width = req.body.width ? parseInt(req.body.width, 10) : undefined; |
| 80 | + const height = req.body.height ? parseInt(req.body.height, 10) : undefined; |
| 81 | + const delay = req.body.delay ? parseInt(req.body.delay, 10) : undefined; |
| 82 | + |
| 83 | + if (!file) { |
| 84 | + return res.status(422).send('No htmlFile sent.'); |
| 85 | + } |
| 86 | + |
| 87 | + const tmp = tempy.file({extension: 'html'}); |
| 88 | + |
| 89 | + file.mv(tmp, (err) => { |
| 90 | + if (err) { |
| 91 | + res.status(500).send('There was an error.'); |
| 92 | + throw err; |
| 93 | + } |
| 94 | + |
| 95 | + getScreenshot({ |
| 96 | + width, |
| 97 | + height, |
| 98 | + delay, |
| 99 | + url: 'file://' + tmp |
| 100 | + }).then((data) => { |
| 101 | + res.status(200).type('image/png').send(data); |
| 102 | + }).catch((e) => { |
| 103 | + console.log(e); |
| 104 | + res.status(500).send('some kind of failure'); |
| 105 | + }); |
| 106 | + }) |
| 107 | +}); |
| 108 | + |
| 109 | +app.listen(process.env.NODE_PORT || 8888); |
0 commit comments