Ryguy
Ryguy

Reputation: 266

Emit Event from within Evaluate CasperJS

I'm having a problem downloading files from urls I am creating when scrapping a website. Currently I discovering the month and year of a file then replacing the values in a url and downloading trying to download from that location. I understand that you can't use the download function from inside the evaluation scope.

this.evaluate(function collectAllData (MONTHS) {
    for (...) {
        // Create url from page information ...
        casper.emit('test.download', url, fileName); 
    }
}, MONTHS);

casper.on('remote.download', function processRemoteDownload(url, fileName) {
    this.download(url, fileName);
});

Is there anyway to emit a custom event from within evaluate? I don't want to navigate away from the current page I am on or have to go back and forth from the evaluate scope. I know I could return a list of urls and process them after the fact but was curious if this was possible. Thanks for any help.

Upvotes: 3

Views: 1203

Answers (2)

user3027221
user3027221

Reputation: 656

Use inside the evaluate callback:

console.log("casper-event:add:[1234]");

then can do it like this (not tested):

casper.on('remote.message', function(msg) {
   if(msg.indexOf("casper-event:" == 0))
   {
       var event = msg.replace(/^casper-event:/, '').replace(/:.*$/, '');
       var result = JSON.parse(msg.replace(/^casper-event:.*?:/, ''));
       this.emit(event, result);
   }
})

casper.on('add'........

Upvotes: 1

rickyduck
rickyduck

Reputation: 4084

Here's what I did for DOMContentLoaded:

casper.then(function getDOMLoaded(){
    this.evaluate(function(){
        window.__DOMLoaded = false;
        document.addEventListener("DOMContentLoaded", function(){
            window.__DOMLoaded = true;
        })
    })
});
casper.waitFor(function check() {
    return this.getGlobal('__DOMLoaded');
}, function then() {    // step to execute when check() is ok
    casper.echo("DOMContentReady in " + (Date.now()-start) + " ms", "INFO_BAR");
}, function timeout() { // step to execute if check has failed
    casper.echo("DOMContentReady took longer than 10 seconds!")
}, 10000);

It sets a global variable, which is updated in the WebPage via evaluate. I then run a waitFor which attempts (for 10s) to check whether window.__DOMLoaded is true (however, within casper but not evaluate, this is accessed via this.getGlobal()

Upvotes: 0

Related Questions