From edb4532cf76cdf76d5a1835b248bb4cb32c8d48e Mon Sep 17 00:00:00 2001 From: Deven Blake Date: Sun, 26 Sep 2021 22:25:53 -0400 Subject: [PATCH] rewrite cookies.js and free the code --- homepage/js/cookies.js | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/homepage/js/cookies.js b/homepage/js/cookies.js index 2cf5d08..2de6658 100644 --- a/homepage/js/cookies.js +++ b/homepage/js/cookies.js @@ -1,49 +1,38 @@ -/* cookies.js; Deven Blake 2021; Public Domain to extent allowed, see below */ +/* cookies.js; Deven Blake 2021; Public Domain */ window.setCookie = function(name, value){ var d; d = new Date(); + /* seconds, minutes, hours, days, years */ d.setTime(d.getTime() + 1000 * 60 * 60 * 24 * 365); /* ( == one year in milliseconds) */ + document.cookie = name + "=" + value + ";" + d.toUTCString() + ";path=/"; } -/* Public domain rewrite */ -/* window.getCookie = function(name){ var c; var i; + try{ c = decodeURIComponent(document.cookie); }catch(URIError){ console.log("Could not decode cookie URIComponent (cookies.js: getCookie: URIError)"); return ''; } + c = c.split(';'); + for(i = 0; i < c.length; ++i){ while(c[i].charAt(0) == ' ') c[i] = c[i].slice(1); + + /* check if the first bit + '=' matches name + '=' */ + /* the added '=' is so 'a' doesn't match 'ab=' */ if(c[i].slice(0, name.length + 1) == name + '=') - return c[i].substring(name.length, c[i].length); + /* return the associated value */ + return c[i].slice(name.length + 1, c[i].length); } return ''; } -*/ - -/* - this code snippet copied from - https://www.w3schools.com/js/js_cookies.asp - used under fair use; for copyright see https://www.w3schools.com/about/about_copyright.asp -*/ -window.getCookie = function(cname) { - var name = cname + "="; - var decodedCookie = decodeURIComponent(document.cookie); - var ca = decodedCookie.split(';'); - for(var i = 0; i