pagecallback.js 623 B

123456789101112131415161718
  1. "use strict";
  2. var p = require("webpage").create();
  3. p.onConsoleMessage = function(msg) { console.log(msg); };
  4. // Calls to "callPhantom" within the page 'p' arrive here
  5. p.onCallback = function(msg) {
  6. console.log("Received by the 'phantom' main context: "+msg);
  7. return "Hello there, I'm coming to you from the 'phantom' context instead";
  8. };
  9. p.evaluate(function() {
  10. // Return-value of the "onCallback" handler arrive here
  11. var callbackResponse = window.callPhantom("Hello, I'm coming to you from the 'page' context");
  12. console.log("Received by the 'page' context: "+callbackResponse);
  13. });
  14. phantom.exit();