-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
367 lines (306 loc) · 11.4 KB
/
popup.js
File metadata and controls
367 lines (306 loc) · 11.4 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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// 扩展弹窗脚本 - 支持Gemini API功能
// 页面加载完成后执行
document.addEventListener('DOMContentLoaded', () => {
// 加载保存的设置
loadSettings();
// 初始化复制按钮为禁用状态
const copyBtn = document.getElementById('copyBtn');
copyBtn.disabled = true;
// 绑定总结按钮点击事件
const summaryBtn = document.getElementById('summaryBtn');
summaryBtn.addEventListener('click', () => {
summarizeArticle();
});
// 绑定设置变化事件
const summaryLength = document.getElementById('summaryLength');
const summaryLanguage = document.getElementById('summaryLanguage');
const apiKey = document.getElementById('apiKey');
const modelSelect = document.getElementById('modelSelect');
const sourceRadios = document.querySelectorAll('input[name="source"]');
summaryLength.addEventListener('change', saveSettings);
summaryLanguage.addEventListener('change', saveSettings);
modelSelect.addEventListener('change', saveSettings);
// 绑定来源选择事件
sourceRadios.forEach(radio => {
radio.addEventListener('change', saveSettings);
});
// 绑定复制按钮事件
copyBtn.addEventListener('click', copySummary);
// 当 API Key 变化时,自动加载可用模型
apiKey.addEventListener('change', async () => {
saveSettings();
await loadAvailableModels();
});
});
// 加载可用的模型列表
async function loadAvailableModels() {
const apiKey = document.getElementById('apiKey').value;
const modelSelect = document.getElementById('modelSelect');
if (!apiKey) {
modelSelect.innerHTML = '<option value="">请先输入 API Key</option>';
return;
}
try {
const response = await fetch(`https://generativelanguage.googleapis.com/v1beta/models?key=${apiKey}`);
if (!response.ok) {
const errorData = await response.json();
console.error('获取模型列表失败:', errorData);
modelSelect.innerHTML = '<option value="">获取模型列表失败</option>';
return;
}
const data = await response.json();
if (!data.models) {
modelSelect.innerHTML = '<option value="">没有可用模型</option>';
return;
}
// 筛选支持 generateContent 的模型
const supportedModels = data.models
.filter(model =>
model.displayName.includes('Gemini') &&
model.supportedGenerationMethods &&
model.supportedGenerationMethods.includes('generateContent')
)
.sort((a, b) => (a.displayName).localeCompare(b.displayName));
if (supportedModels.length === 0) {
modelSelect.innerHTML = '<option value="">没有支持 generateContent 的模型</option>';
return;
}
// 清空现有选项
modelSelect.innerHTML = '';
// 添加模型选项
supportedModels.forEach(model => {
const option = document.createElement('option');
option.value = model.name;
option.textContent = model.displayName;
modelSelect.appendChild(option);
});
// 尝试恢复之前选择的模型
const savedSettings = await chrome.storage.local.get(['selectedModel']);
if (savedSettings.selectedModel) {
const matchingOption = Array.from(modelSelect.options).find(opt => opt.value === savedSettings.selectedModel);
if (matchingOption) {
modelSelect.value = savedSettings.selectedModel;
}
}
} catch (error) {
console.error('加载模型列表失败:', error);
modelSelect.innerHTML = '<option value="">加载失败</option>';
}
}
// 使用Gemini API生成文章总结
async function generateGeminiSummary(content, settings) {
const { apiKey, summaryLength = 'medium', summaryLanguage = 'zh-CN', selectedModel } = settings || {};
if (!apiKey) {
throw new Error('请先在设置中配置Gemini API Key');
}
if (!selectedModel) {
throw new Error('请选择一个模型');
}
const lengthPrompts = {
'short': '请用简短的语言(50-100字)总结以下文章的核心要点',
'medium': '请用中等长度(100-200字)总结以下文章的主要内容',
'long': '请用详细的语言(200-300字)总结以下文章的详细内容'
};
const prompt = `${lengthPrompts[summaryLength]},使用${summaryLanguage === 'zh-CN' ? '中文' : '英文'}回答:\n\n${content}`;
try {
const response = await fetch(`https://generativelanguage.googleapis.com/v1beta/${selectedModel}:generateContent?key=${apiKey}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
contents: [{
parts: [{
text: prompt
}]
}],
generationConfig: {
temperature: 0.7,
topK: 40,
topP: 0.95,
maxOutputTokens: 1024
}
})
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(`Gemini API错误: ${errorData.error?.message || response.statusText}`);
}
const data = await response.json();
if (!data.candidates || data.candidates.length === 0) {
throw new Error('Gemini API未返回结果');
}
const summary = data.candidates[0].content.parts[0].text.trim();
if (!summary) {
throw new Error('Gemini API返回空结果');
}
return summary;
} catch (error) {
console.error('Gemini API调用失败:', error);
throw error;
}
}
// 传统总结方法(备用方案)
function generateTraditionalSummary(content, settings) {
const { summaryLength = 'medium' } = settings || {};
const lengthMap = {
'short': 2,
'medium': 4,
'long': 6
};
const sentenceCount = lengthMap[summaryLength] || 4;
const sentences = content.split(/[.!?。!?]+/).filter(s => s.trim().length > 10);
if (sentences.length === 0) {
return '内容过短,无法生成有效总结';
}
const summarySentences = sentences.slice(0, sentenceCount);
return summarySentences.join('. ') + '.';
}
// 加载设置
function loadSettings() {
chrome.storage.local.get(['summaryLength', 'summaryLanguage', 'apiKey', 'selectedModel', 'selectedSource'], async (result) => {
if (result.summaryLength) {
document.getElementById('summaryLength').value = result.summaryLength;
}
if (result.summaryLanguage) {
document.getElementById('summaryLanguage').value = result.summaryLanguage;
}
if (result.apiKey) {
document.getElementById('apiKey').value = result.apiKey;
// 如果有 API Key,加载可用模型
await loadAvailableModels();
}
if (result.selectedSource) {
const sourceRadio = document.querySelector(`input[name="source"][value="${result.selectedSource}"]`);
if (sourceRadio) {
sourceRadio.checked = true;
}
}
});
}
// 保存设置
function saveSettings() {
const summaryLength = document.getElementById('summaryLength').value;
const summaryLanguage = document.getElementById('summaryLanguage').value;
const apiKey = document.getElementById('apiKey').value;
const selectedModel = document.getElementById('modelSelect').value;
const selectedSource = document.querySelector('input[name="source"]:checked')?.value || '一群';
chrome.storage.local.set({
summaryLength: summaryLength,
summaryLanguage: summaryLanguage,
apiKey: apiKey,
selectedModel: selectedModel,
selectedSource: selectedSource
});
}
// 总结文章
async function summarizeArticle() {
const summaryBtn = document.getElementById('summaryBtn');
const loading = document.getElementById('loading');
const summaryTextarea = document.getElementById('summaryTextarea');
const copyBtn = document.getElementById('copyBtn');
// 显示加载状态
summaryBtn.disabled = true;
loading.style.display = 'block';
summaryTextarea.value = '';
copyBtn.disabled = true;
try {
// 获取当前活动标签页
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
// 使用 scripting API 注入脚本提取网页内容
const injectionResults = await chrome.scripting.executeScript({
target: { tabId: tab.id },
func: extractPageContent
});
const content = injectionResults[0]?.result;
if (content) {
// 获取当前设置
const settings = await chrome.storage.local.get(['summaryLength', 'summaryLanguage', 'apiKey', 'selectedModel', 'selectedSource']);
try {
// 直接调用Gemini API生成总结
const summary = await generateGeminiSummary(content, settings);
// 获取模型显示名称
const modelSelect = document.getElementById('modelSelect');
const selectedOption = modelSelect.options[modelSelect.selectedIndex];
const modelName = selectedOption ? selectedOption.textContent : settings.selectedModel;
// 格式化输出结果
const formattedSummary = formatSummary(summary, tab.title, tab.url, settings.selectedSource || '一群', modelName);
summaryTextarea.value = formattedSummary;
copyBtn.disabled = false;
} catch (error) {
console.error('Gemini API总结失败:', error);
// 使用备用总结方法
const fallbackSummary = generateTraditionalSummary(content, settings);
const formattedSummary = formatSummary(fallbackSummary, tab.title, tab.url, settings.selectedSource || '一群', '');
summaryTextarea.value = `${error.message}\n\n${formattedSummary}`;
copyBtn.disabled = false;
}
} else {
summaryTextarea.value = '无法获取网页内容,请重试';
}
} catch (error) {
console.error('总结失败:', error);
summaryTextarea.value = `总结失败: ${error.message}`;
} finally {
// 隐藏加载状态
summaryBtn.disabled = false;
loading.style.display = 'none';
}
}
// 格式化总结输出
function formatSummary(summary, title, url, source, modelName) {
const modelInfo = modelName ? `[使用 ${modelName} 总结]` : '';
return `🔊 转发 ${source} 成员的原创技术文章
${title}
${url}
${summary}
${modelInfo}`;
}
// 复制总结内容
async function copySummary() {
const summaryTextarea = document.getElementById('summaryTextarea');
const copyBtn = document.getElementById('copyBtn');
try {
await navigator.clipboard.writeText(summaryTextarea.value);
// 显示复制成功提示
const originalText = copyBtn.textContent;
copyBtn.textContent = '复制成功!';
copyBtn.style.backgroundColor = '#137333';
setTimeout(() => {
copyBtn.textContent = originalText;
copyBtn.style.backgroundColor = '#34a853';
}, 2000);
} catch (error) {
console.error('复制失败:', error);
alert('复制失败,请手动复制');
}
}
// 提取文章内容
function extractPageContent() {
let mainContent = '';
// 尝试使用常见的文章容器选择器
const articleSelectors = [
'article',
'.article',
'.post',
'.content',
'.main-content',
'#main-content',
'.entry-content',
'.post-content',
'#page-content',
'.article-content',
'.story-content',
'.text-content'
];
for (const selector of articleSelectors) {
const element = document.querySelector(selector);
if (element) {
mainContent = element.textContent.trim();
if (mainContent.length > 200) {
break;
}
}
}
return mainContent || '无法提取到有效的内容';
}