Created
April 26, 2011 15:36
-
-
Save gtzilla/942498 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// phantom_test.js | |
// bit_spam | |
// | |
// Created by gregory tomlinson on 2011-04-23. | |
// Copyright 2011 the public domain. All rights reserved. | |
// | |
// requires phantomjs (sudo port -v install phantomjs) | |
// -- requires qt4-(os) aka qt4-mac | |
/* | |
Phantom Notes | |
Currently, there is no 'great' way to maintain state across | |
page reloads, this makes it difficult to track items | |
The 'magic' phantom.state | |
is allowed to maintain a state over multiple loads, however, the datatype of it MAY be limited to strings | |
*/ | |
var document_active=false,locations_lst=[], page_wait_ms=20, | |
last_known_url, | |
error_status=false; | |
function open_connection() { | |
var args = phantom.args; | |
if( args ) { | |
phantom.state = last_known_url= args[0]; | |
phantom.userAgent="bitly"; | |
console.log("Open Connection: " + last_known_url) | |
phantom.open(last_known_url); | |
} else { | |
phantom.exit(0); | |
} | |
} | |
function print_message() { | |
console.log("Page Redirect. URL is : " + document.location.href); | |
} | |
function on_page_unload(e) { | |
document_active=true; // set 'global' var | |
// phantom.state.count+=1; | |
error_status=true; | |
// use document_active as a lazy check for redirects | |
} | |
function close_connection() { | |
phantom.sleep(page_wait_ms); | |
if(!document_active) { | |
console.log("Close Connection"); | |
print_message(); | |
if(phantom.state !== document.location.href) { | |
error_status=true; | |
} | |
phantom.exit( (phantom.state > 0 || error_status ) ? 1 : 0 ); | |
} | |
} | |
function main() { | |
if (phantom.state.length === 0) { | |
console.log("open connection") | |
open_connection(); | |
} else { | |
if(phantom.loadStatus == "success") { | |
document_active=false; | |
window.onbeforeunload=on_page_unload; | |
console.log("Title: " + document.title); | |
console.log("Document: " + document.location.href); | |
// todo, search for metarefresh tags | |
close_connection(); | |
} | |
} | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment