|
var webpage = require('webpage'); |
|
var fs = require('fs'); |
|
var page = webpage.create(); |
|
|
|
readCookiesFromFile = function() { |
|
cookieJar = []; |
|
if(fs.isFile(cookieJarFilePath)) { |
|
cookieJar = JSON.parse(fs.read(cookieJarFilePath)); |
|
} |
|
for(var j in cookieJar) { |
|
phantom.addCookie({ |
|
'name' : cookieJar[j].name, |
|
'value' : cookieJar[j].value, |
|
'domain' : cookieJar[j].domain, |
|
'path' : cookieJar[j].path, |
|
'httponly' : cookieJar[j].httponly, |
|
'secure' : cookieJar[j].secure, |
|
'expires' : cookieJar[j].expires |
|
}); |
|
} |
|
}; |
|
|
|
readCookiesFromFile(); |
|
|
|
writeCookiesToFile = function() { |
|
cookieJar = []; |
|
for(var j in phantom.cookies) { |
|
cookieJar.push({ |
|
name: phantom.cookies[j].name, |
|
value: phantom.cookies[j].value, |
|
domain: phantom.cookies[j].domain, |
|
path: phantom.cookies[j].path, |
|
httponly: phantom.cookies[j].httponly, |
|
secure: phantom.cookies[j].secure, |
|
expires: phantom.cookies[j].expires |
|
}); |
|
} |
|
|
|
if(!fs.isFile(cookieJarFilePath)){ |
|
fs.write(filePath, JSON.stringify(cookieJar), 'w'); |
|
} |
|
}; |
|
|
|
page.open('http://localhost/mywebpage.html', 'GET', function(status) { |
|
if(status != 'success') { |
|
phantom.exit(1); |
|
} |
|
else { |
|
setTimeout(function () { |
|
writeCookiesToFile(); |
|
phantom.exit(); |
|
}, 10000); |
|
} |
|
}); |