Created
February 1, 2014 12:06
-
-
Save achiko/8751422 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
/** | |
* Load cookies from a file | |
* | |
* @param {String} file name of da file. | |
* | |
* @return {Array} of cookies. | |
*/ | |
function loadCookies(file) { | |
"use strict"; | |
var cookies = []; | |
if (fs.exists(file)) { | |
cookies = fs.read(file).split("\r\n"); | |
cookies.forEach(function (cookie) { | |
var detail = cookie.split("\t"), | |
newCookie = { | |
'name': detail[5], | |
'value': detail[6], | |
'domain': detail[0], | |
'path': detail[2], | |
'httponly': false, | |
'secure': false, | |
'expires': (new Date()).getTime() + 3600 * 24 * 30 /* <- expires in 1 month */ | |
}; | |
phantom.addCookie(newCookie); | |
}); | |
} else { | |
this.log("Unable to load cookies from " + file + ". File doesn't exist", "warning"); | |
} | |
return cookies; | |
}; | |
/** | |
* Save cookies to a file | |
* | |
* @param {String} file name of da file. | |
*/ | |
function saveCookies(file) { | |
"use strict"; | |
var res = ''; | |
this.page.cookies.forEach(function (cookie) { | |
res += utils.format("%s\t%s\t%s\t%s\t%s\t%s\t%s\r\n", cookie.domain, 'TRUE', cookie.path, 'FALSE', cookie.expiry, cookie.name, cookie.value); | |
}); | |
fs.write(file, res, 'w'); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment