diff --git a/index.html b/index.html index 691dbd5..5299e76 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 afc98f1..a326d7d 100644 --- a/js/coinbin.js +++ b/js/coinbin.js @@ -1932,33 +1932,36 @@ $(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 o = ($("option:selected",this).attr("rel")).split(";"); + optionSeleted = ($("option:selected",this).attr("rel")).split(";"); // 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"){ @@ -2285,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 new file mode 100644 index 0000000..4b9867b --- /dev/null +++ b/js/cookie.js @@ -0,0 +1,85 @@ +class Cookie { + #name; + #value; + + 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