StorageAvailable() function from MDN checks

This commit is contained in:
ReznoRMichael 2021-02-16 00:23:00 +01:00
parent 12ca7da27a
commit 7b333ba47b
2 changed files with 45 additions and 36 deletions

File diff suppressed because one or more lines are too long

View file

@ -1276,18 +1276,11 @@ function InitialHTMLPopulate(divIdObj) {
AppendHTML(divIdObj.achievements, FLEUR_DIVIDE);
// Check local storage first to set proper checkbox state before the below functions start (default is always unchecked)
try {
if (StorageAvailable('localStorage')) {
if (localStorage.getItem("hkCheckboxHints") === "checked") document.getElementById("checkbox-hints").checked = true;
}
catch (error) {
console.log(error);
}
try {
if (localStorage.getItem("hkCheckboxSpoilers") === "checked") document.getElementById("checkbox-spoilers").checked = true;
}
catch (error) {
console.log(error);
}
// if (localStorage.getItem("hkCheckboxHints") === "checked") document.getElementById("checkbox-hints").checked = true;
// if (localStorage.getItem("hkCheckboxSpoilers") === "checked") document.getElementById("checkbox-spoilers").checked = true;
@ -1310,7 +1303,9 @@ function CheckboxHintsToggle(param = "none") {
checkboxId.checked = false;
// remember this choice for subsequent page visits and browser restarts
localStorage.setItem("hkCheckboxHints", "unchecked");
if (StorageAvailable('localStorage')) {
localStorage.setItem("hkCheckboxHints", "unchecked");
}
break;
case "show":
document.getElementById("hk-hints").classList.remove("hidden");
@ -1318,7 +1313,9 @@ function CheckboxHintsToggle(param = "none") {
checkboxId.checked = true;
// remember this choice for subsequent page visits and browser restarts
localStorage.setItem("hkCheckboxHints", "checked");
if (StorageAvailable('localStorage')) {
localStorage.setItem("hkCheckboxHints", "checked");
}
break;
default:
// This runs when the checkbox is not checked
@ -1327,12 +1324,9 @@ function CheckboxHintsToggle(param = "none") {
checkboxId.value = "hints-off";
// remember this choice for subsequent page visits and browser restarts
try {
if (StorageAvailable('localStorage')) {
localStorage.setItem("hkCheckboxHints", "unchecked");
}
catch (error) {
console.log(error);
}
}
// This runs when the checkbox is checked
else {
@ -1340,12 +1334,9 @@ function CheckboxHintsToggle(param = "none") {
checkboxId.value = "hints-on";
// remember this choice for subsequent page visits and browser restarts
try {
if (StorageAvailable('localStorage')) {
localStorage.setItem("hkCheckboxHints", "checked");
}
catch (error) {
console.log(error);
}
}
}
}
@ -1368,12 +1359,9 @@ function CheckboxSpoilersToggle(param = "none") {
checkboxId.checked = false;
// remember this choice for subsequent page visits and browser restarts
try {
if (StorageAvailable('localStorage')) {
localStorage.setItem("hkCheckboxSpoilers", "unchecked");
}
catch (error) {
console.log(error);
}
break;
case "show":
for (let i = 0; i < length; i++) {
@ -1383,12 +1371,9 @@ function CheckboxSpoilersToggle(param = "none") {
checkboxId.checked = true;
// remember this choice for subsequent page visits and browser restarts
try {
if (StorageAvailable('localStorage')) {
localStorage.setItem("hkCheckboxSpoilers", "checked");
}
catch (error) {
console.log(error);
}
break;
default:
// This runs when the checkbox is not checked
@ -1399,12 +1384,9 @@ function CheckboxSpoilersToggle(param = "none") {
checkboxId.value = "spoilers-off";
// remember this choice for subsequent page visits and browser restarts
try {
if (StorageAvailable('localStorage')) {
localStorage.setItem("hkCheckboxSpoilers", "unchecked");
}
catch (error) {
console.log(error);
}
break;
}
// This runs when the checkbox is checked
@ -1415,12 +1397,9 @@ function CheckboxSpoilersToggle(param = "none") {
checkboxId.value = "spoilers-on";
// remember this choice for subsequent page visits and browser restarts
try {
if (StorageAvailable('localStorage')) {
localStorage.setItem("hkCheckboxSpoilers", "checked");
}
catch (error) {
console.log(error);
}
}
}
}
@ -1486,6 +1465,36 @@ function TranslateMapName(mapCode, dictionary = MAP) {
return translation;
}
/**
* Detects whether Storage is both supported and available.
* MDN WebDocs https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API#feature-detecting_localstorage
* @param {Storage} type type of storage. Ex. "localStorage" or "sessionStorage"
* @returns {Boolean}
*/
function StorageAvailable(type) {
var storage;
try {
storage = window[type];
var x = '__storage_test__';
storage.setItem(x, x);
storage.removeItem(x);
return true;
} catch (e) {
return e instanceof DOMException && (
// everything except Firefox
e.code === 22 ||
// Firefox
e.code === 1014 ||
// test name field too, because code might not be present
// everything except Firefox
e.name === 'QuotaExceededError' ||
// Firefox
e.name === 'NS_ERROR_DOM_QUOTA_REACHED') &&
// acknowledge QuotaExceededError only if there's something already stored
(storage && storage.length !== 0);
}
}
// Make functions global so they can be used on click and change events (for Webpack)
// window.InitialHTMLPopulate = InitialHTMLPopulate;