Path: blob/master/6-Selenium/phantomjs/examples/injectme.js
164 views
// Use 'page.injectJs()' to load the script itself in the Page context12if ( typeof(phantom) !== "undefined" ) {3var page = require('webpage').create();45// Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this")6page.onConsoleMessage = function(msg) {7console.log(msg);8};910page.onAlert = function(msg) {11console.log(msg);12};1314console.log("* Script running in the Phantom context.");15console.log("* Script will 'inject' itself in a page...");16page.open("about:blank", function(status) {17if ( status === "success" ) {18console.log(page.injectJs("injectme.js") ? "... done injecting itself!" : "... fail! Check the $PWD?!");19}20phantom.exit();21});22} else {23alert("* Script running in the Page context.");24}252627