-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.js
More file actions
189 lines (160 loc) · 5.96 KB
/
options.js
File metadata and controls
189 lines (160 loc) · 5.96 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
const log = (msg) => console.log(`[Options] ${msg}`);
const errorLog = (msg, e) => console.error(`[Options ERROR] ${msg}`, e);
const saveButton = document.getElementById('save');
const statusEl = document.getElementById('status');
const apiKeyInput = document.getElementById('api-key');
const bgColorInput = document.getElementById('bg-color');
const textColorInput = document.getElementById('text-color');
const transparencyInput = document.getElementById('transparency');
const widthInput = document.getElementById('window-width');
const heightInput = document.getElementById('window-height');
const previewWindow = document.getElementById('preview-window');
const transparencyLabel = document.getElementById('transparency-label');
const widthLabel = document.getElementById('width-label');
const heightLabel = document.getElementById('height-label');
const memoryListContainer = document.getElementById('memory-list-container');
const refreshMemoriesBtn = document.getElementById('refresh-memories');
const defaults = {
geminiApiKey: '',
bgColor: '#282a36',
textColor: '#f8f8f2',
transparency: 0.95,
windowWidth: 350,
windowHeight: 450
};
function save_options() {
log("Saving options...");
const settings = {
geminiApiKey: apiKeyInput.value,
bgColor: bgColorInput.value,
textColor: textColorInput.value,
transparency: parseFloat(transparencyInput.value),
windowWidth: parseInt(widthInput.value, 10),
windowHeight: parseInt(heightInput.value, 10)
};
chrome.storage.sync.set(settings, () => {
log("Options saved to storage.");
statusEl.textContent = 'Options saved. Check your active tab!';
setTimeout(() => { statusEl.textContent = ''; }, 2000);
});
}
function restore_options() {
log("Restoring options...");
chrome.storage.sync.get(defaults, (items) => {
log("Loaded settings:", items);
apiKeyInput.value = items.geminiApiKey;
bgColorInput.value = items.bgColor;
textColorInput.value = items.textColor;
transparencyInput.value = items.transparency;
widthInput.value = items.windowWidth;
heightInput.value = items.windowHeight;
update_preview();
});
}
function update_preview() {
const bgColor = bgColorInput.value;
const textColor = textColorInput.value;
const transparency = parseFloat(transparencyInput.value);
const width = parseInt(widthInput.value, 10);
const height = parseInt(heightInput.value, 10);
const alpha = Math.round(transparency * 255).toString(16).padStart(2, '0');
previewWindow.style.backgroundColor = `${bgColor}${alpha}`;
previewWindow.style.color = textColor;
previewWindow.style.width = `${width}px`;
previewWindow.style.height = `${height}px`;
transparencyLabel.textContent = `Transparency (${transparency.toFixed(2)}):`;
widthLabel.textContent = `Default Width (${width}px):`;
heightLabel.textContent = `Default Height (${height}px):`;
}
function openDB() {
return new Promise((resolve, reject) => {
const request = indexedDB.open("AI_SecondBrain", 1);
request.onsuccess = () => resolve(request.result);
request.onerror = (e) => reject(e);
});
}
async function loadMemories() {
log("Loading memories from DB...");
try {
const db = await openDB();
const tx = db.transaction("vectors", "readonly");
const store = tx.objectStore("vectors");
const request = store.getAll();
request.onsuccess = () => {
const memories = request.result;
log(`Loaded ${memories.length} memories.`);
renderMemoryList(memories);
};
request.onerror = (e) => {
errorLog("Failed to fetch memories", e.target.error);
memoryListContainer.innerHTML = '<p>Error loading memories.</p>';
};
} catch (e) {
errorLog("DB Open failed in Options", e);
memoryListContainer.innerHTML = '<p>No database found yet. Save something first!</p>';
}
}
function renderMemoryList(memories) {
if (!memories || memories.length === 0) {
memoryListContainer.innerHTML = '<p>No memories saved yet. Highlight text and right-click "Save to Brain".</p>';
return;
}
memories.sort((a, b) => b.id - a.id);
let html = '';
memories.forEach(mem => {
const date = new Date(mem.date || mem.id).toLocaleString();
html += `
<div class="memory-item" id="mem-${mem.id}">
<div class="mem-content">
<div class="mem-text">"${escapeHtml(mem.text)}"</div>
<div class="mem-meta">
<span class="mem-date">📅 ${date}</span>
<a href="${mem.url}" target="_blank" class="mem-link">🔗 Source</a>
</div>
</div>
<button class="delete-btn" data-id="${mem.id}">🗑️</button>
</div>
`;
});
memoryListContainer.innerHTML = html;
document.querySelectorAll('.delete-btn').forEach(btn => {
btn.addEventListener('click', (e) => {
const id = parseInt(e.target.dataset.id);
deleteMemory(id);
});
});
}
async function deleteMemory(id) {
if (!confirm("Are you sure you want to delete this memory?")) return;
log(`Deleting memory ID: ${id}`);
const db = await openDB();
const tx = db.transaction("vectors", "readwrite");
const store = tx.objectStore("vectors");
store.delete(id);
tx.oncomplete = () => {
log("Memory deleted.");
const el = document.getElementById(`mem-${id}`);
if (el) el.remove();
if (document.querySelectorAll('.memory-item').length === 0) {
loadMemories();
}
};
}
function escapeHtml(text) {
if (!text) return "";
return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
}
document.addEventListener('DOMContentLoaded', () => {
restore_options();
loadMemories();
});
saveButton.addEventListener('click', save_options);
if(refreshMemoriesBtn) {
refreshMemoriesBtn.addEventListener('click', () => {
log("Refreshing memory list manually...");
loadMemories();
});
}
[bgColorInput, textColorInput, transparencyInput, widthInput, heightInput].forEach(input => {
input.addEventListener('input', update_preview);
});