-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpopup.js
More file actions
217 lines (197 loc) · 8.29 KB
/
Copy pathpopup.js
File metadata and controls
217 lines (197 loc) · 8.29 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
import { AppState } from './src/state.js';
import { PopupUI} from './src/components/popup/PopupUI.js';
import { SettingsUI } from './src/components/settings/SettingsUI.js';
import { OpenAIService, GeminiService } from './src/services/api.js';
import { captureVisibleTab, extractWebpageContent } from './src/utils/helpers.js';
import { MODEL_LIST } from './src/constants/models.js';
let popupUI, settingsUI;
// Accordion logic for provider settings
function setupProviderAccordion() {
const sections = document.querySelectorAll('.provider-section');
sections.forEach(section => {
const header = section.querySelector('.provider-header');
const body = section.querySelector('.provider-body');
header.addEventListener('click', () => {
const isActive = section.classList.contains('active');
if (isActive) {
section.classList.remove('active');
body.style.display = 'none';
} else {
section.classList.add('active');
body.style.display = 'block';
}
});
// Optionally expand OpenAI by default
if (section.dataset.provider === 'openai') {
section.classList.add('active');
body.style.display = 'block';
}
});
}
// Provider API key state management
const PROVIDERS = ['openai', 'gemini'];
function saveProviderApiKey(provider, key) {
chrome.storage.local.get(['apiKeys'], (result) => {
const apiKeys = result.apiKeys || {};
apiKeys[provider] = key;
chrome.storage.local.set({ apiKeys });
});
}
function loadProviderApiKeys() {
chrome.storage.local.get(['apiKeys'], (result) => {
const apiKeys = result.apiKeys || {};
PROVIDERS.forEach(provider => {
const input = document.getElementById(`${provider}ApiKey`);
if (input) input.value = apiKeys[provider] || '';
});
});
}
document.addEventListener('DOMContentLoaded', async () => {
await AppState.initialize();
popupUI = new PopupUI();
settingsUI = new SettingsUI();
// Subscribe UI components to state changes
AppState.subscribe((state) => {
popupUI.render(state);
settingsUI.render(state);
});
// Initial render
popupUI.render(AppState.state);
settingsUI.render(AppState.state);
// Load saved selections
chrome.storage.local.get(['selectedModel', 'selectedPrompt'], (result) => {
if (result.selectedModel) {
document.getElementById('modelSelect').value = result.selectedModel;
}
if (result.selectedPrompt) {
document.getElementById('promptSelect').value = result.selectedPrompt;
}
});
// Save model selection when changed
document.getElementById('modelSelect').addEventListener('change', (e) => {
chrome.storage.local.set({ selectedModel: e.target.value });
});
// Save prompt selection when changed
document.getElementById('promptSelect').addEventListener('change', (e) => {
chrome.storage.local.set({ selectedPrompt: e.target.value });
});
// Handle assist button click
document.getElementById('assist').addEventListener('click', async () => {
const state = AppState.state;
try {
// Check for selected model and API key
const modelSelect = document.getElementById('modelSelect');
const selectedModel = modelSelect.value;
// Get API keys from storage
const apiKeys = await new Promise(resolve => {
chrome.storage.local.get(['apiKeys'], (result) => resolve(result.apiKeys || {}));
});
const modelObj = MODEL_LIST.find(m => m.id === selectedModel);
if (!selectedModel || !modelObj || !apiKeys[modelObj.provider]) {
popupUI.showError('Please select a model and set its API key in settings');
return;
}
// Use the current textarea value as the prompt
const promptText = document.getElementById('mainCustomPrompt').value.trim();
if (!promptText) {
popupUI.showError('Please enter a prompt');
return;
}
// Add Markdown formatting instruction suffix
const markdownSuffix = "\n\nPlease format your response in Markdown. Use bullet points, numbered lists, code blocks, dividers, and headings where appropriate. Be to the point, highly optimised, and aim to achive 100% correctness in your answer. You are an agent that is trying to help the user with all your knowledge and resources.";
const finalPrompt = promptText + markdownSuffix;
// Show loading state
popupUI.updateLoadingState(true);
// Extract webpage content
let webpageContent;
try {
webpageContent = await extractWebpageContent();
} catch (error) {
console.warn('Failed to extract webpage content:', error);
// Continue without webpage content
}
// Screenshot mode logic
const isDeepContext = AppState.state.isDeepContext;
let screenshotUrl = null;
if (!isDeepContext) {
// Shallow context: only include webpage content, no screenshot
screenshotUrl = null;
} else {
// Deep context: include webpage content + full page screenshot
screenshotUrl = await new Promise((resolve, reject) => {
chrome.runtime.sendMessage({ type: 'START_FULL_PAGE_CAPTURE', showModal: false }, (response) => {
if (response && response.error) {
popupUI.showError(response.error);
reject(new Error(response.error));
}
});
// Listen for the result once
const handler = (message, sender, sendResponse) => {
if (message.type === 'FULL_PAGE_CAPTURE_DONE') {
resolve(message.dataUrl);
chrome.runtime.onMessage.removeListener(handler);
}
};
chrome.runtime.onMessage.addListener(handler);
});
}
// Instantiate the correct LLM service
let llmService;
if (modelObj.provider === 'openai') {
llmService = new OpenAIService(apiKeys[modelObj.provider]);
} else if (modelObj.provider === 'gemini') {
llmService = new GeminiService(apiKeys[modelObj.provider]);
} else {
popupUI.showError('Unsupported provider: ' + modelObj.provider);
return;
}
// Generate content
const resultText = await llmService.generateContent(screenshotUrl, finalPrompt, selectedModel, webpageContent);
// Update UI with results (pass screenshotUrl to updateResult)
popupUI.updateResult(resultText, webpageContent, screenshotUrl);
popupUI.updateScreenshot(screenshotUrl);
popupUI.showSuccess('Assist Completed!');
} catch (error) {
popupUI.showError(error.message || 'Failed to generate result');
} finally {
// Hide loading state
popupUI.updateLoadingState(false);
}
});
document.getElementById('fullPageScreenshotBtn').addEventListener('click', async () => {
chrome.runtime.sendMessage({ type: 'START_FULL_PAGE_CAPTURE', showModal: true }, (response) => {
if (response && response.error) {
alert(response.error);
}
});
});
document.getElementById('closeScreenshotModal').addEventListener('click', () => {
document.getElementById('screenshotModal').classList.remove('show');
document.getElementById('fullPageScreenshotPreview').src = '';
});
// Reset button clears all API keys and custom prompts, and shows a snackbar
document.getElementById('resetPromptsBtn').addEventListener('click', async () => {
if (!confirm('Are you sure you want to reset all API keys and custom prompts?')) return;
await new Promise(resolve => chrome.storage.local.remove(['apiKeys'], resolve));
await new Promise(resolve => chrome.storage.sync.remove(['apiKey', 'customPrompts'], resolve));
// Restore default prompts in storage and state
await AppState.resetPromptsToDefault();
AppState.state.selectedModel = null;
// Feedback
const snackbar = document.getElementById('snackbar');
if (snackbar) {
snackbar.textContent = 'All API keys and custom prompts have been reset.';
snackbar.classList.add('show');
setTimeout(() => snackbar.classList.remove('show'), 2000);
}
});
// Update the existing showResult function to use resultContent
function showResult(content) {
const result = document.getElementById('result');
const resultContent = document.getElementById('resultContent');
result.style.display = 'block';
resultContent.innerHTML = marked.parse(content);
}
setupProviderAccordion();
loadProviderApiKeys();
});