Javascript 使用page.evaluate传递参数
我正在使用PhantomJS page.evaluate()进行一些刮片。我的问题是,我传递到webkit页面的代码是沙盒,因此无法访问我的主幻影脚本的变量。这使得很难使刮削代码通用化Javascript 使用page.evaluate传递参数,javascript,phantomjs,Javascript,Phantomjs,我正在使用PhantomJS page.evaluate()进行一些刮片。我的问题是,我传递到webkit页面的代码是沙盒,因此无法访问我的主幻影脚本的变量。这使得很难使刮削代码通用化 page.open(url, function() { var foo = 42; page.evaluate(function() { // this code has no access to foo console.log(foo); }); } 我怎样才能把参数推到页面中呢
page.open(url, function() {
var foo = 42;
page.evaluate(function() {
// this code has no access to foo
console.log(foo);
});
}
我怎样才能把参数推到页面中呢?我遇到了这个问题。这可以通过一些技巧来完成,因为
page.evaluate
也可以接受字符串
有几种方法可以做到这一点,但我使用了一个名为evaluate
的包装器,它接受额外的参数传递给必须在webkit端进行评估的函数。您可以这样使用它:
page.open(url, function() {
var foo = 42;
evaluate(page, function(foo) {
// this code has now has access to foo
console.log(foo);
}, foo);
});
下面是evaluate()
函数:
/*
* This function wraps WebPage.evaluate, and offers the possibility to pass
* parameters into the webpage function. The PhantomJS issue is here:
*
* http://code.google.com/p/phantomjs/issues/detail?id=132
*
* This is from comment #43.
*/
function evaluate(page, func) {
var args = [].slice.call(arguments, 2);
var fn = "function() { return (" + func.toString() + ").apply(this, " + JSON.stringify(args) + ");}";
return page.evaluate(fn);
}
另一种可能性是:将变量与url一起传递。例如,传递对象x
// turn your object "x" into a JSON string
var x_json = JSON.stringify(x);
// add to existing url
// you might want to check for existing "?" and add with "&"
url += '?' + encodeURIComponent(x_json);
page.open(url, function(status){
page.evaluate(function(){
// retrieve your var from document URL - if added with "&" this needs to change
var x_json = decodeURIComponent(window.location.search.substring(1));
// evil or not - eval is handy here
var x = eval('(' + x_json + ')');
)}
});
更改已推送,现在您可以将其用作
page.open(url, function() {
var foo = 42;
page.evaluate( function(foo) {
// this code has now has access to foo
console.log(foo);
}, foo);
}
推送详细信息如下:这对我有用:
page.evaluate("function() {document.body.innerHTML = '" + size + uid + "'}");
意思是把所有的东西都当作一个字符串。不管怎样,后来它变成了一根弦。检查库源代码 有一种解决方案可用于PhantomJS 0.9.2和0.2.0:
page.evaluate(
function (aa, bb) { document.title = aa + "/" + bb;}, //the function
function (result) {}, // a callback when it's done
"aaa", //attr 1
"bbb"); //attr 2
你不能把参数绑定到函数吗
page.evaluate.bind(args)(callbackFn)
虽然可以将参数传递到,但这通常有点麻烦。特别是在传递多个变量或更糟的函数时 要绕过这个障碍,可以使用
其中
my_extra_functionality.js
是同一目录中的本地文件:
var foo = 42;
var myFunction = function(){
return "Hello world!";
}
您可以将参数作为参数传递给page.evaluate函数 例如:
page.evaluate(function(arg1, arg2){
console.log(arg1); //Will print "hi"
console.log(arg2); //Will print "there"
}, "hi", "there");
如果您传递的对象不复杂,例如函数等,这是最好的方法。您能提供一个传递多个参数的示例吗?对于那些想要传递多个参数的人,您是否尝试过使用数组作为单个参数?在PhantomJS中不起作用,因为
page.evaluate()
是沙盒。这可能不是直接针对PhantomJS的,而是node.js和PhantomJS之间的桥梁。这类桥使用的语法稍有不同。如果您试图将参数传递到。文件仍然新鲜。我几乎放弃了,直到我找到了这个答案。谢谢明亮的这段源代码清楚地解释了它是如何工作的。要深入解释为什么会发生这种情况,请检查以下问题:您的解决方案似乎不错,但如果我想通过回调,是否可能?两天的时间变成了垃圾。JS回调中是否存在类似全局对象的参数?我相信您会发现这个答案更加充分:
page.evaluate(function(arg1, arg2){
console.log(arg1); //Will print "hi"
console.log(arg2); //Will print "there"
}, "hi", "there");