Skip to content

Instantly share code, notes, and snippets.

@chrisabrams
Last active April 19, 2018 17:47
Show Gist options
  • Save chrisabrams/a2a098d1b2d697f349f4e1ad1be98602 to your computer and use it in GitHub Desktop.
Save chrisabrams/a2a098d1b2d697f349f4e1ad1be98602 to your computer and use it in GitHub Desktop.

Revisions

  1. chrisabrams revised this gist Apr 19, 2018. No changes.
  2. chrisabrams revised this gist Apr 19, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion get-text.js
    Original 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)
    const nodes = await page.evaluate(matchNodes, 'news')
    console.log('nodes', nodes) // This will be undefined
    await browser.close();
    })();
  3. chrisabrams created this gist Apr 19, 2018.
    41 changes: 41 additions & 0 deletions get-text.js
    Original 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();
    })();