From b48f0adccb11619680a47cac5fa9c68f638bd489 Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Sun, 17 May 2020 13:15:41 -0400 Subject: [WebCards] Update from my local repos --- webcards/scripts/cookie.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 webcards/scripts/cookie.js (limited to 'webcards/scripts/cookie.js') diff --git a/webcards/scripts/cookie.js b/webcards/scripts/cookie.js new file mode 100644 index 0000000..2eb5977 --- /dev/null +++ b/webcards/scripts/cookie.js @@ -0,0 +1,39 @@ +function CookieManager() { +} + +CookieManager.prototype = { + getCookie: function(name){ + let cookies = document.cookie.split(";"); + for(let i in cookies) { + let cname = cookies[i].trim().split("=")[0]; + if(cname == name){ + return cookies[i].trim().slice(name.length + 1); + } + } + return ""; + }, + + setCookie: function(name, value, data={}) { + let extra = ""; + + for(let key in data) + { + extra += "; " + key + "=" + data[key]; + } + + document.cookie = name + "=" + value + extra; + }, + + setYearCookie: function(name, value) { + var date = new Date(Date.now()); + date.setFullYear(date.getFullYear() + 1); + this.setCookie(name, value, {expires: date.toUTCString()}); + }, + + removeCookie: function(name) { + var date = new Date(0); + this.setCookie(name, "", {expires: date.toUTCString()}); + } +}; + +var Cookies = new CookieManager(); \ No newline at end of file -- cgit v1.2.3