These are chat archives for rosshinkley/nightmare

4th
May 2016
Ross Hinkley
@rosshinkley
May 04 2016 00:05
@thepiercingarrow2 You need at least .goto().click().then(), yes
.goto().click() queues events
it's useful for conditional branching, say
but to actually execute the queue, you'll need to make a call to .then().
if you have a more specific example you need help with, i'd be happy to help debug :)
also, hopefully what i've said makes sense :|
Maxwell Zhao
@thepiercingarrow
May 04 2016 00:42
Makes a ton of sense, thanks! :D
Ross Hinkley
@rosshinkley
May 04 2016 00:42
no problem :)
Maxwell Zhao
@thepiercingarrow
May 04 2016 00:43
Normally, on a project, which file do you list the people who helped you?
Ross Hinkley
@rosshinkley
May 04 2016 00:44
that's a great question, and i don't know there is a standard answer
you could include a credits.md or something
or put it right in your readme, i suppose
Maxwell Zhao
@thepiercingarrow
May 04 2016 00:44
Great, I'll put it in my README.md :D
Ross Hinkley
@rosshinkley
May 04 2016 00:44
i've also seen props given in version histories
Maxwell Zhao
@thepiercingarrow
May 04 2016 00:45
Oh, I see, like in the CHANGELOG?
Ross Hinkley
@rosshinkley
May 04 2016 00:45
yeah
Maxwell Zhao
@thepiercingarrow
May 04 2016 00:46
Is there anything in the nightmare documentation about the usage of .then()?
Ross Hinkley
@rosshinkley
May 04 2016 00:47
y'know, i know @Mr0grog and I talked about it a lot... i could dig out some of the issue numbers if you'd like
but i don't think anyone actually got around to documenting it, short of saying "it kind of works like Promise.then()"
Maxwell Zhao
@thepiercingarrow
May 04 2016 00:48
Oh.. lol :smile:
Ross Hinkley
@rosshinkley
May 04 2016 00:49
the Promise example in nightmare-examples might be of some help
but yeah, it's not actually described in the api documentation, i don't think
Maxwell Zhao
@thepiercingarrow
May 04 2016 00:50

I see. So...

.then(function(result) {
    console.log(result);
})

Will do all the methods above it, then print the output of .end() to the console?

Ross Hinkley
@rosshinkley
May 04 2016 00:52
for completeness... you may also want to read through segmentio/nightmare#491 and maybe segmentio/nightmare#527
.end() is an edge case
.end() will actually bubble the previous function's output, i think
so if you do like...
nightmare.goto(someUrl)
  .click(someSelector)
  .title()
  .end()
  .then(function(title){
    console.log('title: ' + title);
  })
i think that should work, shooting from the hip
however.... if you do something like....
nightmare.goto(someUrl)
  .click(someSelector)
  .title()
  .wait(someMs)
  .then(function(title){
    console.log('title: ' + title);
  })
title will be undefined
Maxwell Zhao
@thepiercingarrow
May 04 2016 00:56
Oh, thats neat :smile:
Ross Hinkley
@rosshinkley
May 04 2016 00:57
(and, obviously, your nightmare instance won't end)
Maxwell Zhao
@thepiercingarrow
May 04 2016 01:00

In #491, you said:

I guess getting back to a point @Mr0grog made: I am biased for using yield.

What would yield be the alternative for?

Ross Hinkley
@rosshinkley
May 04 2016 01:00
ah, something i said! :P
yeah
yield in conjunction with vo or co can be pretty useful
and it's especially useful if you want to do multiple steps or grab multiple pieces of information
an example would be useful
This message was deleted
err, hang on
let me edit that
<== wasn't in compose mode
Maxwell Zhao
@thepiercingarrow
May 04 2016 01:02
No hurry :smile:
Ross Hinkley
@rosshinkley
May 04 2016 01:05
shooting totally from the hip
var vo = require('vo');
var Nightmare = require('nightmare');

var run = function*() {
  var nightmare = Nightmare();
  var title = yield nightmare
    .goto(someUrl)
    .title();
  var somethingVisible = yield nightmare.visible('.my-super-awesome-class');
  yield nightmare.end();
  return {title: title, visible: somethingVisible};
}l;
vo(run)(function(err, result){
  //result is the object returned from "run"
});
hopefully, that demonstrates that using yield makes it pretty straightforward to pluck data as you need it, but there's certainly nothing stopping you from using promises to accomplish the same thing
Maxwell Zhao
@thepiercingarrow
May 04 2016 01:08
I see.. so yield will save information that you can work with later?
Ross Hinkley
@rosshinkley
May 04 2016 01:09
generators (the function*() bit) are ... more complicated than that
but! to simplify things and probably get myself in trouble
think of yield like a placeholder for asynchronous functions to put their value when they're finished
so that first yield nightmare.title() line is saying "hey, eventually there will be the title here, wait for it"
there are a ton of good writeups out there on this very topic
that do it way, way, way more justice than i did
or can
rosshinkley @rosshinkley rifles through browsing history
Maxwell Zhao
@thepiercingarrow
May 04 2016 01:15
Sorry, I was disconnected for a minuete :(
Ross Hinkley
@rosshinkley
May 04 2016 01:15
no problem
i did a protracted explanation about this in... one of the issues
(although it was probably tough to follow)
and re-skimming it quickly, now i'm not sure is germane to this conversation
but
here it is
this article is an okay primer, too
Maxwell Zhao
@thepiercingarrow
May 04 2016 01:19
Do you have a dogecoin wallet?
Ross Hinkley
@rosshinkley
May 04 2016 01:20
i do not
Maxwell Zhao
@thepiercingarrow
May 04 2016 01:22
Okay then.. please tell me if you ever get one :)
I think I sorta understand now... (enough to start writing code :P). Thanks so much! :)
Ross Hinkley
@rosshinkley
May 04 2016 01:26
no problem :)
lmk if you get stuck
Maxwell Zhao
@thepiercingarrow
May 04 2016 03:19
I call .wait(5000) in my program, but my program takes less than a second to run. Is this supposed to happen?
Ross Hinkley
@rosshinkley
May 04 2016 03:26
your actions.post() doesn't have a .then()
you might also want to return the promise from the action
also, you have botland.botland-thread... dashes aren't legal in bare identifiers as it'll try to do math
try botland['botland-thread']
Ross Hinkley
@rosshinkley
May 04 2016 03:31
also, biased note, it's generally frowned upon to autopost on forums, and is probably against the terms of use
Maxwell Zhao
@thepiercingarrow
May 04 2016 03:43
Its probably fine, I will only be using it on the forum that I'm an admin of.
How should I refer to you when describing you in my README? I want to say "_ of nightmare" or something like that.
Ross Hinkley
@rosshinkley
May 04 2016 03:45
uh... contributor?
i guess?
Maxwell Zhao
@thepiercingarrow
May 04 2016 03:45
Oops I forgot
I will push my most recent commit - sorry
forgot
Ross Hinkley
@rosshinkley
May 04 2016 03:45
if you're bent on crediting, anyhow
juuuust trying to help
Maxwell Zhao
@thepiercingarrow
May 04 2016 03:48
oops I'm really really sorry. I just pushed.
I'll just call you "Ross Hinkley" ;)
Maxwell Zhao
@thepiercingarrow
May 04 2016 03:54
When I turned on debugging, it listed all my actions, saying they were "queued". Then it said "run". The function was called with the correct parameters, and I'm fairly confident my selectors are correct (I used PaleMoon's web browser select element to generate them).
Could it have something to do with the fact that my program stopped after half a second even though I called .wait() ?
Mingsterism
@mingsterism
May 04 2016 06:08

