multiple libraries, add request and cookie
This commit is contained in:
34
lib/utility/cookie.js
Normal file
34
lib/utility/cookie.js
Normal file
@ -0,0 +1,34 @@
|
||||
function setCookie(name, value, expireDays) {
|
||||
let extra = `; domain=${location.host}; path=/`;
|
||||
if (expireDays != null) {
|
||||
const d = new Date();
|
||||
d.setTime(d.getTime() + (expireDays * 24 * 60 * 60 * 1000));
|
||||
extra += `; expires=${d.toGMTString()}`;
|
||||
}
|
||||
if (/^(https|wss):$/.test(location.protocol)) {
|
||||
extra += '; secure';
|
||||
}
|
||||
document.cookie = `${name}=${encodeURIComponent(value)}${extra}`;
|
||||
}
|
||||
|
||||
function getCookie(name) {
|
||||
name += '=';
|
||||
const cookies = document.cookie.split(';');
|
||||
for (let cookie of cookies) {
|
||||
cookie = cookie.trim();
|
||||
if (cookie.indexOf(name) === 0) {
|
||||
return decodeURIComponent(cookie.substring(name.length));
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function deleteCookie(name) {
|
||||
setCookie(name, '', -1);
|
||||
}
|
||||
|
||||
export {
|
||||
getCookie,
|
||||
setCookie,
|
||||
deleteCookie
|
||||
}
|
Reference in New Issue
Block a user