繁体   English   中英

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

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

我试图通过一个怪异的外部函数,但是当我调用它时,返回的值是'undefined'。 这是我的代码:

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

好的,我找到了解决这个问题的好方法。 我复制了功能字符串,然后在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);
        });

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

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调用函数,请尝试“发出”:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM