From 1f8230e232b69427c7b924f07f59f0eaf3318bba Mon Sep 17 00:00:00 2001 From: Khafra Date: Fri, 27 Sep 2024 13:56:46 -0400 Subject: [PATCH] test --- src/EventListeners.ts | 7 +++--- src/ImportExport.ts | 54 +++++++++++++++++++++++++++++++++++++------ src/Synergism.ts | 12 +++++----- src/Themes.ts | 7 +++--- src/i18n.ts | 10 +++----- 5 files changed, 64 insertions(+), 26 deletions(-) diff --git a/src/EventListeners.ts b/src/EventListeners.ts index 0c7634fd0..1945d5f31 100644 --- a/src/EventListeners.ts +++ b/src/EventListeners.ts @@ -53,6 +53,7 @@ import { promocodesPrompt, reloadDeleteGame, resetGame, + safeLocalStorage, updateSaveString } from './ImportExport' import { buyPlatonicUpgrades, createPlatonicDescription } from './Platonic' @@ -1108,7 +1109,7 @@ TODO: Fix this entire tab it's utter shit const element = event.target as HTMLInputElement if (!element.checked) { - localStorage.removeItem('copyToClipboard') + safeLocalStorage.removeItem('copyToClipboard') event.stopPropagation() return } @@ -1119,9 +1120,9 @@ TODO: Fix this entire tab it's utter shit if (confirmed) { element.checked = !element.checked - localStorage.setItem('copyToClipboard', '') + safeLocalStorage.setItem('copyToClipboard', '') } else { - localStorage.removeItem('copyToClipboard') + safeLocalStorage.removeItem('copyToClipboard') } }) diff --git a/src/ImportExport.ts b/src/ImportExport.ts index 3f8fa7366..36e9c1be5 100644 --- a/src/ImportExport.ts +++ b/src/ImportExport.ts @@ -355,7 +355,7 @@ export const importSynergism = async (input: string | null, reset = false) => { saveCheck.canSave = false await setSave(saveString) - localStorage.setItem('saveScumIsCheating', Date.now().toString()) + safeLocalStorage.setItem('saveScumIsCheating', Date.now().toString()) await reloadShit(reset) saveCheck.canSave = true @@ -846,11 +846,11 @@ export const promocodes = async (input: string | null, amount?: number) => { } else if (input === 'gamble') { if ( typeof player.skillCode === 'number' - || typeof localStorage.getItem('saveScumIsCheating') === 'string' + || typeof safeLocalStorage.getItem('saveScumIsCheating') === 'string' ) { if ( (Date.now() - player.skillCode!) / 1000 < 3600 - || (Date.now() - Number(localStorage.getItem('saveScumIsCheating'))) + || (Date.now() - Number(safeLocalStorage.getItem('saveScumIsCheating'))) / 1000 < 3600 ) { @@ -884,7 +884,7 @@ export const promocodes = async (input: string | null, amount?: number) => { )) } - localStorage.setItem('saveScumIsCheating', Date.now().toString()) + safeLocalStorage.setItem('saveScumIsCheating', Date.now().toString()) const dice = (window.crypto.getRandomValues(new Uint8Array(1))[0] % 6) + 1 // [1, 6] if (dice === 1) { @@ -1230,11 +1230,11 @@ const dailyCodeReward = () => { } export const handleLastModified = (lastModified: number) => { - const localStorageFirstPlayed = localStorage.getItem('firstPlayed') + const localStorageFirstPlayed = safeLocalStorage.getItem('firstPlayed') const lastModifiedDate = new Date(lastModified) if (localStorageFirstPlayed === null) { - localStorage.setItem('firstPlayed', lastModifiedDate.toISOString()) + safeLocalStorage.setItem('firstPlayed', lastModifiedDate.toISOString()) return } @@ -1245,7 +1245,7 @@ export const handleLastModified = (lastModified: number) => { // for the new file, set the oldest date to the last modified. if (localFirstPlayedDate.getTime() > lastModifiedDate.getTime()) { player.firstPlayed = lastModifiedDate.toISOString() - localStorage.setItem('firstPlayed', player.firstPlayed) + safeLocalStorage.setItem('firstPlayed', player.firstPlayed) } } @@ -1270,3 +1270,43 @@ export const setSave = async (saveString: string) => { Notification(`Error saving: ${e}`, 2000) } } + +export const safeLocalStorage = { + /** + * Same as localStorage.setItem, but errors are swallowed + */ + setItem (key: string, value: string) { + try { + localStorage.setItem(key, value) + } catch {} + }, + + /** + * Same as localStorage.removeItem, but errors are swallowed + */ + removeItem (key: string) { + try { + localStorage.removeItem(key) + } catch {} + }, + + /** + * Same as localStorage.getItem, but errors are swallowed + */ + getItem (key: string) { + try { + return localStorage.getItem(key) + } catch { + return null + } + }, + + /** + * Same as localStorage.clear, but errors are swallowed + */ + clear () { + try { + localStorage.clear() + } catch {} + } +} diff --git a/src/Synergism.ts b/src/Synergism.ts index a38e2028c..654033845 100644 --- a/src/Synergism.ts +++ b/src/Synergism.ts @@ -174,7 +174,7 @@ import { changeSubTab, changeTab, Tabs } from './Tabs' import { settingAnnotation, toggleIconSet, toggleTheme } from './Themes' import { clearTimeout, clearTimers, setInterval, setTimeout } from './Timers' import type { PlayerSave } from './types/LegacySynergism' -import { getSaveString, setSave } from './ImportExport' +import { getSaveString, safeLocalStorage, setSave } from './ImportExport' export const player: Player = { firstPlayed: new Date().toISOString(), @@ -6206,7 +6206,7 @@ export const reloadShit = async (reset = false) => { return Alert(i18next.t('save.loadFailed')) } - localStorage.clear() + safeLocalStorage.clear() await setSave(saveString) await Alert(i18next.t('main.transferredFromLZ')) } @@ -6274,9 +6274,9 @@ export const reloadShit = async (reset = false) => { setInterval(cacheReinitialize, 15000) - if (localStorage.getItem('pleaseStar') === null) { + if (safeLocalStorage.getItem('pleaseStar') === null) { void Alert(i18next.t('main.starRepo')) - localStorage.setItem('pleaseStar', '') + safeLocalStorage.setItem('pleaseStar', '') } // All versions of Chrome and Firefox supported by the game have this API, @@ -6299,7 +6299,7 @@ export const reloadShit = async (reset = false) => { } const saveType = DOMCacheGetOrSet('saveType') as HTMLInputElement - saveType.checked = localStorage.getItem('copyToClipboard') !== null + saveType.checked = safeLocalStorage.getItem('copyToClipboard') !== null } function playerNeedsReminderToExport () { @@ -6346,7 +6346,7 @@ window.addEventListener('load', async () => { corruptionButtonsAdd() corruptionLoadoutTableCreate() - handleLogin().catch(console.error) + handleLogin().catch((e) => console.error('caught error', e)) }) window.addEventListener('unload', () => { diff --git a/src/Themes.ts b/src/Themes.ts index eedc049eb..4c5481813 100644 --- a/src/Themes.ts +++ b/src/Themes.ts @@ -1,5 +1,6 @@ import i18next from 'i18next' import { DOMCacheGetOrSet } from './Cache/DOM' +import { safeLocalStorage } from './ImportExport' import { player } from './Synergism' export const toggleTheme = (initial = false, themeNumber = 1, change = false) => { @@ -7,11 +8,11 @@ export const toggleTheme = (initial = false, themeNumber = 1, change = false) => const body = document.body if (change) { - localStorage.setItem('theme', `${themeNumber}`) + safeLocalStorage.setItem('theme', `${themeNumber}`) body.style.setProperty('--transition', '750ms') body.style.setProperty('--transition-extra', '600ms') } else { - themeNumber = Number(localStorage.getItem('theme') ?? 1) + themeNumber = Number(safeLocalStorage.getItem('theme') ?? 1) } /* Full reset for easy out of order change */ @@ -107,7 +108,7 @@ export const toggleTheme = (initial = false, themeNumber = 1, change = false) => } if (themeNumber === 1) { - localStorage.removeItem('theme') + safeLocalStorage.removeItem('theme') themeButton.textContent = 'Dark Mode' } else if (themeNumber === 2) { // 'Darker Mode' body.style.setProperty('--header-color', 'black') diff --git a/src/i18n.ts b/src/i18n.ts index e231b22b4..4f0f3ff7d 100644 --- a/src/i18n.ts +++ b/src/i18n.ts @@ -1,6 +1,7 @@ import i18next, { type Resource } from 'i18next' import { DOMCacheGetOrSet } from './Cache/DOM' import { prod } from './Config' +import { safeLocalStorage } from './ImportExport' import ColorTextPlugin from './Plugins/ColorText' import { Confirm } from './UpdateHTML' @@ -21,12 +22,7 @@ const languageCache = new Map() export const init = async (): Promise => { const resources: Record = {} - let language: string - try { - language = localStorage.getItem('language') ?? 'en' - } catch { - language = 'en' - } + const language = safeLocalStorage.getItem('language') ?? 'en' const response = await fetch(`./translations/${language}.json`) const file = await response.json() as Resource @@ -76,7 +72,7 @@ function buildLanguageButton (langID: string, name: string, flag: string) { // i18next.addResourceBundle await i18next.changeLanguage(langID) - localStorage.setItem('language', langID) + safeLocalStorage.setItem('language', langID) const shouldReload = await Confirm(i18next.t('general.languageChange'))