someone235
someone235

Reputation: 586

is it possible to call evaluation function from node scope in casperjs scope using spookyjs?

I try to pass to spooky an outside function, But when I call it, the returned value is 'undefined'. Here is my code:

        var eval_func = function(){
           return 123;
        };
        console.log('Outside spooky: ' + eval_func());
        var spooky = new Spooky({
            child: {
                transport: 'http',
            },
            casper: {
                logLevel: 'error',
            }
        }, function (err) {
            if (err) {
                e = new Error('Failed to initialize SpookyJS');
                e.details = err;
                throw e;
            }

            spooky.start('http://google.com/',[{
                eval_func:eval_func,
            },function(){
                console.log('Inside spooky: ' + eval_func());
            }]);
            spooky.run();
        });

        spooky.on('console', function (line) {
            console.log(line);
        });
    });

and the output is:

Outside spooky: 123

And I get "ReferenceError: Can't find variable: eval_func". Is it possible to do this without getting any ReferenceError?

Upvotes: 0

Views: 300

Answers (2)

nfarahani
nfarahani

Reputation: 26

If you need the value of the function, pass in the returned value:

var Spooky;
try {
  Spooky = require('spooky');
} catch (e) {
  Spooky = require('../lib/spooky');
}
var eval_func = function() {
  return 123;
};
console.log('Outside spooky: ' + eval_func());
var spooky = new Spooky({
  child: {
    transport: 'http',
  },
  casper: {
    logLevel: 'error',
  }
}, function(err) {
  if (err) {
    e = new Error('Failed to initialize SpookyJS');
    e.details = err;
    throw e;
  }

  spooky.start('http://google.com/', [{
    eval_func: eval_func(),
  }, function() {
    console.log('Inside spooky: ' + eval_func);
  }]);
  spooky.run();
});

spooky.on('console', function(line) {
  console.log(line);
});

If you need to call a function from SpookyJS, try "emit":

var Spooky;
try {
  Spooky = require('spooky');
} catch (e) {
  Spooky = require('../lib/spooky');
}
var eval_func = function() {
  return 123;
};
console.log('Outside spooky: ' + eval_func());
var spooky = new Spooky({
  child: {
    transport: 'http',
  },
  casper: {
    logLevel: 'error',
  }
}, function(err) {
  if (err) {
    e = new Error('Failed to initialize SpookyJS');
    e.details = err;
    throw e;
  }

  spooky.start('http://google.com/', function() {
    var spookyScope = 42;
    this.emit('eval_func_call', "Another value from within Spooky is " + 42);
  });

  spooky.on('eval_func_call', function(spookyValue) {
    console.log("Calling eval_func inside Spooky", eval_func());
    console.log("and...", spookyValue)
  });

  spooky.run();
});

spooky.on('console', function(line) {
  console.log(line);
});

which gives you:

Outside spooky: 123
Calling eval_func inside Spooky 123
and... Another value from within Spooky is 42

Upvotes: 0

someone235
someone235

Reputation: 586

OK, I found a good way to get around this. I copied the function string and then regenerated it in the casperjs scope.

        eval_func = function(){
            return 123;
        }
        console.log('Outside spooky: ' + eval_func());
        var spooky = new Spooky({
            child: {
                transport: 'http',
            },
            casper: {
                logLevel: 'error',
            }
        }, function (err) {
            if (err) {
                e = new Error('Failed to initialize SpookyJS');
                e.details = err;
                throw e;
            }
            eval_func_str = eval_func.toString();

            spooky.start('http://google.com/',[{
                eval_func_str:eval_func_str,
            },function(){
                eval("eval_func=" + eval_func_str);
                console.log('Inside spooky: ' + eval_func());
            }]);

            spooky.run();
        });

        spooky.on('console', function (line) {
            console.log(line);
        });

Upvotes: 2

Related Questions