Skip to content

Instantly share code, notes, and snippets.

@tvieira
Created February 2, 2018 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tvieira/725f6b82f0efb7d59de170627d37334c to your computer and use it in GitHub Desktop.
Save tvieira/725f6b82f0efb7d59de170627d37334c to your computer and use it in GitHub Desktop.
Event listeners for PhantomJS (taken from PhantomJS examples)
"use strict";
var sys = require("system"),
page = require("webpage").create(),
logResources = false;
if (sys.args.length > 1 && sys.args[1] === "-v") {
logResources = true;
}
function printArgs() {
var i, ilen;
for (i = 0, ilen = arguments.length; i < ilen; ++i) {
console.log(" arguments[" + i + "] = " + JSON.stringify(arguments[i]));
}
console.log("");
}
page.onInitialized = function() {
console.log("page.onInitialized");
printArgs.apply(this, arguments);
};
page.onLoadStarted = function() {
console.log("page.onLoadStarted");
printArgs.apply(this, arguments);
};
page.onLoadFinished = function() {
console.log("page.onLoadFinished");
printArgs.apply(this, arguments);
};
page.onUrlChanged = function() {
console.log("page.onUrlChanged");
printArgs.apply(this, arguments);
};
page.onNavigationRequested = function() {
console.log("page.onNavigationRequested");
printArgs.apply(this, arguments);
};
if (logResources === true) {
page.onResourceRequested = function() {
console.log("page.onResourceRequested");
printArgs.apply(this, arguments);
};
page.onResourceReceived = function() {
console.log("page.onResourceReceived");
printArgs.apply(this, arguments);
};
}
// window.alert(msg);
page.onAlert = function() {
console.log("page.onAlert");
printArgs.apply(this, arguments);
};
// var confirmed = window.confirm(msg);
page.onConfirm = function() {
console.log("page.onConfirm");
printArgs.apply(this, arguments);
};
// var user_value = window.prompt(msg, default_value);
page.onPrompt = function() {
console.log("page.onPrompt");
printArgs.apply(this, arguments);
};
// an example to take a screen shot at a certain time
// in this case, after 5 secs.
setTimeout(function() {
console.log("");
console.log("### Screenshot");
setTimeout(function(){
page.render('screenshot.png');
}, 1000);
}, 5000)
// every step could be done like
// load login page
setTimeout(function() {
console.log("");
console.log("### STEP 1: Load '" + MYURL + "'");
page.open(MYURL);
}, 5000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment