6

I do want to render my page and exit PhantomJS in the evaluate function, because I want to trigger it when a specific event is fired.

I've tried something like this:

page.evaluate(page, function (page, phantom) { //do some stuff on my page //i want to execute this in an eventhandler of my page though thats not the problem page.render('imgName.png'); page.render('pdfName.pdf'); phantom.exit(); }, page, phantom); 

This doesn't work in my example, because page.render seems to be undefined. Maybe there is a serializer for the arguments which is not serializing functions of the obj?

Is this even possible? Does anyone know a solution for my issue?

I know I could set a while loop in my evaluate function and prevent its termination and do this page render stuff outside afterwards. I don't like this though.

1
  • Great question and great answer. Thanks! Commented Jan 31, 2017 at 15:06

1 Answer 1

15

page.evaluate() is sandboxed. The function that you pass in is evaluated in the page context and all arguments need to be serializable in order the be successfully passed in. You cannot pass page or phantom into page context. The docs say the following:

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!

This is what page.onCallback is for:

page.onCallback = function(data){ if (data.exit) { page.render('imgName.png'); page.render('pdfName.pdf'); phantom.exit(); } }; page.evaluate(page, function () { //do some stuff on my page //i want to execute this in an eventhandler of my page though thats not the problem window.callPhantom({ exit: true }); }); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.