Last active
April 19, 2018 17:47
-
-
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
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
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