From f39ecb15bd865c5b32c7b942536f003037f297c3 Mon Sep 17 00:00:00 2001 From: Maximilian Bethke Date: Mon, 1 Feb 2021 15:19:26 +0100 Subject: [PATCH] tidy up comments and use of constant for storage key --- js/coinbin.js | 8 +++-- js/cookie.js | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 js/cookie.js diff --git a/js/coinbin.js b/js/coinbin.js index 2c95b9c..ab6f3d1 100644 --- a/js/coinbin.js +++ b/js/coinbin.js @@ -8,6 +8,8 @@ $(document).ready(function() { var wallet_timer = false; + var LOCALSTORAGE_COIN_KEY = "coinjs_coin_option" + $("#openBtn").click(function(){ var email = $("#openEmail").val().toLowerCase(); if(email.match(/[\s\w\d]+@[\s\w\d]+/g)){ @@ -1933,7 +1935,7 @@ $(document).ready(function() { $("#coinjs_coin").change(function(){ // set localStorage for use after page (re)load - localStorage.setItem("coinbin_coin_option", $(this).val()); + localStorage.setItem(LOCALSTORAGE_COIN_KEY, $(this).val()); optionSeleted = ($("option:selected",this).attr("rel")).split(";"); @@ -2287,10 +2289,10 @@ $(document).ready(function() { return true; }; - /* set network from cookie on page load */ + /* set network from localStorage on page load */ function setNetwork () { - var storageItem = localStorage.getItem("coinbin_coin_option"); + var storageItem = localStorage.getItem(LOCALSTORAGE_COIN_KEY); if(storageItem) { $("#coinjs_coin").val(storageItem) diff --git a/js/cookie.js b/js/cookie.js new file mode 100644 index 0000000..d8b6657 --- /dev/null +++ b/js/cookie.js @@ -0,0 +1,82 @@ +class Cookie { + constructor(name) { + this.name = name; + this.value; + + // read the cookies value (initializes value) + this.get(); + } + + get() { + var allCookies = document.cookie; + var cookie; + + // find out how much cookies are set to be able to pick the right one + if(!allCookies) + { + // the tray is empty. leave fields as initialized + return; + } + + if(allCookies.indexOf(";") > 0) + { + // many cookies found, pick one + cookie = this.pickCookie(allCookies); + } else { + // only one cookie found. Check if its the right one + if(this.validateCookie(allCookies)) + { + cookie = allCookies; + } + } + + // read contents of the picked cookie + if(cookie) { + this.value = this.readCookieContents(cookie); + } else { + console.log("Coudn't get cookie"); + } + } + + pickCookie(manyCookies) { + manyCookies.split(";").forEach((cookie, _) => { + if(this.validateCookie(cookie)) + { + return cookie; + } + }); + } + + validateCookie(cookie) { + if(cookie.indexOf("=") >= 0) + { + var cookieCredentials = cookie.split("="); + var name = cookieCredentials[0] + return name == this.name; + } else { + return false; + } + } + + readCookieContents(cookie) { + var cookieCredentials = cookie.split("="); + var value = cookieCredentials[1]; + return JSON.parse(value); + } + + setValue(valueToBeSet) { + // update internal record + this.value = valueToBeSet; + + // prepare value for storing + var valueJSON = JSON.stringify(valueToBeSet); + + // set cookie with expiry on browser close + document.cookie = this.name+"="+valueJSON; + } + + getValue() { + this.get(); + return this.value; + } +} \ No newline at end of file