Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tushuhei
Created August 29, 2017 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tushuhei/3c249e7334ccac7fd0120da1a8592fa1 to your computer and use it in GitHub Desktop.
Save tushuhei/3c249e7334ccac7fd0120da1a8592fa1 to your computer and use it in GitHub Desktop.
Getting Website Title with Headless Chrome
const CDP = require('chrome-remote-interface');
const chromeLauncher = require('lighthouse/chrome-launcher/chrome-launcher');
function onPageLoad(Runtime) {
const js = "document.querySelector('title').textContent";
return Runtime.evaluate({expression: js}).then(result => {
console.log('Title of page: ' + result.result.value);
});
}
async function launchChrome() {
const chrome = await chromeLauncher.launch({
chromeFlags: ['--disable-gpu', '--headless', '--enable-logging']
});
return chrome;
}
launchChrome(true).then(async chrome => {
CDP({port: chrome.port}, protocol => {
const {Page, Runtime} = protocol;
Promise.all([
Page.enable(),
Runtime.enable()
]).then(() => {
Page.navigate({url: 'https://www.chromestatus.com/'});
Page.loadEventFired(() => {
onPageLoad(Runtime).then(() => {
protocol.close();
chrome.kill();
});
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment