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