Skip to content

Instantly share code, notes, and snippets.

@artjomb
Created December 11, 2014 20:14
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 artjomb/82bce4ba312f63abb72a to your computer and use it in GitHub Desktop.
Save artjomb/82bce4ba312f63abb72a to your computer and use it in GitHub Desktop.
PhantomJS proof-of-concept that Google Maps can be used
var page = require('webpage').create(),
classical = true;
// This userAgent works good with GM
//page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1';
page.viewportSize = {
width: 1280,
height: 800
};
page.onError = function(msg, trace) {
var msgStack = ['ERROR: ' + msg];
if (trace && trace.length) {
msgStack.push('TRACE:');
trace.forEach(function(t) {
msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : ''));
});
}
console.error(msgStack.join('\n'));
};
page.onConsoleMessage = function(msg) {
console.log('Msg: ', msg);
};
function click(selector){
page.evaluate(function(selector){
// taken from here: http://stackoverflow.com/a/15948355
var ev = document.createEvent("MouseEvent");
ev.initMouseEvent(
"click",
true /* bubble */, true /* cancelable */,
window, null,
0, 0, 0, 0, /* coordinates */
false, false, false, false, /* modifier keys */
0 /*left*/, null
);
document.querySelector(selector).dispatchEvent(ev);
}, selector);
}
function type(text){
page.evaluate(function(text, classical){
var el = document.querySelector(classical ? "#gbqfq" : "#searchboxinput");
el.value = text;
el.focus();
}, text, classical);
page.sendEvent('keypress', page.event.key.Enter); // works
//page.sendEvent('keypress', page.event.key.Backspace); // doesn't work*/
}
page.open("https://www.google.com/maps", function (status) {
if (status !== 'success') {
console.log('Unable to access network');
phantom.exit();
} else {
if (classical) {
click("#paneltoggle2");
}
setTimeout(function(){
page.render("test53_1.png");
click(classical ? "#mv-primary-container > .mv-primary" : "button.widget-minimap-shim");
}, 1000); // +1 sec
setTimeout(function(){
page.render("test53_2.png");
type("tokio");
}, 11000); //+10 sec
setTimeout(function(){
if (classical) {
click("#paneltoggle2");
}
}, 16000); //+5 sec
setTimeout(function(){
page.render("test53_3.png");
phantom.exit();
}, 21000); //+5 sec
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment