Last active
April 19, 2018 17:47
-
-
Save chrisabrams/a2a098d1b2d697f349f4e1ad1be98602 to your computer and use it in GitHub Desktop.
Revisions
-
chrisabrams revised this gist
Apr 19, 2018 . No changes.There are no files selected for viewing
-
chrisabrams revised this gist
Apr 19, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -35,7 +35,7 @@ const puppeteer = require('puppeteer'); 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(); })(); -
chrisabrams created this gist
Apr 19, 2018 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,41 @@ 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(); })();