0

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

        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 的情况下执行此操作?

4

2 回答 2

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);
        });
于 2015-07-01T16:55:48.380 回答
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
于 2017-02-09T22:23:06.960 回答