-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookies.js
102 lines (92 loc) · 2.64 KB
/
cookies.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
function setCookie(cookieName, cookieValue, expireDays) {
const date = new Date();
date.setTime(date.getTime() + (expireDays*24*60*60*1000));
let expires = "expires=" + date.toUTCString();
// console.log(cookieName + "=" + cookieValue + ";" + expires + ";path=/");
document.cookie = cookieName + "=" + cookieValue + ";" + expires + ";path=/";
}
function getCookieVal(cookieName) {
// console.log(document.cookie)
let name = cookieName + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(';');
for(let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function checkState() {
let state = getCookieVal("state");
if (state != "" && state != null) {
return state;
} else {
deleteAllCookies(); // delete all cookies if the state cookie has been unset
setCookie("state", 1, 30);
return "1";
}
}
function getCookiesDict() {
var cookies = {};
if (document.cookie && document.cookie != '') {
var split = document.cookie.split(';');
for (var i = 0; i < split.length; i++) {
var name_value = split[i].split("=");
name_value[0] = name_value[0].replace(/^ /, '');
cookies[decodeURIComponent(name_value[0])] = decodeURIComponent(name_value[1]);
}
}
console.log(cookies);
return cookies;
}
function deleteCookie(cookieName){
document.cookie = cookieName + "=; expires=Sat, 01 Jan 2000 00:00:00 UTC; path=/;";
}
function deleteAllCookies(){
cookies = getCookiesDict();
for (var key in cookies) {
if (cookies.hasOwnProperty(key))
deleteCookie(key);
}
getCookiesDict();
}
function incrementCookie(cookieName, incrementAmount){
cookieVal = getCookieVal(cookieName);
if (!isNumber(incrementAmount)){
incrementAmount = parseFloat(incrementAmount);
if (isNaN(incrementAmount)){
console.log("incrementAmount is NaN")
return false;
}
}
if (cookieVal != null && cookieVal != ""){
let val = parseFloat(cookieVal);
if (!isNaN(val)){
setCookie(cookieName, val+incrementAmount, 30);
console.log(cookieName + " updated to " + (val+incrementAmount));
return true;
}
}
console.log("Cookie does not exist or does not have a numerical value");
return false;
}
function isNumber(value) {
return typeof value === 'number' && isFinite(value);
}
function getIntFromCookie(cookieName){
cookieVal = parseInt(getCookieVal(cookieName));
if (!isNaN(cookieVal))
return cookieVal;
return false;
}
function getFloatFromCookie(cookieName){
cookieVal = parseFloat(getCookieVal(cookieName));
if (!isNaN(cookieVal))
return cookieVal;
return false;
}