Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
256 lines (218 loc) · 7.2 KB
/
Copy pathapp.js
File metadata and controls
256 lines (218 loc) · 7.2 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// app.js
const API_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent";
// UI Elements
const setupView = document.getElementById('setup-view');
const chatView = document.getElementById('chat-view');
const apiKeyInput = document.getElementById('api-key-input');
const saveKeyBtn = document.getElementById('save-key-btn');
const summarizeBtn = document.getElementById('summarize-btn');
const summaryOutput = document.getElementById('summary-output');
const chatInput = document.getElementById('chat-input');
const sendBtn = document.getElementById('send-btn');
const chatHistory = document.getElementById('chat-history');
const pageTitleEl = document.getElementById('page-title');
const apiStatusDot = document.getElementById('api-status');
const apiStatusText = document.getElementById('api-status-text');
const settingsBtn = document.getElementById('settings-btn');
const tabBtns = document.querySelectorAll('.tab-btn');
const tabPanels = document.querySelectorAll('.tab-panel');
let API_KEY = "";
// Initialize
async function init() {
const data = await chrome.storage.local.get(['gemini_api_key']);
if (data.gemini_api_key) {
API_KEY = data.gemini_api_key;
showView('chat');
updateStatus(true, "Gemini 2.5 Flash Ready");
} else {
showView('setup');
updateStatus(false, "API Key Required");
}
// Detect active tab info
updatePageInfo();
}
function showView(view) {
if (view === 'setup') {
setupView.classList.remove('hidden');
chatView.classList.add('hidden');
} else {
setupView.classList.add('hidden');
chatView.classList.remove('hidden');
}
}
function updateStatus(ready, text) {
if (ready) {
apiStatusDot.classList.remove('error');
apiStatusText.innerText = "Gemini 2.5 Flash Ready";
} else {
apiStatusDot.classList.add('error');
apiStatusText.innerText = text || "Error";
}
}
async function updatePageInfo() {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (tab) {
pageTitleEl.innerText = tab.title || "Unknown Page";
}
}
// Tab Switching
tabBtns.forEach(btn => {
btn.addEventListener('click', () => {
tabBtns.forEach(b => b.classList.remove('active'));
tabPanels.forEach(p => p.classList.remove('active'));
btn.classList.add('active');
const tabId = btn.getAttribute('data-tab');
document.getElementById(`${tabId}-panel`).classList.add('active');
});
});
// API Key Logic
saveKeyBtn.addEventListener('click', async () => {
const key = apiKeyInput.value.trim();
if (key) {
await chrome.storage.local.set({ gemini_api_key: key });
API_KEY = key;
showView('chat');
updateStatus(true);
}
});
settingsBtn.addEventListener('click', () => {
showView('setup');
});
// Content Extraction Helper
async function getPageContext() {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (!tab) return null;
try {
const response = await chrome.tabs.sendMessage(tab.id, { action: "getPageContent" });
return response;
} catch (err) {
console.error("Content extraction failed:", err);
return null;
}
}
// Gemini API Wrapper
async function callGemini(prompt) {
const url = `${API_URL}?key=${API_KEY}`;
const body = {
contents: [
{
parts: [{ text: prompt }]
}
],
generationConfig: {
temperature: 0.7,
topK: 40,
topP: 0.95,
maxOutputTokens: 2048,
}
};
try {
const response = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body)
});
const data = await response.json();
if (data.error) throw new Error(data.error.message);
return data.candidates[0].content.parts[0].text;
} catch (err) {
console.error("Gemini API Error:", err);
throw err;
}
}
// Summarization Logic
summarizeBtn.addEventListener('click', async () => {
summarizeBtn.disabled = true;
summarizeBtn.innerText = "Analyzing Pulse...";
summaryOutput.classList.remove('empty');
summaryOutput.innerHTML = '<div class="loading">Generating summary with Gemini 2.5 Flash...</div>';
try {
const pageData = await getPageContext();
if (!pageData || !pageData.content) {
throw new Error("Could not read page content. Try refreshing the page.");
}
const prompt = `You are Aura AI, a high-speed research assistant.
Analyze the following web page content and provide a high-level summary.
Use this format:
### ⚡ Pulse Summary
(2-3 sentences max)
### 🔑 Key Takeaways
- (Point 1)
- (Point 2)
- (Point 3)
### 💡 Smart Insights
(Optional interesting observation)
Page Title: ${pageData.title}
URL: ${pageData.url}
Content: ${pageData.content.substring(0, 15000)} // Truncate to stay within limits if necessary
`;
const result = await callGemini(prompt);
summaryOutput.innerHTML = formatAIResponse(result);
} catch (err) {
summaryOutput.innerHTML = `<div class="error-msg">Error: ${err.message}</div>`;
} finally {
summarizeBtn.disabled = false;
summarizeBtn.innerText = "Generate Pulse Summary";
}
});
// Chat Logic
async function handleChat() {
const text = chatInput.value.trim();
if (!text || sendBtn.disabled) return;
addMessage(text, 'user');
chatInput.value = '';
chatInput.style.height = 'auto';
sendBtn.disabled = true;
const loadingMsg = addMessage('Aura is thinking...', 'ai', true);
try {
const pageData = await getPageContext();
let prompt = `User question: ${text}`;
if (pageData && pageData.content) {
prompt = `Context of the web page the user is currently looking at:
Title: ${pageData.title}
Content Snippet: ${pageData.content.substring(0, 10000)}
User Question: ${text}
Respond as Aura AI, helpful and concise.`;
}
const result = await callGemini(prompt);
updateAiMessage(loadingMsg, result);
} catch (err) {
updateAiMessage(loadingMsg, `Sorry, I encountered an error: ${err.message}`);
} finally {
sendBtn.disabled = false;
}
}
sendBtn.addEventListener('click', handleChat);
chatInput.addEventListener('keydown', (e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
handleChat();
}
});
// UI Helpers
function formatAIResponse(text) {
// Simple markdown-to-html converter for cleaner display
return text
.replace(/^### (.*$)/gim, '<h3>$1</h3>')
.replace(/^\- (.*$)/gim, '<li>$1</li>')
.replace(/\n\n/g, '</p><p>')
.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');
}
function addMessage(text, role, isLoading = false) {
const msgDiv = document.createElement('div');
msgDiv.className = `message ${role}`;
msgDiv.innerHTML = `<div class="bubble">${isLoading ? text : formatAIResponse(text)}</div>`;
chatHistory.appendChild(msgDiv);
chatHistory.scrollTop = chatHistory.scrollHeight;
return msgDiv;
}
function updateAiMessage(msgDiv, text) {
msgDiv.querySelector('.bubble').innerHTML = formatAIResponse(text);
chatHistory.scrollTop = chatHistory.scrollHeight;
}
// Auto-expand textarea
chatInput.addEventListener('input', () => {
chatInput.style.height = 'auto';
chatInput.style.height = (chatInput.scrollHeight) + 'px';
});
init();