Skip to content

Instantly share code, notes, and snippets.

@thedaviddias
Forked from tushuhei/sample.js
Created February 19, 2019 18:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thedaviddias/1f8a84e7854b2e72fe3a128329bd5c9f to your computer and use it in GitHub Desktop.
Save thedaviddias/1f8a84e7854b2e72fe3a128329bd5c9f 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