是否可以使用spookyjs从casperjs范围内的节点范围调用评估函数?

时间:2015-07-01 16:19:25

标签: javascript node.js phantomjs casperjs spookyjs

我试图传递给一个外部函数,但是当我调用它时,返回的值是“未定义的”#39;。 这是我的代码:

        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);
        });
    });

,输出为:

Outside spooky: 123

我得到" ReferenceError:无法找到变量:eval_func"。 是否可以在没有任何ReferenceError的情况下执行此操作?

2 个答案:

答案 0 :(得分:2)

好的,我找到了解决这个问题的好方法。我复制了函数字符串,然后在casperjs范围内重新生成它。

        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);
        });

答案 1 :(得分:0)

如果您需要该函数的值,请传入返回的值:

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);
});

如果你需要从SpookyJS调用一个函数,请尝试“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);
});

给你:

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