-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtablist.js
319 lines (272 loc) · 11.7 KB
/
tablist.js
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
document.addEventListener('DOMContentLoaded', () => {
const tabList = document.getElementById('tabList');
const summarizeButton = document.getElementById('summarizeButton');
const summaryDiv = document.getElementById('summary');
const apiKeyInput = document.getElementById('apiKeyInput');
const themeToggle = document.getElementById('themeToggle');
const summarizeAllButton = document.getElementById('summarizeAllButton');
summarizeAllButton.addEventListener('click', summarizeAllTabs);
if (summarizeAllButton) {
summarizeAllButton.addEventListener('click', summarizeAllTabs);
} else {
console.error('Summarize All button not found');
}
async function summarizeAllTabs() {
const apiKey = apiKeyInput.value.trim();
if (!apiKey) {
alert("Please enter your Google API key.");
return;
}
if (summaryDiv) {
summarizeAllButton.disabled = true;
summaryDiv.textContent = "Generating summaries...";
} else {
console.error('Summary div not found');
return;
}
try {
// Step 1: Summarize each page asynchronously
// const summaryPromises = savedTabs.map(async (tab, index) => {
// const content = await fetchContent(tab.url);
// const summary = await generateSummary(content, apiKey);
// savedTabs[index].summary = summary;
// renderTabs(); // Update UI with individual summaries
// return summary;
// });
const existingSummaries = savedTabs.map(tab => tab.summary).filter(summary => summary);
// const summaries = await Promise.all(summaryPromises);
// Step 2: Summarize the summaries
const finalSummary = await generateSummary(existingSummaries.join("\n\n"), apiKey);
// Render the final summary as Markdown
summaryDiv.innerHTML = marked.parse(finalSummary);
// Save the updated tabs with summaries
chrome.storage.local.set({ savedTabs: savedTabs });
} catch (error) {
console.error("Error generating summaries:", error);
if (summaryDiv) {
summaryDiv.textContent = "Error generating summaries. Please try again.";
}
} finally {
if (summarizeAllButton) {
summarizeAllButton.disabled = false;
}
}
}
if (themeToggle) {
themeToggle.addEventListener('click', () => {
document.body.classList.toggle('dark-theme');
// Save the theme preference
const isDarkTheme = document.body.classList.contains('dark-theme');
localStorage.setItem('dark-theme', isDarkTheme);
// Toggle the "Summarize All" button class
const summarizeAllButton = document.getElementById('summarizeAllButton');
summarizeAllButton.classList.toggle('dark-theme');
});
// Load saved theme preference
const savedTheme = localStorage.getItem('dark-theme');
if (savedTheme === 'true') {
document.body.classList.add('dark-theme');
const summarizeAllButton = document.getElementById('summarizeAllButton');
summarizeAllButton.classList.add('dark-theme');
}
} else {
console.error('Theme toggle button not found');
}
let savedTabs = [];
function loadTabs() {
chrome.storage.local.get("savedTabs", (data) => {
if (data.savedTabs && data.savedTabs.length > 0) {
savedTabs = data.savedTabs;
renderTabs();
} else {
const li = document.createElement('li');
li.textContent = "No tabs gathered.";
tabList.appendChild(li);
}
});
}
function renderTabs() {
tabList.innerHTML = ''; // Clear existing list
savedTabs.forEach((tab, index) => {
const li = document.createElement('li');
const a = document.createElement('a');
a.href = tab.url;
a.textContent = tab.title || tab.url;
a.target = "_blank";
const deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
deleteButton.className = 'deleteButton';
deleteButton.addEventListener('click', () => deleteTab(index));
const summarizeButton = document.createElement('button');
summarizeButton.textContent = 'Summarize';
summarizeButton.className = 'summarizeButton';
summarizeButton.addEventListener('click', () => summarizeTab(index));
const chatButton = document.createElement('button');
chatButton.textContent = 'Chat';
chatButton.className = 'chatButton';
chatButton.addEventListener('click', () => openChatbot(index));
const summarySpan = document.createElement('span');
summarySpan.className = 'tabSummary';
summarySpan.innerHTML = tab.summary ? marked.parse(tab.summary) : '';
li.appendChild(a);
li.appendChild(deleteButton);
li.appendChild(summarizeButton);
li.appendChild(chatButton);
li.appendChild(summarySpan);
tabList.appendChild(li);
});
}
function openChatbot(index) {
const tab = savedTabs[index];
const chatUrl = chrome.runtime.getURL('chatbot.html') +
`?summary=${encodeURIComponent(tab.summary || '')}&url=${encodeURIComponent(tab.url)}`;
chrome.tabs.create({ url: chatUrl });
}
function deleteTab(index) {
savedTabs.splice(index, 1);
chrome.storage.local.set({ savedTabs: savedTabs }, () => {
renderTabs();
});
}
async function summarizeTab(index) {
const apiKey = apiKeyInput.value.trim();
if (!apiKey) {
alert("Please enter your Google API key.");
return;
}
const tab = savedTabs[index];
const content = await fetchContent(tab.url);
const summary = await generateSummary(content, apiKey);
savedTabs[index].summary = summary;
chrome.storage.local.set({ savedTabs: savedTabs }, () => {
renderTabs();
});
}
loadTabs();
summarizeButton.addEventListener('click', async () => {
const apiKey = apiKeyInput.value.trim();
if (!apiKey) {
alert("Please enter your Google API key.");
return;
}
summarizeButton.disabled = true;
summaryDiv.textContent = "Generating summaries...";
try {
// Step 1: Summarize each page asynchronously
const summaryPromises = savedTabs.map(async (tab, index) => {
const content = await fetchContent(tab.url);
const summary = await generateSummary(content, apiKey);
savedTabs[index].summary = summary;
renderTabs(); // Update UI with individual summaries
return summary;
});
const summaries = await Promise.all(summaryPromises);
// Step 2: Summarize the summaries
const finalSummary = await generateSummary(summaries.join("\n\n"), apiKey);
summaryDiv.textContent = "Final Summary:\n" + finalSummary;
// Save the updated tabs with summaries
chrome.storage.local.set({ savedTabs: savedTabs });
} catch (error) {
console.error("Error generating summaries:", error);
summaryDiv.textContent = "Error generating summaries. Please try again.";
} finally {
summarizeButton.disabled = false;
}
});
});
async function fetchContent(url) {
if (url.startsWith('chrome://')) {
return `Unable to fetch content from ${url} due to browser restrictions.`;
}
try {
const response = await fetch(url);
const html = await response.text();
const parser = new DOMParser();
const doc = parser.parseFromString(html, 'text/html');
return doc.body.innerText;
} catch (error) {
console.error(`Error fetching content from ${url}:`, error);
return `Error fetching content from ${url}`;
}
}
async function generateSummary(content, apiKey, retries = 10) {
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
const apiUrl = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent';
for (let i = 0; i < retries; i++) {
try {
const response = await fetch(`${apiUrl}?key=${apiKey}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
contents: [{
parts: [{
text: `Summarize the following content like an expert in the field and make it concise and step by step using solid logic structure and format:\n\n${content}`
}]
}],
generationConfig: {
temperature: 0.7,
topK: 40,
topP: 0.95,
maxOutputTokens: 500,
},
})
});
if (response.status === 429) {
console.log(`Rate limit hit, retrying in ${2 ** i} seconds...`);
await delay(2 ** i * 1000); // Exponential backoff
continue;
}
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data.candidates[0].content.parts[0].text;
} catch (error) {
if (i === retries - 1) throw error; // Throw on last retry
console.error("Error calling Gemini API:", error);
}
}
throw new Error("Max retries reached. Please try again later.");
}
// async function summarizeAllTabs() {
// const apiKey = apiKeyInput.value.trim();
// if (!apiKey) {
// alert("Please enter your Google API key.");
// return;
// }
// if (summaryDiv) {
// summarizeAllButton.disabled = true;
// summaryDiv.textContent = "Generating summaries...";
// } else {
// console.error('Summary div not found');
// return;
// }
// try {
// // Step 1: Summarize each page asynchronously
// const summaryPromises = savedTabs.map(async (tab, index) => {
// const content = await fetchContent(tab.url);
// const summary = await generateSummary(content, apiKey);
// savedTabs[index].summary = summary;
// renderTabs(); // Update UI with individual summaries
// return summary;
// });
// const summaries = await Promise.all(summaryPromises);
// // Step 2: Summarize the summaries
// const finalSummary = await generateSummary(summaries.join("\n\n"), apiKey);
// // Render the final summary as Markdown
// summaryDiv.innerHTML = marked.parse(finalSummary);
// // Save the updated tabs with summaries
// chrome.storage.local.set({ savedTabs: savedTabs });
// } catch (error) {
// console.error("Error generating summaries:", error);
// if (summaryDiv) {
// summaryDiv.textContent = "Error generating summaries. Please try again.";
// }
// } finally {
// if (summarizeAllButton) {
// summarizeAllButton.disabled = false;
// }
// }
// }