injectme.js 859 B

12345678910111213141516171819202122232425
  1. // Use 'page.injectJs()' to load the script itself in the Page context
  2. if ( typeof(phantom) !== "undefined" ) {
  3. var page = require('webpage').create();
  4. // Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this")
  5. page.onConsoleMessage = function(msg) {
  6. console.log(msg);
  7. };
  8. page.onAlert = function(msg) {
  9. console.log(msg);
  10. };
  11. console.log("* Script running in the Phantom context.");
  12. console.log("* Script will 'inject' itself in a page...");
  13. page.open("about:blank", function(status) {
  14. if ( status === "success" ) {
  15. console.log(page.injectJs("injectme.js") ? "... done injecting itself!" : "... fail! Check the $PWD?!");
  16. }
  17. phantom.exit();
  18. });
  19. } else {
  20. alert("* Script running in the Page context.");
  21. }