2023-07-27 07:31:52 +02:00
|
|
|
|
/*
|
2024-02-13 16:59:27 +01:00
|
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 07:31:52 +02:00
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
*/
|
2024-06-29 11:14:35 +02:00
|
|
|
|
import { isEmbedPage } from '@/scripts/embed-page.js';
|
2023-07-27 07:31:52 +02:00
|
|
|
|
|
2024-06-01 14:03:39 +02:00
|
|
|
|
export type Keys =
|
2023-01-07 02:13:02 +01:00
|
|
|
|
'v' |
|
|
|
|
|
'lastVersion' |
|
|
|
|
|
'instance' |
|
2024-02-23 02:47:17 +01:00
|
|
|
|
'instanceCachedAt' |
|
2023-01-07 02:13:02 +01:00
|
|
|
|
'account' |
|
|
|
|
|
'accounts' |
|
2023-01-07 03:49:00 +01:00
|
|
|
|
'latestDonationInfoShownAt' |
|
|
|
|
|
'neverShowDonationInfo' |
|
2023-04-05 07:30:03 +02:00
|
|
|
|
'neverShowLocalOnlyInfo' |
|
2024-02-17 05:34:50 +01:00
|
|
|
|
'modifiedVersionMustProminentlyOfferInAgplV3Section13Read' |
|
2023-01-07 02:13:02 +01:00
|
|
|
|
'lastUsed' |
|
|
|
|
|
'lang' |
|
|
|
|
|
'drafts' |
|
|
|
|
|
'hashtags' |
|
|
|
|
|
'wallpaper' |
|
|
|
|
|
'theme' |
|
2023-06-05 03:55:18 +02:00
|
|
|
|
'colorScheme' |
|
2023-07-08 00:08:16 +02:00
|
|
|
|
'useSystemFont' |
|
2023-01-07 02:13:02 +01:00
|
|
|
|
'fontSize' |
|
|
|
|
|
'ui' |
|
2023-01-27 05:52:51 +01:00
|
|
|
|
'ui_temp' |
|
2023-01-07 02:13:02 +01:00
|
|
|
|
'locale' |
|
2023-01-21 12:24:15 +01:00
|
|
|
|
'localeVersion' |
|
2023-01-07 02:13:02 +01:00
|
|
|
|
'theme' |
|
|
|
|
|
'customCss' |
|
|
|
|
|
'message_drafts' |
|
|
|
|
|
'scratchpad' |
|
2023-04-13 11:47:49 +02:00
|
|
|
|
'debug' |
|
2023-01-07 02:13:02 +01:00
|
|
|
|
`miux:${string}` |
|
|
|
|
|
`ui:folder:${string}` |
|
|
|
|
|
`themes:${string}` |
|
2023-02-28 12:10:52 +01:00
|
|
|
|
`aiscript:${string}` |
|
|
|
|
|
'lastEmojisFetchedAt' | // DEPRECATED, stored in indexeddb (13.9.0~)
|
2023-12-22 12:41:42 +01:00
|
|
|
|
'emojis' | // DEPRECATED, stored in indexeddb (13.9.0~);
|
2024-06-01 14:03:39 +02:00
|
|
|
|
`channelLastReadedAt:${string}` |
|
|
|
|
|
`idbfallback::${string}`
|
|
|
|
|
|
|
|
|
|
// セッション毎に廃棄されるLocalStorage代替(embedなどで使用)
|
|
|
|
|
const safeSessionStorage = new Map<Keys, string>();
|
2023-01-07 02:13:02 +01:00
|
|
|
|
|
2024-06-29 11:14:35 +02:00
|
|
|
|
const embedPage = isEmbedPage();
|
|
|
|
|
|
2023-01-07 02:13:02 +01:00
|
|
|
|
export const miLocalStorage = {
|
2024-06-01 14:03:39 +02:00
|
|
|
|
getItem: (key: Keys): string | null => {
|
2024-06-29 10:18:32 +02:00
|
|
|
|
if (embedPage) {
|
2024-06-01 14:03:39 +02:00
|
|
|
|
return safeSessionStorage.get(key) ?? null;
|
|
|
|
|
}
|
|
|
|
|
return window.localStorage.getItem(key);
|
|
|
|
|
},
|
|
|
|
|
setItem: (key: Keys, value: string): void => {
|
2024-06-29 10:18:32 +02:00
|
|
|
|
if (embedPage) {
|
2024-06-01 14:03:39 +02:00
|
|
|
|
safeSessionStorage.set(key, value);
|
|
|
|
|
} else {
|
|
|
|
|
window.localStorage.setItem(key, value);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
removeItem: (key: Keys): void => {
|
2024-06-29 10:18:32 +02:00
|
|
|
|
if (embedPage) {
|
2024-06-01 14:03:39 +02:00
|
|
|
|
safeSessionStorage.delete(key);
|
|
|
|
|
} else {
|
|
|
|
|
window.localStorage.removeItem(key);
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-09-10 10:40:59 +02:00
|
|
|
|
getItemAsJson: (key: Keys): any | undefined => {
|
|
|
|
|
const item = miLocalStorage.getItem(key);
|
|
|
|
|
if (item === null) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
return JSON.parse(item);
|
|
|
|
|
},
|
2024-06-01 14:03:39 +02:00
|
|
|
|
setItemAsJson: (key: Keys, value: any): void => {
|
|
|
|
|
miLocalStorage.setItem(key, JSON.stringify(value));
|
|
|
|
|
},
|
2023-01-07 02:13:02 +01:00
|
|
|
|
};
|
2024-06-01 14:03:39 +02:00
|
|
|
|
|
2024-06-29 10:18:32 +02:00
|
|
|
|
if (embedPage) {
|
2024-06-29 11:14:35 +02:00
|
|
|
|
/**
|
|
|
|
|
* EmbedページではlocalStorageを使用できないようにしているが、
|
|
|
|
|
* 動作に必要な値はsafeSessionStorageに移動する
|
|
|
|
|
*/
|
|
|
|
|
const keysToDuplicate: Keys[] = [
|
|
|
|
|
'v',
|
|
|
|
|
'instance',
|
|
|
|
|
'instanceCachedAt',
|
|
|
|
|
'lang',
|
|
|
|
|
'locale',
|
|
|
|
|
'localeVersion',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
keysToDuplicate.forEach(key => {
|
|
|
|
|
const value = window.localStorage.getItem(key);
|
|
|
|
|
if (value && !miLocalStorage.getItem(key)) {
|
|
|
|
|
miLocalStorage.setItem(key, value);
|
|
|
|
|
}
|
|
|
|
|
});
|
2024-06-01 14:03:39 +02:00
|
|
|
|
if (_DEV_) console.warn('Using safeSessionStorage as localStorage alternative');
|
|
|
|
|
}
|