-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptionsProc.js
More file actions
114 lines (98 loc) · 4.04 KB
/
optionsProc.js
File metadata and controls
114 lines (98 loc) · 4.04 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { GoogleGenerativeAI } from "./geminiAPI.js";
document.getElementById("APIKey").addEventListener("input", () => getAPIInput("APIKey", "inpStatus"), true);
document.getElementById("APIKey2").addEventListener("input", () => getAPIInput("APIKey2", "inpStatus2"), true);
document.getElementById("clearKeys").addEventListener("click", clearKeys, true);
let inputStatus = document.getElementById("inpStatus");
let inputStatus2 = document.getElementById("inpStatus2");
let infoText = document.getElementById("optionInfo");
async function getAPIInput(inputId, statusId) {
const APIKey_input = document.getElementById(inputId);
const statusElement = document.getElementById(statusId);
if(APIKey_input.value.length == 0) {
statusElement.innerHTML = null;
return;
}
const avalue = APIKey_input.value;
const tgenAI = new GoogleGenerativeAI(avalue);
const tmodel = tgenAI.getGenerativeModel({ model: "gemini-3.1-flash-lite-preview" });
let test = null;
try {
test = await tmodel.generateContent("Test");
}
catch(error) {
showError(statusElement);
return;
}
await obtainAPI(avalue, inputId);
}
function showError(statusElement) {
statusElement.innerHTML = "error";
if(statusElement.classList.contains("text-success")) {
statusElement.classList.replace("text-success", "text-danger");
}
}
async function obtainAPI(apiv, inputId) {
// Store the API key in the appropriate slot
if (inputId === "APIKey") {
await chrome.storage.local.set({ 'userGAPI': apiv });
} else {
// If the first API key isn't set, also set it with this value
const result = await chrome.storage.local.get('userGAPI');
if (!result.userGAPI) {
await chrome.storage.local.set({ 'userGAPI': apiv });
} else {
await chrome.storage.local.set({ 'userGAPI2': apiv });
}
}
preserveOptions();
chrome.runtime.reload();
}
async function clearKeys() {
await chrome.storage.local.remove(['userGAPI', 'userGAPI2']);
const APIKey_input = document.getElementById("APIKey");
APIKey_input.removeAttribute("disabled");
APIKey_input.setAttribute("placeholder", "API Key 1");
APIKey_input.value = "";
inputStatus.innerHTML = "";
inputStatus.classList.replace("text-success", "text-danger");
const APIKey_input2 = document.getElementById("APIKey2");
APIKey_input2.removeAttribute("disabled");
APIKey_input2.setAttribute("placeholder", "API Key 2 (optional)");
APIKey_input2.value = "";
inputStatus2.innerHTML = "";
inputStatus2.classList.replace("text-success", "text-danger");
chrome.runtime.reload();
}
async function preserveOptions() {
try {
// Handle first API key
const { userGAPI } = await chrome.storage.local.get('userGAPI');
if(userGAPI !== undefined) {
const APIKey_input = document.getElementById("APIKey");
APIKey_input.setAttribute("placeholder", userGAPI);
APIKey_input.setAttribute("disabled", true);
inputStatus.innerHTML = "check_circle";
inputStatus.classList.replace("text-danger", "text-success");
}
// Handle second API key
const { userGAPI2 } = await chrome.storage.local.get('userGAPI2');
if(userGAPI2 !== undefined) {
const APIKey_input2 = document.getElementById("APIKey2");
APIKey_input2.setAttribute("placeholder", userGAPI2);
APIKey_input2.setAttribute("disabled", true);
inputStatus2.innerHTML = "check_circle";
inputStatus2.classList.replace("text-danger", "text-success");
}
// Update info text if no API keys are set
if(userGAPI === undefined && userGAPI2 === undefined) {
// Clear storage if no API keys are set
await chrome.storage.local.remove(['userGAPI', 'userGAPI2']);
}
} catch (error) {
console.error("Error preserving options:", error);
}
}
// Initialize the extension
window.onload = function() {
preserveOptions();
};