-
-
Save thedaviddias/1f8a84e7854b2e72fe3a128329bd5c9f to your computer and use it in GitHub Desktop.
Getting Website Title with Headless Chrome
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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