Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
REMitchell
GitHub Repository: REMitchell/python-crawling
Path: blob/master/6-Selenium/phantomjs/examples/injectme.js
164 views
1
// Use 'page.injectJs()' to load the script itself in the Page context
2
3
if ( typeof(phantom) !== "undefined" ) {
4
var page = require('webpage').create();
5
6
// Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this")
7
page.onConsoleMessage = function(msg) {
8
console.log(msg);
9
};
10
11
page.onAlert = function(msg) {
12
console.log(msg);
13
};
14
15
console.log("* Script running in the Phantom context.");
16
console.log("* Script will 'inject' itself in a page...");
17
page.open("about:blank", function(status) {
18
if ( status === "success" ) {
19
console.log(page.injectJs("injectme.js") ? "... done injecting itself!" : "... fail! Check the $PWD?!");
20
}
21
phantom.exit();
22
});
23
} else {
24
alert("* Script running in the Page context.");
25
}
26
27