Skip to content

Instantly share code, notes, and snippets.

@nnasaki
Last active May 3, 2017 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nnasaki/775caccca35c3097c6deef898257ac95 to your computer and use it in GitHub Desktop.
Save nnasaki/775caccca35c3097c6deef898257ac95 to your computer and use it in GitHub Desktop.
Chrome Launch And Capture Sample
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