Skip to main content
Commonmark migration
Source Link

PhantomJS has two contexts. The inner context or page context is defined by page.evaluate(). It is sandboxed and has no access to variables defined outside. So, it doesn't know what phantom is. In the same way the outer context doesn't know what $ is, because jQuery is a DOM library and was injected into the page. You need to wrap your jQuery request in page.evaluate().

The other thing is that now, phantom.exit() doesn't mean anything. You need to tell PhantomJS to exit from inside of the page context, because the request is asynchronous. That's where the page.onCallback and window.callPhantom() pair comes in.

page.onCallback = function(data){ if (data.type === "exit") { phantom.exit(); } }; function start(){ page.includeJs('https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js', function() { console.log("got here"); page.evaluate(function(){ $.get("http://my-wonderful-site.com") .done(function( data ) { console.log("here!"); console.log(data); window.callPhantom({type: "exit"}); }); }); }); } 

console.log() is defined inside of the page context, but you can't see it there, because PhantomJS doesn't pass them out by default. You have to register to the page.onConsoleMessage event.

You can also send the data to the outer context with the help of callPhantom() instead of logging it. Note that not everything can be passed between the contexts:

Note: The arguments and the return value to the evaluate function must be a simple primitive object. The rule of thumb: if it can be serialized via JSON, then it is fine.

 

Closures, functions, DOM nodes, etc. will not work!

The other helpful event handlers are onError, onResourceError, onResourceTimeout in case there are still problems.

PhantomJS has two contexts. The inner context or page context is defined by page.evaluate(). It is sandboxed and has no access to variables defined outside. So, it doesn't know what phantom is. In the same way the outer context doesn't know what $ is, because jQuery is a DOM library and was injected into the page. You need to wrap your jQuery request in page.evaluate().

The other thing is that now, phantom.exit() doesn't mean anything. You need to tell PhantomJS to exit from inside of the page context, because the request is asynchronous. That's where the page.onCallback and window.callPhantom() pair comes in.

page.onCallback = function(data){ if (data.type === "exit") { phantom.exit(); } }; function start(){ page.includeJs('https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js', function() { console.log("got here"); page.evaluate(function(){ $.get("http://my-wonderful-site.com") .done(function( data ) { console.log("here!"); console.log(data); window.callPhantom({type: "exit"}); }); }); }); } 

console.log() is defined inside of the page context, but you can't see it there, because PhantomJS doesn't pass them out by default. You have to register to the page.onConsoleMessage event.

You can also send the data to the outer context with the help of callPhantom() instead of logging it. Note that not everything can be passed between the contexts:

Note: The arguments and the return value to the evaluate function must be a simple primitive object. The rule of thumb: if it can be serialized via JSON, then it is fine.

 

Closures, functions, DOM nodes, etc. will not work!

The other helpful event handlers are onError, onResourceError, onResourceTimeout in case there are still problems.

PhantomJS has two contexts. The inner context or page context is defined by page.evaluate(). It is sandboxed and has no access to variables defined outside. So, it doesn't know what phantom is. In the same way the outer context doesn't know what $ is, because jQuery is a DOM library and was injected into the page. You need to wrap your jQuery request in page.evaluate().

The other thing is that now, phantom.exit() doesn't mean anything. You need to tell PhantomJS to exit from inside of the page context, because the request is asynchronous. That's where the page.onCallback and window.callPhantom() pair comes in.

page.onCallback = function(data){ if (data.type === "exit") { phantom.exit(); } }; function start(){ page.includeJs('https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js', function() { console.log("got here"); page.evaluate(function(){ $.get("http://my-wonderful-site.com") .done(function( data ) { console.log("here!"); console.log(data); window.callPhantom({type: "exit"}); }); }); }); } 

console.log() is defined inside of the page context, but you can't see it there, because PhantomJS doesn't pass them out by default. You have to register to the page.onConsoleMessage event.

You can also send the data to the outer context with the help of callPhantom() instead of logging it. Note that not everything can be passed between the contexts:

Note: The arguments and the return value to the evaluate function must be a simple primitive object. The rule of thumb: if it can be serialized via JSON, then it is fine.

Closures, functions, DOM nodes, etc. will not work!

The other helpful event handlers are onError, onResourceError, onResourceTimeout in case there are still problems.

Source Link
Artjom B.
  • 62k
  • 26
  • 137
  • 236

PhantomJS has two contexts. The inner context or page context is defined by page.evaluate(). It is sandboxed and has no access to variables defined outside. So, it doesn't know what phantom is. In the same way the outer context doesn't know what $ is, because jQuery is a DOM library and was injected into the page. You need to wrap your jQuery request in page.evaluate().

The other thing is that now, phantom.exit() doesn't mean anything. You need to tell PhantomJS to exit from inside of the page context, because the request is asynchronous. That's where the page.onCallback and window.callPhantom() pair comes in.

page.onCallback = function(data){ if (data.type === "exit") { phantom.exit(); } }; function start(){ page.includeJs('https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js', function() { console.log("got here"); page.evaluate(function(){ $.get("http://my-wonderful-site.com") .done(function( data ) { console.log("here!"); console.log(data); window.callPhantom({type: "exit"}); }); }); }); } 

console.log() is defined inside of the page context, but you can't see it there, because PhantomJS doesn't pass them out by default. You have to register to the page.onConsoleMessage event.

You can also send the data to the outer context with the help of callPhantom() instead of logging it. Note that not everything can be passed between the contexts:

Note: The arguments and the return value to the evaluate function must be a simple primitive object. The rule of thumb: if it can be serialized via JSON, then it is fine.

Closures, functions, DOM nodes, etc. will not work!

The other helpful event handlers are onError, onResourceError, onResourceTimeout in case there are still problems.