|
const myCookieApi = { |
|
|
|
existsValue: function (cookieName) { |
|
|
|
let value = myCookieApi.get(cookieName) |
|
|
|
if ( value === undefined || value === null || value == [] || value == {} || value === '' ) return false |
|
|
|
return true |
|
}, |
|
existsAny: function () { |
|
|
|
let any = false |
|
|
|
for (let cname of memorization.allCookiesNames) |
|
any = myCookieApi.existsValue(cname) |
|
|
|
return any |
|
}, |
|
get: function (cookieName) { |
|
|
|
let parts = `; ${document.cookie}` |
|
parts = parts.split(`; ${cookieName}=`) |
|
if (parts.length === 2) return parts.pop().split(';').shift() |
|
|
|
}, |
|
getAll: function () { |
|
|
|
let arr = [] |
|
|
|
for (let cname of memorization.allCookiesNames) |
|
arr.push({ |
|
name: cname, |
|
value: myCookieApi.get(cname) |
|
}) |
|
|
|
return arr |
|
}, |
|
setAll: function (allNewCookiesObjs, lifespanInMinutes) { |
|
|
|
let allCookiesNames = [] |
|
for (let objName in allNewCookiesObjs) |
|
allCookiesNames = objName.value |
|
|
|
// console.log(`Cookies before: \r\n${document.cookie}`) |
|
myCookieApi.remove(allCookiesNames) |
|
|
|
// Calculate date of expiration |
|
let expDateTime = new Date(), |
|
time = expDateTime.getTime() |
|
expDateTime = new Date(time + 60000 * lifespanInMinutes) |
|
expDateTime = expDateTime.toGMTString() |
|
|
|
// Prepares new cookie |
|
let str = `` |
|
for (let c of allNewCookiesObjs) str += `${c.name}=${c.value}; ` |
|
|
|
// Writes new cookie |
|
document.cookie = `${str} expires=${expDateTime}; domain=${document.domain}; path=/` |
|
|
|
// console.clear() |
|
str = `` |
|
for (let cname of memorization.allCookiesNames) |
|
str += myCookieApi.get(cname) + `\r\n` |
|
|
|
console.log(`Memorized user settings: \r\n${str}`) |
|
|
|
return str |
|
}, |
|
set: function (name, newValue, lifespanInMinutes) { |
|
|
|
// Updates data |
|
newSet = myCookieApi.getAll() |
|
newSet.find(c => c.name == name).value = newValue |
|
|
|
// Updates cookies with data |
|
myCookieApi.setAll(newSet, lifespanInMinutes) |
|
|
|
// console.log(`Cookie: ${myCookieApi.get(name)}`) |
|
return myCookieApi.existsValue(name) |
|
}, |
|
remove: function (...cookieNames) { |
|
|
|
let str = '' |
|
for (let cname of arguments) str += `${cname}=; ` |
|
document.cookie = `${str} expires=Thu, 01 Jan 1970 00:00:00 GMT; domain=${document.domain}; path=/` |
|
|
|
}, |
|
getExpirationDate: function() { |
|
|
|
return myCookieApi.get('expires') |
|
}, |
|
setExpirationDate: function(allCookiesNames, lifespanInMinutes) { |
|
|
|
let expDateTime = new Date() |
|
|
|
// Zero equals delete all cookies |
|
if (lifespanInMinutes == 0) { |
|
|
|
if (!confirm('You are about to cancel all of the changes that you made. Continue?')) return false |
|
|
|
myCookieApi.remove(allCookiesNames) |
|
} |
|
|
|
// Calculates new delay |
|
let time = expDateTime.getTime() |
|
expDateTime = new Date(time + 60000 * lifespanInMinutes) |
|
expDateTime = expDateTime.toGMTString() |
|
|
|
// Updates all cookies with new delay |
|
myCookieApi.setAll( myCookieApi.getAll(), expDateTime ) |
|
} |
|
} |