I’m using PhantomJS page.evaluate() to do some scraping. My problem is that the code I pass to the webkit page is sandboxed, and so has no access to the variables of my main phantom script. This makes it hard make the scraping code generic.
page.open(url, function() {
var foo = 42;
page.evaluate(function() {
// this code has no access to foo
console.log(foo);
});
}
How could I push arguments into the page?
I’ve had that exact problem. It can be done with a little trickery, because
page.evaluatealso can accept a string.There are several ways to do it, but I use a wrapper called
evaluate, which accepts additional parameters to pass to the function that must be evaluated on the webkit side. You would use it like this:And here is the
evaluate()function: