These are chat archives for rosshinkley/nightmare

6th
May 2016
Mingsterism
@mingsterism
May 06 2016 08:05
Hi. I'm having this buggy problem with my code. Its supposed to go to Reddit, grab the post titles, and paginate next.
I've tried all the DOM in console, and it works. But some how I'm getting error on my console.
Cannot read property 'innerText' of undefined
my code below
function getLinks() {
    var propertyNames = Array.prototype.slice.call(document.getElementsByClassName('title may-blank'));
    var next = function() {
        var elem = document.getElementsByClassName('nextprev')[1].getElementsByTagName('a');
        if(elem[0].innerText.slice(2,6) === 'next') {
            return elem[0];
        } else if (elem[1].innerText.slice(0,4) === 'next') {
            return elem[1];
        } else {
            return;
            }
        }
    return {
        t: propertyNames.map(function (title) { return title.innerText;}),
        nxtWord: next().innerText,
        nxtLink: next().href
    }
};


function start(link) {
    var Nightmare = require('nightmare');
    var nightmare = Nightmare({ show: true })
    var a = true;

    nightmare
        .goto(link)
        .evaluate(getLinks)
        .end()
        .then(function(content) {
            if(a) {
                console.log(content.t);
                // console.log(content.nxtWord)
                console.log(content.href);
            } else {
                console.log('no a for you today');
            } 
            if(content.nxt) {
                console.log('-------------------------- ' + content.nxt + ' -----------------------')
                start(content.nxtLink)
            } 
        })
        .catch(function(err) {
            console.log(err);
        })
}

start('https://www.reddit.com/?count=26&before=t3_4i0jrx')
appreciate some help. thanks very much.
Michael Groncki
@dergroncki
May 06 2016 17:17
This message was deleted
This message was deleted
@mingsterism I changed your code a bit and it seems to be working now.
function getLinks(link) {
    var propertyNames = Array.prototype.slice.call(document.getElementsByClassName('title may-blank'));         

    if (link === 'https://www.reddit.com/')
        var elem = document.querySelector("#siteTable > div.nav-buttons > span.nextprev > a").href;
    else     
        var elem = document.querySelector("#siteTable > div.nav-buttons > span.nextprev > a:nth-child(3)").href;

    return {
        t: propertyNames.map(function (title) { return title.innerText;}),
        nxtLink: elem
    }
};

function start(link) {
    var Nightmare = require('nightmare');
    var nightmare = Nightmare({ show: true })
    var a = true;

    nightmare
        .goto(link)
        .evaluate(getLinks, link)
        .end()
        .then(function(content) {
            //console.log(content.nxtLink);
            console.log(content.t);
            if (content.nxtLink != undefined) start(content.nxtLink);
        })
        .catch(function(err) {
            console.log(err);
        })
}

start('https://www.reddit.com/')
This message was deleted
Mingsterism
@mingsterism
May 06 2016 23:10
@DerGroncki thanks very much. Will try it. But what was wrong with my earlier code?
also is this right way to write nightmare code? Encapsulate it in function. Any way to improve it? Any advise appreciated
@rosshinkley
Ross Hinkley
@rosshinkley
May 06 2016 23:23
@mingsterism haven't had a chance to dig into your original source
as for "right"... that depends
I would probably have your wrapping function return a promise
but... depends on what your application is