Skip arguments with page.evaluate

I use PhantomJS page.evaluate () to do some curettage. My problem is that the code I submit to the webkit page is isolated and therefore does not have access to the variables of my main phantom script. This makes it difficult to create a common scrambling code.

page.open(url, function() { var foo = 42; page.evaluate(function() { // this code has no access to foo console.log(foo); }); } 

How can I click arguments on a page?

+44
javascript phantomjs
Mar 23 2018-12-23T00:
source share
7 answers

I had this exact problem. This can be done with a little trick, because page.evaluate can also accept a string.

There are several ways to do this, but I use a shell called evaluate , which takes additional parameters to go to a function that needs to be evaluated on the website side. You would use it as follows:

 page.open(url, function() { var foo = 42; evaluate(page, function(foo) { // this code has now has access to foo console.log(foo); }, foo); }); 

And here is the evaluate() function:

 /* * 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); } 
+59
Mar 23 '12 at 11:13
source share
— -

This change has been pushed, and now you can use it as

 page.open(url, function() { var foo = 42; page.evaluate( function(foo) { // this code has now has access to foo console.log(foo); }, foo); } 

Details click here: https://github.com/ariya/phantomjs/commit/81794f9096

+59
Jun 28 2018-12-12T00:
source share

Another possibility: pass variables to URL. For example, pass object 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 + ')'); )} }); 
+2
May 22 '12 at 3:45
source share

There is a solution that works with PhantomJS 0.9.2 and 0.2.0:

 page.evaluate( function (aa, bb) { document.title = aa + "/" + bb;}, //the function function (result) {}, // a callback when it done "aaa", //attr 1 "bbb"); //attr 2 
+2
Feb 20 '15 at 12:47
source share

This works for me:

 page.evaluate("function() {document.body.innerHTML = '" + size + uid + "'}"); 

So put everything as a string. One way or another, it will become a string. Check the source of the library.

0
Feb 17 '15 at 19:39
source share

Can't you just bind the arguments to the function?

 page.evaluate.bind(args)(callbackFn) 
0
Aug 19 '16 at 1:41
source share

While you can pass arguments to evaluate (function, arg1, arg2, ...) , this is often a bit cumbersome. Especially in cases where the transfer of several variables or, even worse, functions.

To get around this obstacle, instead of injectJs (filename) .

 page.open(url, function() { if ( webpage.injectJs('my_extra_functionality.js') ) { page.evaluate( function() { // this code has access to foo and also myFunction(); console.log(foo); console.log(myFunction()); }); } else { console.log("Failed to inject JS"); } } 



Where my_extra_functionality.js is a local file in the same directory:

 var foo = 42; var myFunction = function(){ return "Hello world!"; } 
0
Jul 05 '17 at 7:56 on
source share



All Articles