SinF的Electron系列之一---Cookie和localStorage
Electron不支持之前在浏览器中使用localstroage的方法,暂时采用cookie解决。
- 需Electron默认的cookie是保存在主流程里面,所以需要使用remote模块提取
- getcookie的函数,我改写成了promise(),用参考博客里面的方法,无法获得cookie的返回值。
官方文档
https://electronjs.org/docs/a...参考博客
https://blog.csdn.net/changhu...使用
const Cookie = require('./static/js/cookie.js') Cookie.getCookies('token').then(function (res) { console.log('res', res) })
cookie.js代码
// cookie.js // 通过remote模块取的原本在主进程中才能使用的session模块 const session = require('electron').remote.session; /** * 获得 */ const Cookie = {} const Session = session Cookie.getCookies = (name) => { let data = [] return new Promise(function (resolve, reject) { Session.defaultSession.cookies.get({ name:name}, function (error, cookies) { console.log('cookies', cookies); if (cookies.length > 0) { data = cookies } resolve(cookies) }) }) }; /** * 清空缓存 */ Cookie.clearCookies = (url) => { Session.defaultSession.clearStorageData({ origin: url, storages: ['cookies'] }, function (error) { if (error) console.error(error); }) }; /** * 保存cookie * @param name cookie名称 * @param value cookie值 */ Cookie.setCookie = (name, value) => { let Days = 30; let exp = new Date(); let date = Math.round(exp.getTime() / 1000) + Days * 24 * 60 * 60; const cookie = { url: "", name: name, value: value, expirationDate: date }; Session.defaultSession.cookies.set(cookie, (error) => { if (error) console.error(error); }); }; // export default cookie module.exports = Cookie
相关推荐
houmenghu 2020-11-17
jincheng 2020-09-01
阳光之吻 2020-08-03
hzyuhz 2020-07-04
服务器端攻城师 2020-06-26
xuanwenchao 2020-06-14
Lophole 2020-06-13
明瞳 2020-06-08
Dreamya 2020-06-03
SoarFly00 2020-06-03
咻咻ing 2020-06-01
三动 2020-05-29
kentrl 2020-11-10
逍遥友 2020-10-26
Blueberry 2020-08-15
xclxcl 2020-08-03
zmzmmf 2020-08-03
PkJY 2020-07-08