tidy up comments and use of constant for storage key

This commit is contained in:
Maximilian Bethke 2021-02-01 15:19:26 +01:00 committed by GitHub
parent cb4924d5e6
commit f39ecb15bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 87 additions and 3 deletions

View File

@ -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)

82
js/cookie.js Normal file
View File

@ -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;
}
}