-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.js
More file actions
61 lines (54 loc) · 2.43 KB
/
Copy pathoptions.js
File metadata and controls
61 lines (54 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const DEFAULT_SETTINGS = {
v2exApiBaseUrl: "https://www.v2ex.com/api",
baseUrl: "https://api.openai.com/v1",
apiKey: "",
model: "gpt-4o-mini",
temperature: 0.2,
maxViewpoints: 5,
minRawCommentsThreshold: 10
};
async function restore() {
const settings = await chrome.storage.sync.get(DEFAULT_SETTINGS);
document.getElementById("v2exApiBaseUrl").value = settings.v2exApiBaseUrl;
document.getElementById("baseUrl").value = settings.baseUrl;
document.getElementById("apiKey").value = settings.apiKey;
document.getElementById("model").value = settings.model;
document.getElementById("temperature").value = settings.temperature;
document.getElementById("maxViewpoints").value = settings.maxViewpoints;
document.getElementById("minRawCommentsThreshold").value = settings.minRawCommentsThreshold;
}
async function save() {
const status = document.getElementById("status");
const nextSettings = {
v2exApiBaseUrl:
document.getElementById("v2exApiBaseUrl").value.trim() || DEFAULT_SETTINGS.v2exApiBaseUrl,
baseUrl: document.getElementById("baseUrl").value.trim() || DEFAULT_SETTINGS.baseUrl,
apiKey: document.getElementById("apiKey").value.trim(),
model: document.getElementById("model").value.trim() || DEFAULT_SETTINGS.model,
temperature: Number(document.getElementById("temperature").value || DEFAULT_SETTINGS.temperature),
maxViewpoints: Number(document.getElementById("maxViewpoints").value || DEFAULT_SETTINGS.maxViewpoints),
minRawCommentsThreshold: Number(
document.getElementById("minRawCommentsThreshold").value || DEFAULT_SETTINGS.minRawCommentsThreshold
)
};
if (nextSettings.temperature < 0 || nextSettings.temperature > 2) {
status.textContent = "temperature 必须在 0~2 之间";
status.style.color = "#b91c1c";
return;
}
if (nextSettings.maxViewpoints < 1 || nextSettings.maxViewpoints > 12) {
status.textContent = "最大观点数量必须在 1~12 之间";
status.style.color = "#b91c1c";
return;
}
if (nextSettings.minRawCommentsThreshold < 0 || nextSettings.minRawCommentsThreshold > 500) {
status.textContent = "分析阈值必须在 0~500 之间";
status.style.color = "#b91c1c";
return;
}
await chrome.storage.sync.set(nextSettings);
status.textContent = "设置已保存。";
status.style.color = "#065f46";
}
document.getElementById("saveBtn").addEventListener("click", save);
document.addEventListener("DOMContentLoaded", restore);