enhance: embedページではstoreの保存先を完全に分離するように

This commit is contained in:
kakkokari-gtyih
2024-06-01 21:03:39 +09:00
parent ecf7945fe8
commit e9b3b5ffcd
13 changed files with 168 additions and 23 deletions

View File

@@ -2,8 +2,9 @@
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { isEmbedPage, initEmbedPageLocalStorage } from "@/scripts/embed-page.js";
type Keys =
export type Keys =
'v' |
'lastVersion' |
'instance' |
@@ -38,12 +39,33 @@ type Keys =
`aiscript:${string}` |
'lastEmojisFetchedAt' | // DEPRECATED, stored in indexeddb (13.9.0~)
'emojis' | // DEPRECATED, stored in indexeddb (13.9.0~);
`channelLastReadedAt:${string}`
`channelLastReadedAt:${string}` |
`idbfallback::${string}`
// セッション毎に廃棄されるLocalStorage代替embedなどで使用
const safeSessionStorage = new Map<Keys, string>();
export const miLocalStorage = {
getItem: (key: Keys): string | null => window.localStorage.getItem(key),
setItem: (key: Keys, value: string): void => window.localStorage.setItem(key, value),
removeItem: (key: Keys): void => window.localStorage.removeItem(key),
getItem: (key: Keys): string | null => {
if (isEmbedPage()) {
return safeSessionStorage.get(key) ?? null;
}
return window.localStorage.getItem(key);
},
setItem: (key: Keys, value: string): void => {
if (isEmbedPage()) {
safeSessionStorage.set(key, value);
} else {
window.localStorage.setItem(key, value);
}
},
removeItem: (key: Keys): void => {
if (isEmbedPage()) {
safeSessionStorage.delete(key);
} else {
window.localStorage.removeItem(key);
}
},
getItemAsJson: (key: Keys): any | undefined => {
const item = miLocalStorage.getItem(key);
if (item === null) {
@@ -51,5 +73,12 @@ export const miLocalStorage = {
}
return JSON.parse(item);
},
setItemAsJson: (key: Keys, value: any): void => window.localStorage.setItem(key, JSON.stringify(value)),
setItemAsJson: (key: Keys, value: any): void => {
miLocalStorage.setItem(key, JSON.stringify(value));
},
};
if (isEmbedPage()) {
initEmbedPageLocalStorage();
if (_DEV_) console.warn('Using safeSessionStorage as localStorage alternative');
}