diff --git a/src/client/account.ts b/src/client/account.ts index 1ba7a28908..5519fd028c 100644 --- a/src/client/account.ts +++ b/src/client/account.ts @@ -1,4 +1,4 @@ -import { get, set } from 'idb-keyval'; +import { get, set } from '@/scripts/idb-proxy'; import { reactive } from 'vue'; import { apiUrl } from '@/config'; import { waiting } from '@/os'; diff --git a/src/client/init.ts b/src/client/init.ts index 264ecaf8d1..28d919e8b9 100644 --- a/src/client/init.ts +++ b/src/client/init.ts @@ -4,7 +4,7 @@ import '@/style.scss'; -import { set } from 'idb-keyval'; +import { set } from '@/scripts/idb-proxy'; // TODO: そのうち消す if (localStorage.getItem('vuex') != null) { diff --git a/src/client/scripts/get-account-from-id.ts b/src/client/scripts/get-account-from-id.ts index be4cfaeba4..ba3adceecc 100644 --- a/src/client/scripts/get-account-from-id.ts +++ b/src/client/scripts/get-account-from-id.ts @@ -1,4 +1,4 @@ -import { get } from 'idb-keyval'; +import { get } from '@/scripts/idb-proxy'; export async function getAccountFromId(id: string) { const accounts = await get('accounts') as { token: string; id: string; }[]; diff --git a/src/client/scripts/idb-proxy.ts b/src/client/scripts/idb-proxy.ts new file mode 100644 index 0000000000..986e8ee8dc --- /dev/null +++ b/src/client/scripts/idb-proxy.ts @@ -0,0 +1,23 @@ +// FirefoxのプライベートモードなどではindexedDBが使用不可能なので、使う +import { + get as iget, + set as iset, + del as idel +} from 'idb-keyval'; + +const fallbackName = (key: string) => `idbfallback::${key}`; + +export async function get(key: string) { + if (window.indexedDB) return iget(key); + return JSON.parse(localStorage.getItem(fallbackName(key)) || 'null'); +} + +export async function set(key: string, val: any) { + if (window.indexedDB) return iset(key, val); + return localStorage.setItem(fallbackName(key), JSON.stringify(val)); +} + +export async function del(key: string) { + if (window.indexedDB) return idel(key); + return localStorage.removeItem(fallbackName(key)); +} diff --git a/src/client/sw/lang.ts b/src/client/sw/lang.ts index 1b3f1ad620..99f0e518c3 100644 --- a/src/client/sw/lang.ts +++ b/src/client/sw/lang.ts @@ -3,7 +3,7 @@ */ declare var self: ServiceWorkerGlobalScope; -import { get, set } from 'idb-keyval'; +import { get, set } from '@/scripts/idb-proxy'; import { I18n } from '../../misc/i18n'; class SwLang { diff --git a/src/client/sw/notification-read.ts b/src/client/sw/notification-read.ts index 093322a8b7..03cb0611ca 100644 --- a/src/client/sw/notification-read.ts +++ b/src/client/sw/notification-read.ts @@ -1,6 +1,6 @@ declare var self: ServiceWorkerGlobalScope; -import { get } from 'idb-keyval'; +import { get } from '@/scripts/idb-proxy'; import { pushNotificationData } from '../../types'; import { api } from './operations';