Skip to content

Instantly share code, notes, and snippets.

@chrisabrams
Last active April 19, 2018 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisabrams/a2a098d1b2d697f349f4e1ad1be98602 to your computer and use it in GitHub Desktop.
Save chrisabrams/a2a098d1b2d697f349f4e1ad1be98602 to your computer and use it in GitHub Desktop.
[This does not work for some reason] Use puppeteer to get nodes containing text
function matchNodes(text) {
try {
const filter = {
acceptNode: function(node){
if(node.nodeValue.toLowerCase().includes(text)){
return NodeFilter.FILTER_ACCEPT
}
return NodeFilter.FILTER_REJECT
}
}
const nodes = []
const rootElement = document.body
const walker = document.createTreeWalker(rootElement, NodeFilter.SHOW_TEXT, filter, false)
while(walker.nextNode()) {
nodes.push(walker.currentNode.parentNode)
}
console.log(nodes) // This will contain nodes
return nodes
}
catch(e) {
console.error(e)
return e
}
}
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('http://www.greenmatters.com')
const nodes = await page.evaluate(matchNodes, 'news')
console.log('nodes', nodes) // This will be undefined
await browser.close();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment