Add QdSettings

This commit is contained in:
2026-02-14 10:00:57 +08:00
parent dc252af38a
commit deefa95918
10 changed files with 83 additions and 14 deletions

View File

@@ -35,5 +35,8 @@
"options_ui": {
"open_in_tab": true,
"page": "dist/settings.html"
}
},
"permissions": [
"storage"
]
}

View File

@@ -0,0 +1,12 @@
import { Modal } from "antd";
export type AlertWarnProps = {
title?: string;
content: string;
onClose?: () => void;
okText?: string;
};
export default function AlertWarn({ title = "警告", content, onClose, okText = "好的" }: AlertWarnProps) {
return <Modal title={title} open={true} onOk={onClose} onCancel={onClose} okText={okText} cancelButtonProps={{ style: { display: "none" } }}>{content}</Modal>
}

View File

@@ -0,0 +1,3 @@
.switch-label > span {
cursor: pointer;
}

View File

@@ -0,0 +1,17 @@
import { Switch } from "antd";
import styles from "./SwitchLabel.module.css";
export type SwitchLabelProps = {
label: string;
checked: boolean;
onChange: (checked: boolean) => void;
}
export default function SwitchLabel({ label, checked, onChange }: SwitchLabelProps) {
return (
<div className={styles["switch-label"]}>
<span onClick={() => onChange(!checked)}>{label}</span>
<Switch checked={checked} onChange={onChange} />
</div>
);
}

View File

@@ -18,6 +18,9 @@ export class QdConfig {
}
await saveConfig(QdConfig.STORAGE_KEY, this.config);
}
reset() {
this.config = {};
}
get AutoSaveChapter(): boolean {
return this.config?.AutoSaveChapter ?? false;
}

View File

@@ -45,7 +45,7 @@ export default function QdChapterInfo({ bookInfo, chapterInfo }: QdChapterInfoPr
}
return (
<Space direction="vertical" size="middle" style={{ width: '100%' }}>
<Space orientation="vertical" size="middle" style={{ width: '100%' }}>
{/* 章节信息 */}
<Card title="章节信息" size="small">
<Descriptions column={1} size="small">

View File

@@ -54,7 +54,7 @@ function PopupBody() {
load().catch(e => {
setError(e instanceof Error ? e.message : 'Unknown error');
});
});
}, []);
if (result) {
console.log(result);
if (result.ok && result.body?.type === 'QdChapterInfo') {

View File

@@ -4,7 +4,7 @@
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width,initial-scale=1"/>
<title>扩展设置</title>
<!--<link rel="stylesheet" href="./settings.css"/>-->
<link rel="stylesheet" href="./settings.css"/>
</head>
<body>
<div id="root"></div>

View File

@@ -1,18 +1,49 @@
import { Typography, Switch, FloatButton, Affix, Button } from "antd";
import { SaveTwoTone, SaveOutlined } from "@ant-design/icons";
import { useState } from "react";
const { Title } = Typography;
import { FloatButton, Affix, Button, Space } from "antd";
import { SaveTwoTone, SaveOutlined, SyncOutlined } from "@ant-design/icons";
import { useEffect, useState } from "react";
import { QdConfig } from "../config";
import SwitchLabel from "../components/SwitchLabel";
import AlertWarn from "../components/AlertWarn";
export default function QdSettings() {
const [container, setContainer] = useState<HTMLElement | null>(null);
const [config] = useState(new QdConfig());
const [autoSaveChapter, setAutoSaveChapter] = useState(false);
const [alert, setAlert] = useState<{ title?: string; content: string } | null>(null);
function handleConfig() {
setAutoSaveChapter(config.AutoSaveChapter);
}
useEffect(() => {
config.init().then(() => {
handleConfig();
}).catch(e => {
setAlert({ content: "加载设置失败:" + (e instanceof Error ? e.message : "未知错误"), title: "错误" });
});
}, []);
function saveSettings() {
config.AutoSaveChapter = autoSaveChapter;
config.save().then(() => {
setAlert({ content: "设置已保存!", title: "通知" });
}).catch(e => {
setAlert({ content: e instanceof Error ? e.message : "未知错误", title: "错误" });
});
}
function resetSettings() {
config.reset();
handleConfig();
}
return (
<div ref={setContainer}>
<Title level={2}></Title>
<Affix target={() => container}>
<Button type="primary" icon={<SaveOutlined />}></Button>
<Button type="primary" icon={<SaveOutlined />} onClick={saveSettings}></Button>
<Button onClick={resetSettings} style={{ marginLeft: 8 }} icon={<SyncOutlined />}></Button>
</Affix>
<FloatButton icon={<SaveTwoTone />} tooltip="保存设置" />
<br />
<Space orientation="vertical">
<SwitchLabel label="自动保存章节" checked={autoSaveChapter} onChange={setAutoSaveChapter} />
</Space>
<FloatButton icon={<SaveTwoTone />} tooltip="保存设置" onClick={saveSettings} />
{alert && <AlertWarn title={alert.title} content={alert.content} onClose={() => setAlert(null)} />}
</div>
);
}

View File

@@ -60,11 +60,11 @@ export function saveAsFile(filename: string, content: string, mimeType: string =
}
export async function saveConfig(key: string, config: any): Promise<void> {
return await chrome.storage.managed.set({ [key]: config });
return await chrome.storage.local.set({ [key]: config });
}
export async function loadConfig<T>(key: string, defaultValue: T): Promise<T> {
const r = await chrome.storage.managed.get<{[key]: T | undefined}>(key);
const r = await chrome.storage.local.get<{[key]: T | undefined}>(key);
if (r[key] === undefined) {
return defaultValue;
}