diff --git a/src/cssCache.js b/src/cssCache.js index 4afc33b6..e3ef5ce1 100644 --- a/src/cssCache.js +++ b/src/cssCache.js @@ -17,7 +17,7 @@ const save = () => { css = css.replace(new RegExp(`body.${x}`, 'g'), `body`) }); - localStorage.setItem('goosemodCSSCache', css); + goosemod.storage.set('goosemodCSSCache', css); goosemod.showToast('Saved', { subtext: 'CSS Cache', type: 'debuginfo' }); }; @@ -72,7 +72,7 @@ export const load = () => { const el = document.createElement('style'); el.id = `gm-css-cache`; - el.appendChild(document.createTextNode(localStorage.getItem('goosemodCSSCache') || '')); + el.appendChild(document.createTextNode(goosemod.storage.get('goosemodCSSCache') || '')); document.body.appendChild(el); diff --git a/src/gmSettings.js b/src/gmSettings.js index 20abf8e9..0b450c57 100644 --- a/src/gmSettings.js +++ b/src/gmSettings.js @@ -20,7 +20,7 @@ export const get = () => { // Cache as this function is called frequently if (cache) return cache; - cache = JSON.parse(localStorage.getItem('goosemodGMSettings')) || defaultSettings; + cache = JSON.parse(goosemod.storage.get('goosemodGMSettings')) || defaultSettings; cache = { ...defaultSettings, @@ -35,7 +35,7 @@ export const set = (key, value) => { settings[key] = value; - localStorage.setItem('goosemodGMSettings', JSON.stringify(settings)); + goosemod.storage.set('goosemodGMSettings', JSON.stringify(settings)); cache = settings; // Set cache to new value }; \ No newline at end of file diff --git a/src/i18n/cache.js b/src/i18n/cache.js index 63b96d24..1298bc5a 100644 --- a/src/i18n/cache.js +++ b/src/i18n/cache.js @@ -7,15 +7,15 @@ export const setThisScope = (scope) => { goosemodScope = scope; }; -export const getCache = () => JSON.parse(localStorage.getItem('goosemodi18nCache') || '{}'); -export const purgeCache = () => localStorage.removeItem('goosemodi18nCache'); +export const getCache = () => JSON.parse(goosemod.storage.get('goosemodi18nCache') || '{}'); +export const purgeCache = () => goosemod.storage.remove('goosemodi18nCache'); export const updateCache = (lang, hash, goosemodStrings) => { let cache = getCache(); cache[lang] = { hash, goosemodStrings }; - localStorage.setItem('goosemodi18nCache', JSON.stringify(cache)); + goosemod.storage.set('goosemodi18nCache', JSON.stringify(cache)); }; export const geti18nData = async (lang) => { diff --git a/src/index.js b/src/index.js index 9a5c38b1..d865e522 100644 --- a/src/index.js +++ b/src/index.js @@ -34,7 +34,7 @@ import * as CSSCache from './cssCache'; import * as GMBadges from './gmBadges'; -import * as Storage from './storage'; +import Storage from './storage'; const scopeSetterFncs = [ setThisScope1, @@ -112,15 +112,15 @@ const init = async function () { } this.versioning = { - version: '10.0.0-dev (AND/I)', + version: '10.0.0-dev (S:LS)', hash: '', // Hash of built final js file is inserted here via build script - lastUsedVersion: localStorage.getItem('goosemodLastVersion') + lastUsedVersion: this.storage.get('goosemodLastVersion') }; this.versioning.isDeveloperBuild = this.versioning.hash === ''; - localStorage.setItem('goosemodLastVersion', this.versioning.version); + this.storage.set('goosemodLastVersion', this.versioning.version); this.logger.debug('import.version.goosemod', `${this.versioning.version} (${this.versioning.hash})`); @@ -141,15 +141,15 @@ const init = async function () { this.updateLoadingScreen('Initialising internals...'); - let toInstallModules = Object.keys(JSON.parse(localStorage.getItem('goosemodModules')) || {}); - let disabledModules = Object.keys(JSON.parse(localStorage.getItem('goosemodDisabled')) || {}); + let toInstallModules = Object.keys(JSON.parse(this.storage.get('goosemodModules')) || {}); + let disabledModules = Object.keys(JSON.parse(this.storage.get('goosemodDisabled')) || {}); this.modules = toInstallModules.filter((x) => disabledModules.indexOf(x) === -1).reduce((acc, v) => { acc[v] = { goosemodHandlers: { } }; return acc; }, {}); this.disabledModules = toInstallModules.filter((x) => disabledModules.indexOf(x) !== -1).reduce((acc, v) => { acc[v] = { goosemodHandlers: { } }; return acc; }, {}); console.log(this.modules, this.disabledModules); - this.moduleStoreAPI.modules = JSON.parse(localStorage.getItem('goosemodCachedModules')) || []; + this.moduleStoreAPI.modules = JSON.parse(this.storage.get('goosemodCachedModules')) || []; this.moduleStoreAPI.modules.cached = true; this.settings.makeGooseModSettings(); @@ -158,7 +158,7 @@ const init = async function () { this.removed = false; - if (!localStorage.getItem('goosemodCachedModules')) { // If not cached, fetch latest repos + if (!this.storage.get('goosemodCachedModules')) { // If not cached, fetch latest repos await this.moduleStoreAPI.updateModules(true); } @@ -216,7 +216,7 @@ const init = async function () { clearInterval(this.i18nCheckNewLangInterval); clearInterval(this.hotupdateInterval); - Object.keys(localStorage).filter((x) => x.toLowerCase().startsWith('goosemod')).forEach((x) => localStorage.removeItem(x)); + this.storage.keys().filter((x) => x.toLowerCase().startsWith('goosemod')).forEach((x) => this.storage.remove(x)); this.removed = true; @@ -245,7 +245,7 @@ const init = async function () { } - if (!localStorage.getItem('goosemodOOTB')) { // First time install + if (!this.storage.get('goosemodOOTB')) { // First time install await sleep(1000); // Wait for slowdown / Discord loading to ease while (document.querySelector('.modal-3O0aXp')) { // Wait for modals to close (GM changelog, etc) @@ -254,7 +254,7 @@ const init = async function () { this.ootb.start(); - localStorage.setItem('goosemodOOTB', true); + this.storage.set('goosemodOOTB', true); } }; diff --git a/src/moduleSettingsStore.js b/src/moduleSettingsStore.js index d54bcf4c..138cea0b 100644 --- a/src/moduleSettingsStore.js +++ b/src/moduleSettingsStore.js @@ -6,30 +6,30 @@ export const setThisScope = (scope) => { export const disableModule = (name) => { - let settings = JSON.parse(localStorage.getItem('goosemodDisabled')) || {}; + let settings = JSON.parse(goosemod.storage.get('goosemodDisabled')) || {}; settings[name] = true; - localStorage.setItem('goosemodDisabled', JSON.stringify(settings)); + goosemod.storage.set('goosemodDisabled', JSON.stringify(settings)); }; export const enableModule = (name) => { - let settings = JSON.parse(localStorage.getItem('goosemodDisabled')) || {}; + let settings = JSON.parse(goosemod.storage.get('goosemodDisabled')) || {}; delete settings[name]; - localStorage.setItem('goosemodDisabled', JSON.stringify(settings)); + goosemod.storage.set('goosemodDisabled', JSON.stringify(settings)); }; export const checkDisabled = (name) => { - return Object.keys(JSON.parse(localStorage.getItem('goosemodDisabled')) || {}).includes(name); + return Object.keys(JSON.parse(goosemod.storage.get('goosemodDisabled')) || {}).includes(name); }; export const saveModuleSettings = async () => { //goosemodScope.logger.debug('settings', 'Saving module settings...'); - let settings = JSON.parse(localStorage.getItem('goosemodModules')) || {}; + let settings = JSON.parse(goosemod.storage.get('goosemodModules')) || {}; for (let p in goosemodScope.modules) { if (goosemodScope.modules.hasOwnProperty(p)) { @@ -37,29 +37,29 @@ export const saveModuleSettings = async () => { } } - if (JSON.stringify(JSON.parse(localStorage.getItem('goosemodModules'))) !== JSON.stringify(settings)) { - localStorage.setItem('goosemodModules', JSON.stringify(settings)); + if (JSON.stringify(JSON.parse(goosemod.storage.get('goosemodModules'))) !== JSON.stringify(settings)) { + goosemod.storage.set('goosemodModules', JSON.stringify(settings)); // goosemodScope.showToast('Settings saved'); } }; export const clearModuleSetting = (moduleName) => { - let settings = JSON.parse(localStorage.getItem('goosemodModules')); + let settings = JSON.parse(goosemod.storage.get('goosemodModules')); if (!settings || !settings[moduleName]) return; delete settings[moduleName]; - localStorage.setItem('goosemodModules', JSON.stringify(settings)); + goosemod.storage.set('goosemodModules', JSON.stringify(settings)); }; export const clearSettings = () => { - localStorage.removeItem('goosemodModules'); + goosemod.storage.remove('goosemodModules'); }; export const loadSavedModuleSetting = async (moduleName) => { - let settings = JSON.parse(localStorage.getItem('goosemodModules')); + let settings = JSON.parse(goosemod.storage.get('goosemodModules')); if (!settings || !settings[moduleName]) return; @@ -69,7 +69,7 @@ export const loadSavedModuleSetting = async (moduleName) => { /* export const loadSavedModuleSettings = async () => { //goosemodScope.logger.debug('settings', 'Loading module settings...'); - let settings = JSON.parse(localStorage.getItem('goosemodModules')); + let settings = JSON.parse(goosemod.storage.get('goosemodModules')); if (!settings) return; diff --git a/src/moduleStore/idCache.js b/src/moduleStore/idCache.js index 55e02c77..33bd7817 100644 --- a/src/moduleStore/idCache.js +++ b/src/moduleStore/idCache.js @@ -29,8 +29,8 @@ const processQueue = async () => { queueReturns.push(await getUser(id)); }; -export const getCache = () => JSON.parse(localStorage.getItem('goosemodIDCache') || '{}'); -export const purgeCache = () => localStorage.removeItem('goosemodIDCache'); +export const getCache = () => JSON.parse(goosemod.storage.get('goosemodIDCache') || '{}'); +export const purgeCache = () => goosemod.storage.remove('goosemodIDCache'); export const updateCache = (id, data) => { let cache = getCache(); @@ -40,7 +40,7 @@ export const updateCache = (id, data) => { time: currentDate }; - localStorage.setItem('goosemodIDCache', JSON.stringify(cache)); + goosemod.storage.set('goosemodIDCache', JSON.stringify(cache)); }; export const getDataForID = async (id) => { diff --git a/src/moduleStore/index.js b/src/moduleStore/index.js index fe9820b5..fe961a0d 100644 --- a/src/moduleStore/index.js +++ b/src/moduleStore/index.js @@ -67,7 +67,7 @@ export default { meta: await getFirstMeta(url) }); - goosemodScope.moduleStoreAPI.repos = JSON.parse(localStorage.getItem('goosemodRepos')) || [ + goosemodScope.moduleStoreAPI.repos = JSON.parse(goosemod.storage.get('goosemodRepos')) || [ await getFirstObj(`https://store.goosemod.com/goosemod.json`), await getFirstObj(`https://store.goosemod.com/ms2porter.json`), await getFirstObj(`https://store.goosemod.com/bdthemes.json`), @@ -106,8 +106,8 @@ export default { goosemodScope.moduleStoreAPI.modules = newModules; - localStorage.setItem('goosemodRepos', JSON.stringify(goosemodScope.moduleStoreAPI.repos)); - localStorage.setItem('goosemodCachedModules', JSON.stringify(goosemodScope.moduleStoreAPI.modules)); + goosemod.storage.set('goosemodRepos', JSON.stringify(goosemodScope.moduleStoreAPI.repos)); + goosemod.storage.set('goosemodCachedModules', JSON.stringify(goosemodScope.moduleStoreAPI.modules)); }, importModule: async (moduleName, disabled = false) => { diff --git a/src/moduleStore/jsCache.js b/src/moduleStore/jsCache.js index 55aaf910..ca778f62 100644 --- a/src/moduleStore/jsCache.js +++ b/src/moduleStore/jsCache.js @@ -4,15 +4,15 @@ export const setThisScope = (scope) => { goosemodScope = scope; }; -export const getCache = () => JSON.parse(localStorage.getItem('goosemodJSCache') || '{}'); -export const purgeCache = () => localStorage.removeItem('goosemodJSCache'); +export const getCache = () => JSON.parse(goosemod.storage.get('goosemodJSCache') || '{}'); +export const purgeCache = () => goosemod.storage.remove('goosemodJSCache'); export const updateCache = (moduleName, hash, js) => { let cache = goosemodScope.moduleStoreAPI.jsCache.getCache(); cache[moduleName] = {hash, js}; - localStorage.setItem('goosemodJSCache', JSON.stringify(cache)); + goosemod.storage.set('goosemodJSCache', JSON.stringify(cache)); }; export const getJSForModule = async (moduleName) => { diff --git a/src/ui/settings/baseItems.js b/src/ui/settings/baseItems.js index 24b84969..a92e96d4 100644 --- a/src/ui/settings/baseItems.js +++ b/src/ui/settings/baseItems.js @@ -33,9 +33,9 @@ export default (goosemodScope, gmSettings, Items) => { case 'devchannel': { if (value) { - localStorage.setItem('goosemodUntetheredBranch', 'dev'); + goosemod.storage.set('goosemodUntetheredBranch', 'dev'); } else { - localStorage.removeItem('goosemodUntetheredBranch'); + goosemod.storage.remove('goosemodUntetheredBranch'); } break; @@ -177,7 +177,7 @@ export default (goosemodScope, gmSettings, Items) => { changeSetting('devchannel', c); refreshPrompt(); }, - isToggled: () => localStorage.getItem('goosemodUntetheredBranch') === 'dev' + isToggled: () => goosemod.storage.get('goosemodUntetheredBranch') === 'dev' }, { @@ -238,7 +238,7 @@ export default (goosemodScope, gmSettings, Items) => { onclick: async () => { // Like remove's dynamic local storage removal, but only remove GooseMod keys with "Cache" in - Object.keys(localStorage).filter((x) => x.toLowerCase().startsWith('goosemod') && x.includes('Cache')).forEach((x) => localStorage.removeItem(x)); + goosemod.storage.keys().filter((x) => x.toLowerCase().startsWith('goosemod') && x.includes('Cache')).forEach((x) => goosemod.storage.remove(x)); refreshPrompt(); } @@ -269,8 +269,8 @@ export default (goosemodScope, gmSettings, Items) => { buttonText: 'Backup', onclick: () => { - const obj = Object.keys(localStorage).filter((x) => x.toLowerCase().startsWith('goosemod') && !x.includes('Cache')).reduce((acc, k) => { - acc[k] = localStorage.getItem(k); + const obj = goosemod.storage.keys().filter((x) => x.toLowerCase().startsWith('goosemod') && !x.includes('Cache')).reduce((acc, k) => { + acc[k] = goosemod.storage.get(k); return acc; }, {}); @@ -319,7 +319,7 @@ export default (goosemodScope, gmSettings, Items) => { for (const k in obj) { if (!k.startsWith('goosemod')) continue; // Don't set if not goosemod key for some security - localStorage.setItem(k, obj[k]); + goosemod.storage.set(k, obj[k]); } location.reload(); diff --git a/src/ui/settings/home/index.js b/src/ui/settings/home/index.js index 07372654..9e2fe8d8 100644 --- a/src/ui/settings/home/index.js +++ b/src/ui/settings/home/index.js @@ -263,7 +263,7 @@ export default (goosemodScope) => { // Make store setting with cached modules whilst waiting for hotupdate from repos await goosemodScope.moduleStoreAPI.updateStoreSetting(); - const snippetsLoaded = (JSON.parse(localStorage.getItem('goosemodSnippets')) || {}); + const snippetsLoaded = (JSON.parse(goosemod.storage.get('goosemodSnippets')) || {}); for (const id in snippetsLoaded) { const css = snippetsLoaded[id]; @@ -358,7 +358,7 @@ export default (goosemodScope) => { toSave[id] = toSave[id].textContent; } - localStorage.setItem('goosemodSnippets', JSON.stringify(toSave)); + goosemod.storage.set('goosemodSnippets', JSON.stringify(toSave)); }, showToggle: false, // No toggling snippets as overcomplex for small snippets