Search code examples
phantomjspuppeteergoogle-chrome-headless

Is it possible to send data from client (browser) to puppeteer?


I have been created a server side render by phantomjs before like this.

client side

// when all client ajax request and page render finished
window.callPhantom('page.done')

// when page not found
window.callPhantom('page.fail')

server side

page.onCallback = function(status) {
  // when page render finished
  if(status == 'page.done') {
    // start render
  }

  // when page not found
  else {
    // response 404 page not found
  }
}

And I wanna change from phantomjs to chrome puppeteer now.

How can I to pass data from the client side (browser) to server(puppeteer) after all client ajax request is finished (like phantomjs callPhantom).

Is it possible do that by using chrome puppeteer?


Solution

  • I had face the same problem as you, this is the solution what I got:

    You can use the function page.exposeFunction.

    It's going to bind a function into the page window, so you can just evaluate that function in the client side to do something in the Node side.

    There's an example in the documentation.

    I hope this can helps you.