Skip to content

Commit

Permalink
Don't check for console or localstorage to exist; use window.localSto…
Browse files Browse the repository at this point in the history
…rage
  • Loading branch information
bluehexagons committed Nov 21, 2023
1 parent 7ea28f3 commit e87a3d8
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions html/foodguide.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,8 @@ import {makeLinkable, isStat, isBestStat, makeImage, pl} from './utils.js';
activePage = elements['simulator'];

try {
if (window.localStorage && localStorage.foodGuideState) {
const storage = JSON.parse(localStorage.foodGuideState);
if (window.localStorage.foodGuideState) {
const storage = JSON.parse(window.localStorage.foodGuideState);

if (storage.activeTab && tabs[storage.activeTab]) {
activeTab = tabs[storage.activeTab];
Expand All @@ -543,7 +543,10 @@ import {makeLinkable, isStat, isBestStat, makeImage, pl} from './utils.js';
}
}
} catch(err) {
console && console.warn('Unable to access localStorage', err);
console.warn('Unable to access localStorage', err);
try {
window.localStorage.removeItem('foodGuideState')
} catch {}
}

activeTab.className = 'selected';
Expand All @@ -553,18 +556,16 @@ import {makeLinkable, isStat, isBestStat, makeImage, pl} from './utils.js';
let obj;

try {
if (window.localStorage) {
if (!localStorage.foodGuideState) {
localStorage.foodGuideState = '{}';
}

obj = JSON.parse(localStorage.foodGuideState);
obj.activeTab = activeTab.dataset.tab;
obj.modeMask = modeMask;
localStorage.foodGuideState = JSON.stringify(obj);
if (!window.localStorage.foodGuideState) {
window.localStorage.foodGuideState = '{}';
}

obj = JSON.parse(window.localStorage.foodGuideState);
obj.activeTab = activeTab.dataset.tab;
obj.modeMask = modeMask;
window.localStorage.foodGuideState = JSON.stringify(obj);
} catch(err) {
console && console.warn('Unable to access localStorage', err);
console.warn('Unable to access localStorage', err);
}
});
})();
Expand Down Expand Up @@ -1291,15 +1292,17 @@ import {makeLinkable, isStat, isBestStat, makeImage, pl} from './utils.js';
const item = food[id] || recipes[id] || null;

if (!id) {
console && console.warn('ID not set');
console.warn('ID not set');
return -1;
}

if (limited) {
for (let i = 0; i < slots.length; i++) {
if (getSlot(slots[i]) === null) {
setSlot(slots[i], item);
loaded && updateRecipes();
if (loaded) {
updateRecipes();
}

return i;
}
Expand All @@ -1314,7 +1317,9 @@ import {makeLinkable, isStat, isBestStat, makeImage, pl} from './utils.js';
setSlot(i, item);
i.addEventListener('click', removeSlot, false);
parent.appendChild(i);
loaded && updateRecipes();
if (loaded) {
updateRecipes();
}
}

return 1;
Expand Down Expand Up @@ -1531,8 +1536,8 @@ import {makeLinkable, isStat, isBestStat, makeImage, pl} from './utils.js';
}

try {
if (window.localStorage && localStorage.foodGuideState) {
state = JSON.parse(localStorage.foodGuideState).pickers;
if (window.localStorage.foodGuideState) {
state = JSON.parse(window.localStorage.foodGuideState).pickers;

if (state && state[index]) {
state[index].forEach(id => {
Expand All @@ -1543,7 +1548,7 @@ import {makeLinkable, isStat, isBestStat, makeImage, pl} from './utils.js';
}
}
} catch (err) {
console && console.warn('Unable to access localStorage', err);
console.warn('Unable to access localStorage', err);
}

loaded = true;
Expand Down Expand Up @@ -1900,13 +1905,10 @@ import {makeLinkable, isStat, isBestStat, makeImage, pl} from './utils.js';
updateRecipes();

window.addEventListener('beforeunload', () => {
if (!window.localStorage) {
return
}
if (!localStorage.foodGuideState) {
localStorage.foodGuideState = '{}';
if (!window.localStorage.foodGuideState) {
window.localStorage.foodGuideState = '{}';
}
const obj = JSON.parse(localStorage.foodGuideState);
const obj = JSON.parse(window.localStorage.foodGuideState);
if (!obj.pickers) {
obj.pickers = [];
}
Expand All @@ -1919,7 +1921,7 @@ import {makeLinkable, isStat, isBestStat, makeImage, pl} from './utils.js';
} else {
obj.pickers[index] = slots;
}
localStorage.foodGuideState = JSON.stringify(obj);
window.localStorage.foodGuideState = JSON.stringify(obj);
});

modeRefreshers.push(refreshPicker);
Expand Down

0 comments on commit e87a3d8

Please sign in to comment.