-
Notifications
You must be signed in to change notification settings - Fork 2
/
cookie.js
145 lines (125 loc) · 3.98 KB
/
cookie.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* Represents a cookie.
*/
class Cookie {
name = null;
value = null;
// how to order the rest?
// let's follow the Chrome DevTools
domain = null;
path = null;
expires = null;
maxAge = null;
secure = null;
samesite = null;
// constructor follows the above/mentioned order
constructor(name, value, domain, path, expiresOrMaxAge, secure, samesite) {
this.name = name || this.name;
this.value = value || this.value;
this.domain = domain || this.domain;
this.path = path || this.path;
if (expiresOrMaxAge) {
if (typeof expiresOrMaxAge == "number") {
this.maxAge = expiresOrMaxAge;
} else {
this.expires = expiresOrMaxAge;
}
}
this.secure = secure || this.secure;
this.samesite = samesite || this.samesite;
}
/**
* Returns a (document.cookie)-ready string representation.
*/
toString() {
if (!this.name) {
return "";
}
let pairs = [
[this.name, (this.value || "")],
];
if (this.domain) {
pairs.push(["domain", this.domain]);
}
if (this.path) {
pairs.push(["path", this.path]);
}
if (this.expires) {
pairs.push(["expires", this.expires]);
} else if (this.maxAge != null) {
pairs.push(["max-age", this.maxAge]);
}
if (this.secure) {
pairs.push(["secure", this.secure]);
}
if (this.samesite) {
pairs.push(["samesite", this.samesite]);
}
pairs = pairs.map(pair => {
return pair.join("=");
});
pairs = pairs.join("; ");
return pairs;
}
}
/**
* Cookie Manager.
*
* https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie
* https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies
* https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/cookies
*/
let Cookies = {
get : function getCookie(name) {
if (!name) {
return null;
}
let cookiePattern = new RegExp("(?:^| )" + encodeURIComponent(name) + "=([^;]+)");
var match = document.cookie.match(cookiePattern);
if (match) {
return decodeURIComponent(match[1]);
}
return null;
},
getAll : function getAllCookies() {
let table = {};
let cookies = document.cookie.split(";");
cookies.forEach(cookie => {
cookie = cookie.trim().split("=");
table[cookie[0]] = cookie[1];
});
return table;
},
// follows the argument order that's set in Cookie
set : function setCookie(name, value, domain, path, expiresOrMaxAge, secure, samesite) {
let cookie = new Cookie(name, value, domain, path, expiresOrMaxAge, secure, samesite);
document.cookie = cookie.toString();
},
remove : function removeCookie(name, domain, path) {
let cookie = new Cookie(name);
cookie.domain = domain || location.host;
if (path) {
cookie.path = path;
} else {
path = location.pathname.split("/");
// if the path does not end with a trailing slash
// the last segment seems to be treated as an item/file, that is, not part of the dir/path
path.pop();
path = path.join("/");
cookie.path = path;
}
cookie.maxAge = 0;
document.cookie = cookie.toString();
},
log : function logCookie(name) {
if (typeof console.table === "function") {
// console.table({name : cookies.get(name)});
console.log(Cookies.get(name));
}
},
logAll : function logAllCookies() {
if (typeof console.table === "function") {
console.table(Cookies.getAll());
}
},
};