是否可以使用spookyjs从casperjs范围内的节点范围调用评估函数?
is it possible to call evaluation function from node scope in casperjs scope using spookyjs?
我试图将一个外部函数传递给spooky,但是当我调用它时,返回值是'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: Can't find variable: 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 调用函数,请尝试 "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
我试图将一个外部函数传递给spooky,但是当我调用它时,返回值是'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: Can't find variable: 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 调用函数,请尝试 "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