本文介绍了电子饼干的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于电子饼干,我使用
For electron cookie I used https://www.npmjs.com/package/electron-cookies
然后将此添加到我的html中
Then added this into my html
<script type="text/javascript">
require('electron-cookies')
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function getCookie12(name) {
var regexp = new RegExp("(?:^" + name + "|;\s*"+ name + ")=(.*?)(?:;|$)", "g");
var result = regexp.exec(document.cookie);
alert(document.cookie);
return (result === null) ? null : result[1];
}
</script>
并调用方法:
<button onclick="createCookie('ppkcookie','testcookie',7)">Set Cookie</button>
<button onclick="getCookie12('ppkcookie')">Get Cookie</button>
但是警报(document.cookie)
只显示我
but the alert(document.cookie)
shows me only
ppkcookie
不是 ppkcookie = testcookie
任何想法为何?
非常感谢
推荐答案
这就是电子处理自己的cookie的方式。
This is how electron handles his own cookies.
var session = require('electron').remote.session;
var ses = session.fromPartition('persist:name');
这是设置cookie的方法
This is how to set a cookie
function setCookie(data, name) {
var expiration = new Date();
var hour = expiration.getHours();
hour = hour + 6;
expiration.setHours(hour);
ses.cookies.set({
url: BaseURL, //the url of the cookie.
name: name, // a name to identify it.
value: data, // the value that you want to save
expirationDate: expiration.getTime()
}, function(error) {
/*console.log(error);*/
});
}
这是获取cookie值的方法
This is how to get the value of the cookie
function getCookie(name) {
var value = {
name: name // the request must have this format to search the cookie.
};
ses.cookies.get(value, function(error, cookies) {
console.console.log(cookies[0].value); // the value saved on the cookie
});
}
有关电子饼干的更多信息,请阅读
For more information about the cookies of electron you can read here
这篇关于电子饼干的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!