Files
kiss-translator/src/libs/storage.js

162 lines
3.8 KiB
JavaScript
Raw Normal View History

2023-08-30 18:05:37 +08:00
import {
STOKEY_SETTING,
STOKEY_RULES,
2023-10-26 17:32:55 +08:00
STOKEY_WORDS,
2023-08-30 18:05:37 +08:00
STOKEY_FAB,
STOKEY_SYNC,
STOKEY_MSAUTH,
2023-10-20 17:44:48 +08:00
STOKEY_BDAUTH,
2023-08-30 18:05:37 +08:00
STOKEY_RULESCACHE_PREFIX,
DEFAULT_SETTING,
DEFAULT_RULES,
DEFAULT_SYNC,
BUILTIN_RULES,
} from "../config";
2023-08-31 00:18:57 +08:00
import { isExt, isGm } from "./client";
import { browser } from "./browser";
2024-03-19 18:07:18 +08:00
import { kissLog } from "./log";
2023-07-20 13:45:41 +08:00
async function set(key, val) {
2023-08-05 18:15:01 +08:00
if (isExt) {
2023-07-20 13:45:41 +08:00
await browser.storage.local.set({ [key]: val });
2023-08-05 18:15:01 +08:00
} else if (isGm) {
2023-08-29 00:06:50 +08:00
await (window.KISS_GM || GM).setValue(key, val);
2023-07-20 13:45:41 +08:00
} else {
window.localStorage.setItem(key, val);
}
}
async function get(key) {
2023-08-05 18:15:01 +08:00
if (isExt) {
2023-08-05 15:32:51 +08:00
const val = await browser.storage.local.get([key]);
return val[key];
2023-08-05 18:15:01 +08:00
} else if (isGm) {
2023-08-29 00:06:50 +08:00
const val = await (window.KISS_GM || GM).getValue(key);
2023-08-05 15:32:51 +08:00
return val;
2023-07-20 13:45:41 +08:00
}
return window.localStorage.getItem(key);
}
async function del(key) {
2023-08-05 18:15:01 +08:00
if (isExt) {
2023-07-20 13:45:41 +08:00
await browser.storage.local.remove([key]);
2023-08-05 18:15:01 +08:00
} else if (isGm) {
2023-08-29 00:06:50 +08:00
await (window.KISS_GM || GM).deleteValue(key);
2023-07-20 13:45:41 +08:00
} else {
window.localStorage.removeItem(key);
}
}
async function setObj(key, obj) {
await set(key, JSON.stringify(obj));
}
async function trySetObj(key, obj) {
if (!(await get(key))) {
await setObj(key, obj);
}
}
async function getObj(key) {
const val = await get(key);
return val && JSON.parse(val);
}
async function putObj(key, obj) {
const cur = (await getObj(key)) ?? {};
await setObj(key, { ...cur, ...obj });
}
/**
* 对storage的封装
*/
2023-08-30 18:05:37 +08:00
export const storage = {
2023-07-20 13:45:41 +08:00
get,
set,
del,
setObj,
trySetObj,
getObj,
putObj,
2023-08-30 18:05:37 +08:00
// onChanged,
2023-07-20 13:45:41 +08:00
};
2023-08-30 18:05:37 +08:00
/**
* 设置信息
*/
export const getSetting = () => getObj(STOKEY_SETTING);
2024-03-16 23:37:27 +08:00
export const getSettingWithDefault = async () => ({
...DEFAULT_SETTING,
...((await getSetting()) || {}),
});
2023-08-30 18:05:37 +08:00
export const setSetting = (val) => setObj(STOKEY_SETTING, val);
export const updateSetting = (obj) => putObj(STOKEY_SETTING, obj);
/**
* 规则列表
*/
export const getRules = () => getObj(STOKEY_RULES);
export const getRulesWithDefault = async () =>
(await getRules()) || DEFAULT_RULES;
export const setRules = (val) => setObj(STOKEY_RULES, val);
2023-10-26 17:32:55 +08:00
/**
* 词汇列表
*/
export const getWords = () => getObj(STOKEY_WORDS);
export const getWordsWithDefault = async () => (await getWords()) || {};
export const setWords = (val) => setObj(STOKEY_WORDS, val);
2023-08-30 18:05:37 +08:00
/**
* 订阅规则
*/
export const getSubRules = (url) => getObj(STOKEY_RULESCACHE_PREFIX + url);
export const getSubRulesWithDefault = async () => (await getSubRules()) || [];
export const delSubRules = (url) => del(STOKEY_RULESCACHE_PREFIX + url);
export const setSubRules = (url, val) =>
setObj(STOKEY_RULESCACHE_PREFIX + url, val);
/**
* fab位置
*/
export const getFab = () => getObj(STOKEY_FAB);
export const getFabWithDefault = async () => (await getFab()) || {};
export const setFab = (obj) => setObj(STOKEY_FAB, obj);
2023-09-23 19:16:51 +08:00
export const updateFab = (obj) => putObj(STOKEY_FAB, obj);
2023-08-30 18:05:37 +08:00
/**
* 数据同步
*/
export const getSync = () => getObj(STOKEY_SYNC);
export const getSyncWithDefault = async () => (await getSync()) || DEFAULT_SYNC;
export const updateSync = (obj) => putObj(STOKEY_SYNC, obj);
/**
* ms auth
*/
export const getMsauth = () => getObj(STOKEY_MSAUTH);
export const setMsauth = (val) => setObj(STOKEY_MSAUTH, val);
2023-10-20 17:44:48 +08:00
/**
* baidu auth
*/
export const getBdauth = () => getObj(STOKEY_BDAUTH);
export const setBdauth = (val) => setObj(STOKEY_BDAUTH, val);
2023-08-30 18:05:37 +08:00
/**
* 存入默认数据
*/
export const tryInitDefaultData = async () => {
try {
await trySetObj(STOKEY_SETTING, DEFAULT_SETTING);
await trySetObj(STOKEY_RULES, DEFAULT_RULES);
await trySetObj(STOKEY_SYNC, DEFAULT_SYNC);
await trySetObj(
`${STOKEY_RULESCACHE_PREFIX}${process.env.REACT_APP_RULESURL}`,
BUILTIN_RULES
);
} catch (err) {
2024-03-19 18:07:18 +08:00
kissLog(err, "init default");
2023-08-30 18:05:37 +08:00
}
};