feat: 支持配置全局网页元素等待
This commit is contained in:
1 parent
5e9f0df57f
commit
bec3d9daa4
8 files changed
+210
-12
No files matched your search
@@ -0,0 +1,45 @@
|
||||
import { app } from "electron";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import type { AppSettings } from "../shared/types";
|
||||
|
||||
export const DEFAULT_APP_SETTINGS: AppSettings = {
|
||||
browserElementWaitMs: 5000
|
||||
};
|
||||
|
||||
let cache: AppSettings | null = null;
|
||||
|
||||
function settingsPath(): string {
|
||||
return path.join(app.getPath("userData"), "settings.json");
|
||||
}
|
||||
|
||||
function normalizeSettings(value: unknown): AppSettings {
|
||||
const source = (value && typeof value === "object" ? value : {}) as Partial<AppSettings>;
|
||||
const wait = Number(source.browserElementWaitMs);
|
||||
return {
|
||||
browserElementWaitMs: Number.isFinite(wait)
|
||||
? Math.min(600000, Math.max(0, Math.round(wait)))
|
||||
: DEFAULT_APP_SETTINGS.browserElementWaitMs
|
||||
};
|
||||
}
|
||||
|
||||
export function loadSettings(): AppSettings {
|
||||
try {
|
||||
cache = normalizeSettings(JSON.parse(fs.readFileSync(settingsPath(), "utf8")));
|
||||
} catch {
|
||||
cache = { ...DEFAULT_APP_SETTINGS };
|
||||
}
|
||||
return cache;
|
||||
}
|
||||
|
||||
export function saveSettings(settings: AppSettings): AppSettings {
|
||||
const normalized = normalizeSettings(settings);
|
||||
fs.mkdirSync(app.getPath("userData"), { recursive: true });
|
||||
fs.writeFileSync(settingsPath(), JSON.stringify(normalized, null, 2), "utf8");
|
||||
cache = normalized;
|
||||
return normalized;
|
||||
}
|
||||
|
||||
export function updateSettings(patch: Partial<AppSettings> = {}): AppSettings {
|
||||
return saveSettings({ ...(cache || loadSettings()), ...patch });
|
||||
}
|
||||
Reference in new issue
Block a user