rosshinkley / nightmare-inline-download

36 stars 8 forks source link

Do I need to `click` or can I `goto(downloadUrl)` directly? #9

Closed thomasspiesser closed 7 years ago

thomasspiesser commented 8 years ago

Hey, I'm playing with downloading files and was wondering if it is at all possible to supply the downloadUrl directly to goto somehow. Like a request.

Like e.g. "https://github.com/segmentio/nightmare/archive/master.zip"

My use case is that I need to authenticate on a page and then want to download some files where I have the Urls but no button to click on to get to those Urls. Hence I want to start the download programmatically. Any way to do this?

Thanks in advance for taking the time.

rosshinkley commented 7 years ago

The problem with going to a link directly is Chromium (and consequently, Electron) issues a navigation aborted event, which will cause Nightmare to error out.

There are a couple of options here. You could write a simple file out with the links you want to download and click them with the same instance as your logged in session. That's a little hackish, but it should work. The alternative is to add a .downloadGoto() (or something similar) to this library that could handle going directly to downloaded resources.

thomasspiesser commented 7 years ago

Hey @rosshinkley thanks for your answer. I will play around with the hackish approach a little and see where it gets me. Will close this for the mo, thanks again.

Kinzi commented 7 years ago

👍🏻 for .downloadGoto()

ghost commented 7 years ago

Kind of another hackish, lame-o solution, but for posterity...

...
const nightmare = new Nightmare()
nightmare
    // login      
    .goto(loginUrl)
    .type('#eid', 'some.user')
    .type('#pw', 'supermegaawesomeuberkillerpassword')
    .click('#submit')
    // wait for the login to complete
    .wait('.all-sites-icon')
    // create a lame-o link hack
    .goto(`javascript:document.write(\"<a href='${downloadUrl}' id='click-me'>Click</a>\");`)
    // then click it
    .click('#click-me')
    .download(pathToSaveTo)
    .end()
    .then(() => {
        console.log("Download completed.")
    })
    .catch(function (error) {
        console.error('Fail:', error);
    });
...