diff --git a/src/lib/host/index.ts b/src/lib/host/index.ts index 5668c02..5d9ab34 100644 --- a/src/lib/host/index.ts +++ b/src/lib/host/index.ts @@ -30,9 +30,11 @@ export interface HostMatch { url: string | null; } +export type HostId = string; + export interface Host { name: string; - id: string; + id: HostId; domains: string[]; replace?: boolean; regex: RegExp[]; @@ -63,6 +65,10 @@ export const hosts = [ Vupload ]; +export function getHostFromId(id: HostId): Host | null { + return hosts.find((host) => host.id === id) ?? null; +} + export async function getHost(domain: string): Promise { if (await HostSettings.getAllHostsDisabled()) return null; @@ -74,5 +80,5 @@ export async function getHost(domain: string): Promise { } } - return HostSettings.checkTemporaryHostDomain(domain); + return HostSettings.checkTemporaryHostDomain(domain).then(getHostFromId); } diff --git a/src/lib/settings.ts b/src/lib/settings.ts index eb2468c..952f24a 100644 --- a/src/lib/settings.ts +++ b/src/lib/settings.ts @@ -1,86 +1,66 @@ import { storage } from '#imports'; -import { hosts, type Host } from '@/lib/host'; +import type { Host, HostId } from '@/lib/host'; + +class Setting { + private item; + + constructor(key: Parameters[0], fallback: T) { + this.item = storage.defineItem(key, { fallback }); + } + + get = () => this.item.getValue(); + set = (value: T) => this.item.setValue(value); + + update = (fn: (val: T) => T) => this.get().then(fn).then(this.set); +} export class HostSettings { /* disabled hosts */ - private static disabledHosts = storage.defineItem('local:disabledHosts', { fallback: [] }); - private static allHostsDisabled = storage.defineItem('local:allHostsDisabled', { fallback: false }); + private static disabledHosts = new Setting('local:disabledHosts', []); + private static allHostsDisabled = new Setting('local:allHostsDisabled', false); - static async addDisabledHost(host: Host) { - const ids = await this.disabledHosts.getValue(); + static addDisabledHost = (host: Host) => + this.disabledHosts.update((val) => { + if (!val.includes(host.id)) val.push(host.id); + return val; + }); + static removeDisabledHost = (host: Host) => + this.disabledHosts.update((val) => { + const index = val.indexOf(host.id); + if (index !== -1) val.splice(index, 1); + return val; + }); - const index = ids.indexOf(host.id); - if (index === -1) { - ids.push(host.id); - await this.disabledHosts.setValue(ids); - } - } - static async removeDisabledHost(host: Host) { - const ids = await this.disabledHosts.getValue(); - - const index = ids.indexOf(host.id); - if (index !== -1) { - ids.splice(index, 1); - await this.disabledHosts.setValue(ids); - } - } - static async getDisabledHosts() { - return await this.disabledHosts.getValue(); - } - - static async getAllHostsDisabled() { - return await this.allHostsDisabled.getValue(); - } - - static async setAllHostsDisabled(disabled: boolean) { - await this.allHostsDisabled.setValue(disabled); - } + static getDisabledHosts = () => this.disabledHosts.get(); + static getAllHostsDisabled = () => this.allHostsDisabled.get(); + static setAllHostsDisabled = (disabled: boolean) => this.allHostsDisabled.set(disabled); /* tmp */ - private static temporaryHostDomain = storage.defineItem>('local:temporaryHostDomain', { - fallback: {} - }); + private static temporaryHostDomain = new Setting>('local:temporaryHostDomain', {}); - static async addTemporaryHostDomain(host: Host, domain: string) { - const temporaryHostDomains = await this.temporaryHostDomain.getValue(); - - temporaryHostDomains[domain] = host.id; - await this.temporaryHostDomain.setValue(temporaryHostDomains); - } - static async checkTemporaryHostDomain(domain: string) { - const temporaryHostDomains = await this.temporaryHostDomain.getValue(); - - const hostId = temporaryHostDomains[domain]; - return hostId ? (hosts.find((host) => host.id === hostId) ?? null) : null; - } + static addTemporaryHostDomain = (host: Host, domain: string) => + this.temporaryHostDomain.update((val) => { + val[domain] = host.id; + return val; + }); + static checkTemporaryHostDomain = (domain: string) => this.temporaryHostDomain.get().then((val) => val[domain]); } export class FF2MPVSettings { - private static ff2mpvEnabled = storage.defineItem('local:ff2mpv', { fallback: false }); + private static ff2mpvEnabled = new Setting('local:ff2mpv', false); - static async getEnabled() { - return await this.ff2mpvEnabled.getValue(); - } - static async setEnabled(enabled: boolean) { - await this.ff2mpvEnabled.setValue(enabled); - } + static getEnabled = () => this.ff2mpvEnabled.get(); + static setEnabled = (enabled: boolean) => this.ff2mpvEnabled.set(enabled); } export class UrlReferer { - private static temporaryUrlReferer = storage.defineItem>('local:temporaryUrlReferer', { - fallback: {} - }); + private static temporaryUrlReferer = new Setting>('local:temporaryUrlReferer', {}); - static async addTemporary(hostname: string, referer: string) { - const tmpUrlReferer = await this.temporaryUrlReferer.getValue(); + static addTemporary = (hostname: string, referer: string) => + this.temporaryUrlReferer.update((val) => { + val[hostname] = referer; + return val; + }); - tmpUrlReferer[hostname] = referer; - await this.temporaryUrlReferer.setValue(tmpUrlReferer); - } - - static async get(hostname: string) { - const tmpUrlReferer = await this.temporaryUrlReferer.getValue(); - - return tmpUrlReferer[hostname] ?? null; - } + static get = (hostname: string) => this.temporaryUrlReferer.get().then((val) => val[hostname] ?? null); }