2023-08-05 18:15:01 +08:00
|
|
|
import { browser, isExt, isGm } from "./browser";
|
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
|
|
|
const oldValue = await (window.KISS_GM || GM).getValue(key);
|
|
|
|
|
await (window.KISS_GM || GM).setValue(key, val);
|
2023-08-05 20:11:02 +08:00
|
|
|
window.dispatchEvent(
|
|
|
|
|
new StorageEvent("storage", {
|
|
|
|
|
key,
|
|
|
|
|
oldValue,
|
|
|
|
|
newValue: val,
|
|
|
|
|
})
|
|
|
|
|
);
|
2023-07-20 13:45:41 +08:00
|
|
|
} else {
|
|
|
|
|
const oldValue = window.localStorage.getItem(key);
|
|
|
|
|
window.localStorage.setItem(key, val);
|
|
|
|
|
window.dispatchEvent(
|
|
|
|
|
new StorageEvent("storage", {
|
|
|
|
|
key,
|
|
|
|
|
oldValue,
|
|
|
|
|
newValue: 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
|
|
|
const oldValue = await (window.KISS_GM || GM).getValue(key);
|
|
|
|
|
await (window.KISS_GM || GM).deleteValue(key);
|
2023-08-05 20:11:02 +08:00
|
|
|
window.dispatchEvent(
|
|
|
|
|
new StorageEvent("storage", {
|
|
|
|
|
key,
|
|
|
|
|
oldValue,
|
|
|
|
|
newValue: null,
|
|
|
|
|
})
|
|
|
|
|
);
|
2023-07-20 13:45:41 +08:00
|
|
|
} else {
|
|
|
|
|
const oldValue = window.localStorage.getItem(key);
|
|
|
|
|
window.localStorage.removeItem(key);
|
|
|
|
|
window.dispatchEvent(
|
|
|
|
|
new StorageEvent("storage", {
|
|
|
|
|
key,
|
|
|
|
|
oldValue,
|
|
|
|
|
newValue: null,
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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事件
|
|
|
|
|
* @param {*} handleChanged
|
|
|
|
|
*/
|
|
|
|
|
function onChanged(handleChanged) {
|
2023-08-05 18:15:01 +08:00
|
|
|
if (isExt) {
|
2023-07-20 13:45:41 +08:00
|
|
|
browser.storage.onChanged.addListener(handleChanged);
|
|
|
|
|
} else {
|
|
|
|
|
window.addEventListener("storage", handleChanged);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 对storage的封装
|
|
|
|
|
*/
|
|
|
|
|
const storage = {
|
|
|
|
|
get,
|
|
|
|
|
set,
|
|
|
|
|
del,
|
|
|
|
|
setObj,
|
|
|
|
|
trySetObj,
|
|
|
|
|
getObj,
|
|
|
|
|
putObj,
|
|
|
|
|
onChanged,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default storage;
|