PEE/src/stores.ts

80 lines
1.8 KiB
TypeScript
Raw Normal View History

import type { HydrusClient } from "./hydrus";
2022-01-09 14:29:51 +00:00
import type { Booru } from "./thirdeye";
2022-01-02 13:12:19 +00:00
2022-08-08 11:05:25 +00:00
import { writable } from "svelte/store";
import { Platform } from "./platform";
2022-07-24 17:22:39 +00:00
// Todo: use GM get/setValue instead?
export const localLoad = async <T>(key: string, def: T) => {
2022-08-08 11:05:25 +00:00
const ret = await Platform.getValue(key, def);
2022-08-06 12:08:17 +00:00
if (execution_mode == "worker") {
2022-08-08 11:05:25 +00:00
return (ret as any).res;
2022-08-06 12:08:17 +00:00
}
2022-08-08 11:05:25 +00:00
2022-07-24 17:22:39 +00:00
return ret;
};
const localSet = (key: string, value: any) => {
2022-08-08 11:05:25 +00:00
if (execution_mode == "worker") {
2022-08-06 12:08:17 +00:00
return;
}
2022-07-24 17:22:39 +00:00
if (execution_mode != "userscript")
chrome.storage.local.set({ [key]: JSON.stringify(value) });
else
localStorage.setItem('__pee__' + key, JSON.stringify(value));
};
2022-01-02 13:12:19 +00:00
2022-04-14 20:07:13 +00:00
export const initial_settings = localLoad('settingsv2', {
2022-01-04 20:26:05 +00:00
loop: true,
2022-01-05 01:14:23 +00:00
dh: false,
2022-07-29 01:06:03 +00:00
pmeth: 5,
2022-01-02 15:02:09 +00:00
xpv: false,
xpi: false,
hyd: false,
notcata: false,
ak: '',
auto_embed: 0,
auto_tags: '',
2022-01-05 01:14:23 +00:00
te: false,
2022-01-05 19:12:12 +00:00
eye: false,
2022-01-07 04:43:28 +00:00
ca: false,
pre: false,
prev: false,
sh: false,
ep: false,
2022-04-11 21:31:32 +00:00
tm: false,
dvc: false,
2022-01-12 04:18:50 +00:00
expte: false,
2022-01-26 20:45:15 +00:00
mdist: -1,
phash: false,
2022-01-12 08:09:30 +00:00
hotlink: false,
2022-07-19 15:29:49 +00:00
jpeg: false,
2022-01-13 07:38:50 +00:00
vercheck: false,
cache: undefined as (boolean | undefined), // meaning defaults to false, except on b4k
fhost: 0,
maxe: 5,
2022-01-12 04:18:50 +00:00
conc: 8,
2022-01-09 19:41:04 +00:00
ho: false,
2022-07-22 18:47:39 +00:00
blacklist: [] as string[],
2022-07-24 17:22:39 +00:00
rsources: [] as (Omit<Booru, 'quirks'> & { view: string, disabled?: boolean })[],
2022-04-14 20:07:13 +00:00
});
2022-07-24 17:22:39 +00:00
export const settings = writable<Awaited<typeof initial_settings>>();
initial_settings.then(v => {
settings.set(v);
});
2022-01-02 13:12:19 +00:00
2022-01-07 04:43:28 +00:00
export const appState = writable({
2022-01-07 06:45:30 +00:00
isCatalog: false,
2022-01-08 22:08:20 +00:00
is4chanX: false,
akValid: false,
herror: '' as string | undefined,
client: null as HydrusClient | null,
2022-01-07 06:45:30 +00:00
foundPosts: [] as HTMLElement[]
2022-01-07 04:43:28 +00:00
});
2022-01-02 13:12:19 +00:00
settings.subscribe(newVal => {
2022-01-09 14:29:51 +00:00
localSet('settingsv2', newVal);
2022-01-02 13:12:19 +00:00
});