From 745f32f0e000d0470761b3101d706661b78f81eb Mon Sep 17 00:00:00 2001 From: Maximilian Bethke Date: Fri, 29 Jan 2021 16:59:59 +0100 Subject: [PATCH 1/8] Added cookie class to be able to store information beyond sessions --- index.html | 46 ++++++++++++++++++++++++---------------------- js/coinbin.js | 25 +++++++++++++++---------- js/cookie.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 32 deletions(-) create mode 100644 js/cookie.js diff --git a/index.html b/index.html index 691dbd5b..fd52bdaa 100644 --- a/index.html +++ b/index.html @@ -37,6 +37,8 @@ + + @@ -54,28 +56,28 @@ diff --git a/js/coinbin.js b/js/coinbin.js index afc98f17..1e56054b 100644 --- a/js/coinbin.js +++ b/js/coinbin.js @@ -1933,32 +1933,37 @@ $(document).ready(function() { $("#coinjs_coin").change(function(){ - var o = ($("option:selected",this).attr("rel")).split(";"); + var optionSeleted = ($("option:selected",this).attr("rel")).split(";"); + + let cookieSelectedCoin = new Cookie("coinbin_coin"); + cookieSelectedCoin.setCookie(optionSeleted) + + console.log(cookieSelectedCoin.cookieValue); // deal with broadcasting settings - if(o[5]=="false"){ + if(optionSeleted[5]=="false"){ $("#coinjs_broadcast, #rawTransaction, #rawSubmitBtn, #openBtn").attr('disabled',true); $("#coinjs_broadcast").val("coinb.in"); } else { - $("#coinjs_broadcast").val(o[5]); + $("#coinjs_broadcast").val(optionSeleted[5]); $("#coinjs_broadcast, #rawTransaction, #rawSubmitBtn, #openBtn").attr('disabled',false); } // deal with unspent output settings - if(o[6]=="false"){ + if(optionSeleted[6]=="false"){ $("#coinjs_utxo, #redeemFrom, #redeemFromBtn, #openBtn, .qrcodeScanner").attr('disabled',true); $("#coinjs_utxo").val("coinb.in"); } else { - $("#coinjs_utxo").val(o[6]); + $("#coinjs_utxo").val(optionSeleted[6]); $("#coinjs_utxo, #redeemFrom, #redeemFromBtn, #openBtn, .qrcodeScanner").attr('disabled',false); } // deal with the reset - $("#coinjs_pub").val(o[0]); - $("#coinjs_priv").val(o[1]); - $("#coinjs_multisig").val(o[2]); - $("#coinjs_hdpub").val(o[3]); - $("#coinjs_hdprv").val(o[4]); + $("#coinjs_pub").val(optionSeleted[0]); + $("#coinjs_priv").val(optionSeleted[1]); + $("#coinjs_multisig").val(optionSeleted[2]); + $("#coinjs_hdpub").val(optionSeleted[3]); + $("#coinjs_hdprv").val(optionSeleted[4]); // hide/show custom screen if($("option:selected",this).val()=="custom"){ diff --git a/js/cookie.js b/js/cookie.js new file mode 100644 index 00000000..1e989ab1 --- /dev/null +++ b/js/cookie.js @@ -0,0 +1,43 @@ +class Cookie { + constructor(name) { + this.cookieName = name; + + // read the cookies value (initializes cookieValue) + this.readCookie(); + } + + readCookie() { + var allCookies = document.cookie; + + // find the cookie that is currently being looked at. + allCookies.split(";").forEach((_, cookie) => { + var currentCookieName, currentCookieValue = cookie.split("="); + + if(currentCookieName = this.cookieName) + { + // set cookieValue to the value of this cookie + this.cookieValue = currentCookieValue; + break; + } + }); + + // default to NULL if cookie was not found + this.cookieValue = NULL; + } + + setCookie(cookieValue) { + // update internal record + this.cookieValue = currentCookieValue; + + // prepare value for storing + cookieValueJSON = JSON.stringify(cookieValue); + + // set cookie with expiry on browser close + document.cookie = this.cookieName+"="+cookieValueJSON; + } + + getCookieValue() { + this.readCookie(); + return this.cookieValue; + } +} \ No newline at end of file From 23dd62cfd07978e445597484bc7b9622a04aad72 Mon Sep 17 00:00:00 2001 From: Maximilian Bethke Date: Mon, 1 Feb 2021 11:58:07 +0100 Subject: [PATCH 2/8] added cookie class for network to be saved --- index.html | 4 +-- js/coinbin.js | 22 ++++++++++---- js/cookie.js | 84 ++++++++++++++++++++++++++++++++++++++------------- 3 files changed, 81 insertions(+), 29 deletions(-) diff --git a/index.html b/index.html index fd52bdaa..5299e762 100644 --- a/index.html +++ b/index.html @@ -75,8 +75,8 @@
  • Wallet
  • About
  • - - +
  • Settings
  • +
  • Fees
  • diff --git a/js/coinbin.js b/js/coinbin.js index 1e56054b..a326d7dd 100644 --- a/js/coinbin.js +++ b/js/coinbin.js @@ -1932,13 +1932,11 @@ $(document).ready(function() { }); $("#coinjs_coin").change(function(){ + // set cookie for use after page (re)load + let cookie = new Cookie("coinbin_coin_option"); + cookie.setValue($(this).val()); - var optionSeleted = ($("option:selected",this).attr("rel")).split(";"); - - let cookieSelectedCoin = new Cookie("coinbin_coin"); - cookieSelectedCoin.setCookie(optionSeleted) - - console.log(cookieSelectedCoin.cookieValue); + optionSeleted = ($("option:selected",this).attr("rel")).split(";"); // deal with broadcasting settings if(optionSeleted[5]=="false"){ @@ -2290,4 +2288,16 @@ $(document).ready(function() { return true; }; + /* set network from cookie on page load */ + + function setNetwork () { + let cookie = new Cookie("coinbin_coin_option"); + if(val = cookie.getValue()) + { + $("#coinjs_coin").val(val) + $("#coinjs_coin").trigger("change") + } + } + setNetwork() + }); diff --git a/js/cookie.js b/js/cookie.js index 1e989ab1..4b9867bc 100644 --- a/js/cookie.js +++ b/js/cookie.js @@ -1,43 +1,85 @@ class Cookie { + #name; + #value; + constructor(name) { - this.cookieName = name; - - // read the cookies value (initializes cookieValue) - this.readCookie(); + this.name = name; + this.value; + + // read the cookies value (initializes value) + this.get(); } - readCookie() { + 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; + } + } - // find the cookie that is currently being looked at. - allCookies.split(";").forEach((_, cookie) => { - var currentCookieName, currentCookieValue = cookie.split("="); + // read contents of the picked cookie + if(cookie) { + this.value = this.readCookieContents(cookie); + } else { + console.log("Coudn't get cookie"); + } + } - if(currentCookieName = this.cookieName) + pickCookie(manyCookies) { + manyCookies.split(";").forEach((cookie, _) => { + if(this.validateCookie(cookie)) { - // set cookieValue to the value of this cookie - this.cookieValue = currentCookieValue; - break; + return cookie; } }); + } + + validateCookie(cookie) { + if(cookie.indexOf("=") >= 0) + { + var cookieCredentials = cookie.split("="); + var name = cookieCredentials[0] + return name == this.name; + } else { + return false; + } + } - // default to NULL if cookie was not found - this.cookieValue = NULL; + readCookieContents(cookie) { + var cookieCredentials = cookie.split("="); + var value = cookieCredentials[1]; + return JSON.parse(value); } - setCookie(cookieValue) { + setValue(valueToBeSet) { // update internal record - this.cookieValue = currentCookieValue; + this.value = valueToBeSet; // prepare value for storing - cookieValueJSON = JSON.stringify(cookieValue); + var valueJSON = JSON.stringify(valueToBeSet); // set cookie with expiry on browser close - document.cookie = this.cookieName+"="+cookieValueJSON; + document.cookie = this.name+"="+valueJSON; } - getCookieValue() { - this.readCookie(); - return this.cookieValue; + getValue() { + this.get(); + return this.value; } } \ No newline at end of file From cd5c681ef82e3557fdbe380e64b2675824017e4a Mon Sep 17 00:00:00 2001 From: Maximilian Bethke Date: Mon, 1 Feb 2021 12:10:39 +0100 Subject: [PATCH 3/8] Fixed indentation in navbar --- index.html | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/index.html b/index.html index 5299e762..1b9c67b4 100644 --- a/index.html +++ b/index.html @@ -56,28 +56,28 @@ From 9a9bb4d6d0c2e79930216e6a28ddd1b2badd166b Mon Sep 17 00:00:00 2001 From: Maximilian Bethke Date: Mon, 1 Feb 2021 14:50:41 +0100 Subject: [PATCH 4/8] removed ESNext private fields Forgot that they we're still there... --- js/cookie.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/js/cookie.js b/js/cookie.js index 4b9867bc..d8b66579 100644 --- a/js/cookie.js +++ b/js/cookie.js @@ -1,7 +1,4 @@ class Cookie { - #name; - #value; - constructor(name) { this.name = name; this.value; From 7cbb8e768d7d033e810627b01e069c4076ce87e3 Mon Sep 17 00:00:00 2001 From: Maximilian Bethke Date: Mon, 1 Feb 2021 15:10:53 +0100 Subject: [PATCH 5/8] Switched from cookies to localStorage Using localStorage makes much more sense in this case. --- index.html | 2 -- js/coinbin.js | 11 +++++------ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/index.html b/index.html index 1b9c67b4..c1426553 100644 --- a/index.html +++ b/index.html @@ -37,8 +37,6 @@ - - diff --git a/js/coinbin.js b/js/coinbin.js index a326d7dd..2c95b9cd 100644 --- a/js/coinbin.js +++ b/js/coinbin.js @@ -1932,9 +1932,8 @@ $(document).ready(function() { }); $("#coinjs_coin").change(function(){ - // set cookie for use after page (re)load - let cookie = new Cookie("coinbin_coin_option"); - cookie.setValue($(this).val()); + // set localStorage for use after page (re)load + localStorage.setItem("coinbin_coin_option", $(this).val()); optionSeleted = ($("option:selected",this).attr("rel")).split(";"); @@ -2291,10 +2290,10 @@ $(document).ready(function() { /* set network from cookie on page load */ function setNetwork () { - let cookie = new Cookie("coinbin_coin_option"); - if(val = cookie.getValue()) + var storageItem = localStorage.getItem("coinbin_coin_option"); + if(storageItem) { - $("#coinjs_coin").val(val) + $("#coinjs_coin").val(storageItem) $("#coinjs_coin").trigger("change") } } From cb4924d5e62ec4172806ea5b96626c2e9f5173ec Mon Sep 17 00:00:00 2001 From: Maximilian Bethke Date: Mon, 1 Feb 2021 15:11:20 +0100 Subject: [PATCH 6/8] Delete cookie.js not used anymore --- js/cookie.js | 82 ---------------------------------------------------- 1 file changed, 82 deletions(-) delete mode 100644 js/cookie.js diff --git a/js/cookie.js b/js/cookie.js deleted file mode 100644 index d8b66579..00000000 --- a/js/cookie.js +++ /dev/null @@ -1,82 +0,0 @@ -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 From f39ecb15bd865c5b32c7b942536f003037f297c3 Mon Sep 17 00:00:00 2001 From: Maximilian Bethke Date: Mon, 1 Feb 2021 15:19:26 +0100 Subject: [PATCH 7/8] 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 2c95b9cd..ab6f3d14 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 00000000..d8b66579 --- /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 From 8151b3eda4c77571bbe5df31eafab62b2a18873d Mon Sep 17 00:00:00 2001 From: Maximilian Bethke Date: Mon, 1 Feb 2021 15:21:25 +0100 Subject: [PATCH 8/8] Delete cookie.js (again) --- js/cookie.js | 82 ---------------------------------------------------- 1 file changed, 82 deletions(-) delete mode 100644 js/cookie.js diff --git a/js/cookie.js b/js/cookie.js deleted file mode 100644 index d8b66579..00000000 --- a/js/cookie.js +++ /dev/null @@ -1,82 +0,0 @@ -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