injectme.js 865 B

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