├── .gitignore ├── package.json ├── LICENSE └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "chrome-remote-interface": "^0.12.3" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2016 Google Inc. All Rights Reserved. 2 | Licensed under the Apache License, Version 2.0 (the "License"); 3 | you may not use this file except in compliance with the License. 4 | You may obtain a copy of the License at 5 | http://www.apache.org/licenses/LICENSE-2.0 6 | Unless required by applicable law or agreed to in writing, software 7 | distributed under the License is distributed on an "AS IS" BASIS, 8 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 9 | See the License for the specific language governing permissions and 10 | limitations under the License. 11 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2016 Google Inc. All Rights Reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | */ 13 | 14 | var fs = require('fs'); 15 | var Chrome = require('chrome-remote-interface'); 16 | var spawn = require('child_process').spawn; 17 | 18 | if (process.argv[2] === undefined) { 19 | throw Error('No headless binary path provided.'); 20 | } 21 | 22 | var headless = spawn(process.argv[2], [ 23 | '--remote-debugging-port=9222']); 24 | 25 | var screenshotUrl = process.argv[3] || 'https://paulirish.com'; 26 | 27 | // Go ahead and log out a few things 28 | headless.stdout.on('data', d => console.log(d.toString())); 29 | headless.stderr.on('data', d => console.log(d.toString())); 30 | 31 | // Dumb timeout for now 32 | setTimeout(connect, 2000); 33 | 34 | function getChromeInstance() { 35 | return new Promise((res, rej) => { 36 | Chrome(function (chromeInstance) { 37 | res(chromeInstance); 38 | }).on('error', rej); 39 | }); 40 | } 41 | 42 | function takeScreenshot(instance) { 43 | instance.Page.captureScreenshot().then((v) => { 44 | let filename = `screenshot-${Date.now()}.png`; 45 | 46 | fs.writeFileSync(filename, v.data, 'base64'); 47 | console.log(`Image saved as ${filename}`); 48 | 49 | // Take it all down now. 50 | headless.kill(); 51 | process.exit(0); 52 | }); 53 | } 54 | 55 | function connect() { 56 | getChromeInstance().then(instance => { 57 | instance.Page.loadEventFired(takeScreenshot.bind(null, instance)); 58 | instance.Page.enable(); 59 | instance.once('ready', () => { 60 | instance.Page.navigate({url: screenshotUrl}) 61 | }); 62 | }); 63 | } 64 | --------------------------------------------------------------------------------