-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.js
More file actions
89 lines (73 loc) · 2.49 KB
/
options.js
File metadata and controls
89 lines (73 loc) · 2.49 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const MODEL_OPTIONS = {
openai: ["gpt-5.4", "gpt-5.3-codex", "gpt-5.2-codex", "gpt-4o-mini"],
glm: ["glm-5", "glm-4.7", "glm-4.7-flash", "glm-4-plus"]
};
const DEFAULT_PROVIDER = "openai";
const els = {
provider: document.getElementById("provider"),
model: document.getElementById("model"),
customModel: document.getElementById("customModel"),
apiKey: document.getElementById("apiKey"),
saveBtn: document.getElementById("saveBtn"),
status: document.getElementById("status")
};
init().catch((error) => setStatus(error.message || "初始化失败", true));
async function init() {
fillProviderOptions();
bindEvents();
await loadConfig();
}
function bindEvents() {
els.provider.addEventListener("change", () => {
fillModelOptions(els.provider.value);
clearStatus();
});
els.saveBtn.addEventListener("click", onSave);
}
function fillProviderOptions() {
els.provider.innerHTML = Object.keys(MODEL_OPTIONS)
.map((provider) => `<option value="${provider}">${provider.toUpperCase()}</option>`)
.join("");
}
function fillModelOptions(provider, selectedModel = "") {
const options = MODEL_OPTIONS[provider] || [];
els.model.innerHTML = options.map((model) => `<option value="${model}">${model}</option>`).join("");
if (selectedModel && options.includes(selectedModel)) {
els.model.value = selectedModel;
}
}
async function loadConfig() {
const { config } = await chrome.storage.local.get("config");
const provider = config?.provider || DEFAULT_PROVIDER;
const customModel = config?.custom_model || "";
const selectedModel = config?.model || MODEL_OPTIONS[provider][0];
els.provider.value = provider;
fillModelOptions(provider, selectedModel);
els.customModel.value = customModel;
els.apiKey.value = config?.api_key || "";
}
async function onSave() {
const provider = els.provider.value;
const dropdownModel = els.model.value;
const customModel = els.customModel.value.trim();
const api_key = els.apiKey.value.trim();
const model = customModel || dropdownModel;
await chrome.storage.local.set({
config: {
provider,
model,
custom_model: customModel,
api_key
}
});
setStatus("设置已保存", false, true);
}
function clearStatus() {
els.status.textContent = "";
els.status.classList.remove("ok", "error");
}
function setStatus(message, isError = false, isOk = false) {
els.status.textContent = message;
els.status.classList.toggle("error", Boolean(isError));
els.status.classList.toggle("ok", Boolean(isOk));
}