Last active
May 3, 2017 23:37
-
-
Save nnasaki/775caccca35c3097c6deef898257ac95 to your computer and use it in GitHub Desktop.
Chrome Launch And Capture Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const CDP = require('chrome-remote-interface'); | |
const fs = require('fs'); | |
const { ChromeLauncher } = require('lighthouse/lighthouse-cli/chrome-launcher'); | |
/** | |
* Launches a debugging instance of Chrome on port 9222. | |
* @param {boolean=} headless True (default) to launch Chrome in headless mode. | |
* Set to false to launch Chrome normally. | |
* @return {Promise<ChromeLauncher>} | |
*/ | |
function launchChrome() { | |
const launcher = new ChromeLauncher({ | |
port: 9222, | |
autoSelectChrome: true, // False to manually select which Chrome install. | |
additionalFlags: ['--disable-gpu'] | |
}); | |
return launcher.run().then() | |
.catch(err => { | |
return launcher.kill().then(() => { // Kill Chrome if there's an error. | |
throw err; | |
}, console.error); | |
}); | |
} | |
launchChrome().then(() => { | |
CDP(async (client) => { | |
const { Page } = client; | |
try { | |
await Page.enable(); | |
await Page.navigate({ url: 'https://www.chromestatus.com/features/' }); | |
await Page.loadEventFired(); | |
const { data } = await Page.captureScreenshot(); | |
console.log('Image saved'); | |
fs.writeFileSync('scrot.png', Buffer.from(data, 'base64')); | |
} catch (err) { | |
console.error(err); | |
} | |
}).on('error', (err) => { | |
console.error(err); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment