Skip to content

Instantly share code, notes, and snippets.

@t-okushima
Last active November 11, 2018 09:58
Show Gist options
  • Save t-okushima/5b9b132470c03caf7b6424350c786f34 to your computer and use it in GitHub Desktop.
Save t-okushima/5b9b132470c03caf7b6424350c786f34 to your computer and use it in GitHub Desktop.
Google ChromeがHeadlessで使える様なのでnode.jsから動かしてみる ref: http://qiita.com/t-okushima/items/1e9f736abcfc5ca47b6c
npm install chrome-launcher
npm install chrome-remote-interface
'use strict';
const chromeLauncher = require('chrome-launcher');
const CDP = require('chrome-remote-interface');
const targetUrl = 'https://www.google.co.jp';
async function startHeadlessChrome() {
try {
return await chromeLauncher.launch({
startingUrl: 'target:brank',
chromeFlags: ['--headless', '--disable-gpu']
});
} catch (error) {
console.log(error);
}
}
async function main() {
const chrome = await startHeadlessChrome(targetUrl);
const options = {
port: chrome.port
};
CDP(options, async(client) => {
try {
// extract domains
const {Network, Page} = client;
// setup handlers
Network.requestWillBeSent((params) => {
console.log(params.request.url);
});
// enable events then start!
await Promise.all([
Network.enable(),
Page.enable()
]);
await Page.navigate({url: targetUrl});
await Page.loadEventFired();
} catch (error) {
console.log(error);
} finally {
// close the connection to the remote instance.
await client.close();
// headless chrome close
await chrome.kill();
}
}).on('error', (err) => {
// cannot connect to the remote endpoint
console.error(err);
});
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment