Skip to content

Instantly share code, notes, and snippets.

@downthecrop
Last active December 6, 2020 19:44
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 downthecrop/d142514c3e8db5abc19c37164b3d7a66 to your computer and use it in GitHub Desktop.
Save downthecrop/d142514c3e8db5abc19c37164b3d7a66 to your computer and use it in GitHub Desktop.
Save and Load cookies in electron
session.defaultSession.cookies.get({
url: 'https://www.wegame.com.cn/'
})
.then((cookies) => {
cookieDump(cookies)
console.log("Wrote Cookies")
}).catch((error) => {
console.log(error)
})
//Create cookies.json or this will fail the first time.
function cookieDump(cookies) {
var buffer = '['
for (let cookie of cookies) {
console.log(cookie)
buffer += JSON.stringify(cookie)
buffer += ","
}
buffer = buffer.slice(0, -1) + "]"
fs.writeFile('cookies.json', buffer, function (err) {
if (err) return console.log(err);
console.log('Wrote cookies to file');
});
}
function cookieLoad(cookies) {
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
session.defaultSession.cookies.set({
url: "https://www.wegame.com.cn/",
name: cookie["name"],
value: cookie["value"],
domain: cookie["domain"],
path: cookie["path"],
secure: cookie["secure"],
httpOnly: cookie["httpOnly"],
expirationDate: Math.floor(new Date().getTime() / 1000) + 1209600
})
if (cookie["name"] == "tgp_ticket"){
console.log(cookie["name"])
tgp_ticket = cookie["value"]
}
if (cookie["name"] == "skey"){
console.log(cookie["name"])
skey = cookie["value"]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment