mirror of
https://github.com/bytedream/stream-bypass.git
synced 2026-08-04 21:10:40 +02:00
cleanup settings
This commit is contained in:
@@ -30,9 +30,11 @@ export interface HostMatch {
|
|||||||
url: string | null;
|
url: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type HostId = string;
|
||||||
|
|
||||||
export interface Host {
|
export interface Host {
|
||||||
name: string;
|
name: string;
|
||||||
id: string;
|
id: HostId;
|
||||||
domains: string[];
|
domains: string[];
|
||||||
replace?: boolean;
|
replace?: boolean;
|
||||||
regex: RegExp[];
|
regex: RegExp[];
|
||||||
@@ -63,6 +65,10 @@ export const hosts = [
|
|||||||
Vupload
|
Vupload
|
||||||
];
|
];
|
||||||
|
|
||||||
|
export function getHostFromId(id: HostId): Host | null {
|
||||||
|
return hosts.find((host) => host.id === id) ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
export async function getHost(domain: string): Promise<Host | null> {
|
export async function getHost(domain: string): Promise<Host | null> {
|
||||||
if (await HostSettings.getAllHostsDisabled()) return null;
|
if (await HostSettings.getAllHostsDisabled()) return null;
|
||||||
|
|
||||||
@@ -74,5 +80,5 @@ export async function getHost(domain: string): Promise<Host | null> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return HostSettings.checkTemporaryHostDomain(domain);
|
return HostSettings.checkTemporaryHostDomain(domain).then(getHostFromId);
|
||||||
}
|
}
|
||||||
|
|||||||
+47
-67
@@ -1,86 +1,66 @@
|
|||||||
import { storage } from '#imports';
|
import { storage } from '#imports';
|
||||||
import { hosts, type Host } from '@/lib/host';
|
import type { Host, HostId } from '@/lib/host';
|
||||||
|
|
||||||
|
class Setting<T> {
|
||||||
|
private item;
|
||||||
|
|
||||||
|
constructor(key: Parameters<typeof storage.defineItem>[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 {
|
export class HostSettings {
|
||||||
/* disabled hosts */
|
/* disabled hosts */
|
||||||
private static disabledHosts = storage.defineItem<string[]>('local:disabledHosts', { fallback: [] });
|
private static disabledHosts = new Setting<HostId[]>('local:disabledHosts', []);
|
||||||
private static allHostsDisabled = storage.defineItem<boolean>('local:allHostsDisabled', { fallback: false });
|
private static allHostsDisabled = new Setting<boolean>('local:allHostsDisabled', false);
|
||||||
|
|
||||||
static async addDisabledHost(host: Host) {
|
static addDisabledHost = (host: Host) =>
|
||||||
const ids = await this.disabledHosts.getValue();
|
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);
|
static getDisabledHosts = () => this.disabledHosts.get();
|
||||||
if (index === -1) {
|
static getAllHostsDisabled = () => this.allHostsDisabled.get();
|
||||||
ids.push(host.id);
|
static setAllHostsDisabled = (disabled: boolean) => this.allHostsDisabled.set(disabled);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* tmp */
|
/* tmp */
|
||||||
private static temporaryHostDomain = storage.defineItem<Record<string, string>>('local:temporaryHostDomain', {
|
private static temporaryHostDomain = new Setting<Record<string, string>>('local:temporaryHostDomain', {});
|
||||||
fallback: {}
|
|
||||||
});
|
|
||||||
|
|
||||||
static async addTemporaryHostDomain(host: Host, domain: string) {
|
static addTemporaryHostDomain = (host: Host, domain: string) =>
|
||||||
const temporaryHostDomains = await this.temporaryHostDomain.getValue();
|
this.temporaryHostDomain.update((val) => {
|
||||||
|
val[domain] = host.id;
|
||||||
temporaryHostDomains[domain] = host.id;
|
return val;
|
||||||
await this.temporaryHostDomain.setValue(temporaryHostDomains);
|
});
|
||||||
}
|
static checkTemporaryHostDomain = (domain: string) => this.temporaryHostDomain.get().then((val) => val[domain]);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class FF2MPVSettings {
|
export class FF2MPVSettings {
|
||||||
private static ff2mpvEnabled = storage.defineItem<boolean>('local:ff2mpv', { fallback: false });
|
private static ff2mpvEnabled = new Setting<boolean>('local:ff2mpv', false);
|
||||||
|
|
||||||
static async getEnabled() {
|
static getEnabled = () => this.ff2mpvEnabled.get();
|
||||||
return await this.ff2mpvEnabled.getValue();
|
static setEnabled = (enabled: boolean) => this.ff2mpvEnabled.set(enabled);
|
||||||
}
|
|
||||||
static async setEnabled(enabled: boolean) {
|
|
||||||
await this.ff2mpvEnabled.setValue(enabled);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class UrlReferer {
|
export class UrlReferer {
|
||||||
private static temporaryUrlReferer = storage.defineItem<Record<string, string>>('local:temporaryUrlReferer', {
|
private static temporaryUrlReferer = new Setting<Record<string, string>>('local:temporaryUrlReferer', {});
|
||||||
fallback: {}
|
|
||||||
});
|
|
||||||
|
|
||||||
static async addTemporary(hostname: string, referer: string) {
|
static addTemporary = (hostname: string, referer: string) =>
|
||||||
const tmpUrlReferer = await this.temporaryUrlReferer.getValue();
|
this.temporaryUrlReferer.update((val) => {
|
||||||
|
val[hostname] = referer;
|
||||||
|
return val;
|
||||||
|
});
|
||||||
|
|
||||||
tmpUrlReferer[hostname] = referer;
|
static get = (hostname: string) => this.temporaryUrlReferer.get().then((val) => val[hostname] ?? null);
|
||||||
await this.temporaryUrlReferer.setValue(tmpUrlReferer);
|
|
||||||
}
|
|
||||||
|
|
||||||
static async get(hostname: string) {
|
|
||||||
const tmpUrlReferer = await this.temporaryUrlReferer.getValue();
|
|
||||||
|
|
||||||
return tmpUrlReferer[hostname] ?? null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user