@rosshinkley Can someone help me with this code. It returns empty objects.

var Nightmare = require('nightmare');
var startingLink = "http://stackoverflow.com"

var nightmare = Nightmare({show:true});

nightmare 
    .goto(startingLink)
    .evaluate(function() {
        return document.getElementsByClassName('question-hyperlink')
    })
    .end()
    .then(function(content) {
        console.log(content);
    })

I don't know what's wrong with it.

Ross Hinkley
@rosshinkley
May 04 2016 14:09
@thepiercingarrow2 hm, not 100% sure, what happens if you watch it with visible:true?
@mingsterism i saw your submitted issue, moving over to that in a minute
Mingsterism
@mingsterism
May 04 2016 15:27
Hi @rosshinkley thanks for help.
What is you mean by IPC
Boundary
Ross Hinkley
@rosshinkley
May 04 2016 15:27
np
ipc = interprocess communication
Mingsterism
@mingsterism
May 04 2016 15:28
What's that exactly? Sry I'm still new.
Ross Hinkley
@rosshinkley
May 04 2016 15:28
that's no problem!
to keep it simple, it's a connection between Nightmare and Electron so they can communicate
and to send data back and forth, it needs to be serialized (more or less)
i haven't dug into the specifics, but i suspect HTMLCollection and DOMElement don't serialize nicely
Mingsterism
@mingsterism
May 04 2016 15:31
I see. Ok. Understood. Thanks.
Ross Hinkley
@rosshinkley
May 04 2016 15:31
np :)
Mingsterism
@mingsterism
May 04 2016 15:31
Will give the code a try. Let you know the result
Ross Hinkley
@rosshinkley
May 04 2016 15:32
right on, good luck
Mingsterism
@mingsterism
May 04 2016 15:58
@rosshinkley Works perfectly. thanks again.
But how do I prevent this problem in the future. Because I don't fully understand it.
Ross Hinkley
@rosshinkley
May 04 2016 15:59
np
Mingsterism
@mingsterism
May 04 2016 16:00
does it mean document.querySelectorAll or document.getElementByClassName will not work?
because HTMLCollection?
Ross Hinkley
@rosshinkley
May 04 2016 16:00
no, but in general, i don't think you'll want to return the results directly
Mingsterism
@mingsterism
May 04 2016 16:01
i see. So I should return an object?
Ross Hinkley
@rosshinkley
May 04 2016 16:01
yep, i'd recommend pulling out only what you need
Mingsterism
@mingsterism
May 04 2016 16:04
Thanks. Got it. But should I use Array.prototype.slice.call since you mentioned that is deliberate
Ross Hinkley
@rosshinkley
May 04 2016 16:14
yeah, the results of document.getElementsByClassname returns an HTMLCollection, which isn't an arrray
it's array-like, kind of like arguments in function(){ ...
so you can't directly use array prototype functions like map or slice
but you can apply them against the array-like
Mingsterism
@mingsterism
May 04 2016 16:19
I see. Ok. Got it now. much clearer.
Thanks :)
Ross Hinkley
@rosshinkley
May 04 2016 16:20
np :)
Maxwell Zhao
@thepiercingarrow
May 04 2016 18:10
@rosshinkley how would I do that?
Ross Hinkley
@rosshinkley
May 04 2016 18:51
@thepiercingarrow2 when you create a nightmare instance, do something like var nightmare = Nightmare({visible: true});
Maxwell Zhao
@thepiercingarrow
May 04 2016 19:02
Okay.. will try it when I get home