Skip to content

Commit

Permalink
Merge git://github.com/cowchimp/chrome-headless-screenshots into full…
Browse files Browse the repository at this point in the history
…-async

Conflicts:
	index.js
  • Loading branch information
schnerd committed Jun 13, 2017
2 parents c505326 + 211511f commit 9f4f7e2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 45 deletions.
88 changes: 45 additions & 43 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const CDP = require('chrome-remote-interface');
const argv = require('minimist')(process.argv.slice(2));
const file = require('fs');
const file = require('mz/fs');
const timeout = require('delay');

// CLI Args
const url = argv.url || 'https://www.google.com';
Expand All @@ -12,37 +13,44 @@ const userAgent = argv.userAgent;
const fullPage = argv.full;
const output = argv.output || `output.${format === 'png' ? 'png' : 'jpg'}`;

// Start the Chrome Debugging Protocol
CDP(async function(client) {
// Extract used DevTools domains.
const {DOM, Emulation, Network, Page, Runtime} = client;
init();

// Enable events on domains we are interested in.
await Page.enable();
await DOM.enable();
await Network.enable();
async function init() {
try {
// Start the Chrome Debugging Protocol
const client = await CDP();
// Extract used DevTools domains.
const {DOM, Emulation, Network, Page, Runtime} = client;

// If user agent override was specified, pass to Network domain
if (userAgent) {
await Network.setUserAgentOverride({userAgent});
}
// Enable events on domains we are interested in.
await Page.enable();
await DOM.enable();
await Network.enable();

// If user agent override was specified, pass to Network domain
if (userAgent) {
await Network.setUserAgentOverride({userAgent});
}

// Set up viewport resolution, etc.
const deviceMetrics = {
width: viewportWidth,
height: viewportHeight,
deviceScaleFactor: 0,
mobile: false,
fitWindow: false,
};
await Emulation.setDeviceMetricsOverride(deviceMetrics);
await Emulation.setVisibleSize({width: viewportWidth, height: viewportHeight});
// Set up viewport resolution, etc.
const deviceMetrics = {
width: viewportWidth,
height: viewportHeight,
deviceScaleFactor: 0,
mobile: false,
fitWindow: false,
};
await Emulation.setDeviceMetricsOverride(deviceMetrics);
await Emulation.setVisibleSize({
width: viewportWidth,
height: viewportHeight,
});

// Navigate to target page
await Page.navigate({url});
// Navigate to target page
await Page.navigate({url});

// Wait for page load event to take screenshot
Page.loadEventFired(async () => {
// Wait for page load event to take screenshot
await Page.loadEventFired();
// If the `full` CLI option was passed, we need to measure the height of
// the rendered page and use Emulation.setVisibleSize
if (fullPage) {
Expand All @@ -59,19 +67,13 @@ CDP(async function(client) {
await Emulation.forceViewport({x: 0, y: 0, scale: 1});
}

setTimeout(async function() {
const screenshot = await Page.captureScreenshot({format});
const buffer = new Buffer(screenshot.data, 'base64');
file.writeFile(output, buffer, 'base64', function(err) {
if (err) {
console.error(err);
} else {
console.log('Screenshot saved');
}
client.close();
});
}, delay);
});
}).on('error', err => {
console.error('Cannot connect to browser:', err);
});
await timeout(delay);
const screenshot = await Page.captureScreenshot({format});
const buffer = new Buffer(screenshot.data, 'base64');
await file.writeFile(output, buffer, 'base64');
console.log('Screenshot saved');
client.close();
} catch (err) {
console.error('Exception while taking screenshot:', err);
}
}
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"author": "",
"license": "ISC",
"dependencies": {
"chrome-remote-interface": "^0.19.0",
"minimist": "^1.2.0"
"chrome-remote-interface": "^0.23.0",
"delay": "^2.0.0",
"minimist": "^1.2.0",
"mz": "^2.6.0"
}
}

0 comments on commit 9f4f7e2

Please sign in to comment.