Created
September 30, 2012 22:35
-
-
Save davisford/3808630 to your computer and use it in GitHub Desktop.
phantom cookies puzzle
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
var page = require('webpage').create(), | |
fs = require('fs'), | |
requests = [], responses = []; | |
phantom.cookiesEnabled = true; | |
console.log('\n\n cookies we know about before page.open => \n\n' + JSON.stringify(phantom.cookies, null, 2)); | |
// clear log from last time | |
if (fs.exists('requests.log')) { | |
fs.remove('requests.log'); | |
} | |
// spoof user-agent | |
page.settings.userAgent = | |
'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0'; | |
const USER = 'cvUg338yQa'; | |
const PASS = 'GNyoqNKJK5'; | |
page.onResourceRequested = function(req) { | |
if (req.url.match(/http:\/\/www.reddit.com\//)) { | |
console.log('\n\n cookies we know about => \n\n' + JSON.stringify(phantom.cookies, null, 2)); | |
requests.push(req); | |
} | |
} | |
page.onResourceReceived = function(res) { | |
if (res.url.match(/http:\/\/www.reddit.com\//)) { | |
responses.push(res); | |
} | |
} | |
function logRequests() { | |
var file = fs.open('requests.log', 'a'); | |
file.writeLine(JSON.stringify(requests, null, 2)); | |
file.close(); | |
requests.length = 0; | |
file = fs.open('responses.log', 'a'); | |
file.writeLine(JSON.stringify(responses, null, 2)); | |
file.close(); | |
responses.length = 0; | |
} | |
page.onLoadFinished = function() { | |
logRequests(); | |
var user = page.evaluate(function () { | |
return $('span.user').children('a').html(); | |
}); | |
if (user !== USER) { | |
console.log('forcing login...'); | |
page.evaluate(function(username, password) { | |
$('input[name="user"]:first').attr('value', username); | |
$('input[name="passwd"]:first').attr('value', password); | |
$("form#login_login-main").submit(); | |
}, USER, PASS); | |
} else { | |
console.log('already logged in...'); | |
phantom.exit(); | |
} | |
}; | |
page.open('http://www.reddit.com'); |
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
var page = require('webpage').create(); | |
phantom.cookiesEnabled = true; | |
console.log('\n\n cookies we know about => \n\n' + JSON.stringify(phantom.cookies, null, 2)); | |
// spoof user-agent | |
page.settings.userAgent = | |
'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0'; | |
const USER = 'cvUg338yQa'; | |
const PASS = 'GNyoqNKJK5'; | |
page.onLoadFinished = function() { | |
var user = page.evaluate(function () { | |
return $('span.user').children('a').html(); | |
}); | |
if (user !== USER) { | |
console.log('forcing login...'); | |
page.evaluate(function(username, password) { | |
$('input[name="user"]:first').attr('value', username); | |
$('input[name="passwd"]:first').attr('value', password); | |
$("form#login_login-main").submit(); | |
}, USER, PASS); | |
} else { | |
console.log('already logged in...'); | |
phantom.exit(); | |
} | |
}; | |
page.open('http://www.reddit.com'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment