2011-06-29 03:34:01 +04:00
|
|
|
// Use 'page.injectJs()' to load the script itself in the Page context
|
2011-06-07 20:20:22 +04:00
|
|
|
|
2015-04-10 10:51:00 +03:00
|
|
|
"use strict";
|
2011-06-07 20:20:22 +04:00
|
|
|
if ( typeof(phantom) !== "undefined" ) {
|
2011-09-13 00:17:54 +04:00
|
|
|
var page = require('webpage').create();
|
2011-06-07 20:20:22 +04:00
|
|
|
|
|
|
|
// Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this")
|
|
|
|
page.onConsoleMessage = function(msg) {
|
|
|
|
console.log(msg);
|
|
|
|
};
|
2016-01-19 18:24:19 +03:00
|
|
|
|
2011-06-07 20:20:22 +04:00
|
|
|
page.onAlert = function(msg) {
|
|
|
|
console.log(msg);
|
|
|
|
};
|
2016-01-19 18:24:19 +03:00
|
|
|
|
2011-06-07 20:20:22 +04:00
|
|
|
console.log("* Script running in the Phantom context.");
|
|
|
|
console.log("* Script will 'inject' itself in a page...");
|
|
|
|
page.open("about:blank", function(status) {
|
|
|
|
if ( status === "success" ) {
|
2011-06-08 02:22:41 +04:00
|
|
|
console.log(page.injectJs("injectme.js") ? "... done injecting itself!" : "... fail! Check the $PWD?!");
|
2011-06-07 20:20:22 +04:00
|
|
|
}
|
|
|
|
phantom.exit();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
alert("* Script running in the Page context.");
|
|
|
|
}
|