-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
76 lines (63 loc) · 2.48 KB
/
popup.js
File metadata and controls
76 lines (63 loc) · 2.48 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
document.getElementById("summarize").addEventListener("click", () => {
const resultDiv = document.getElementById("result");
const summaryType = document.getElementById("summary-type").value;
resultDiv.innerHTML = '<div class="loader"></div>';
chrome.storage.sync.get(["geminiApiKey"], ({ geminiApiKey }) => {
if (!geminiApiKey) {
resultDiv.textContent = "No API key set. Click the gear icon to add one.";
return;
}
chrome.tabs.query({ active: true, currentWindow: true }, ([tab]) => {
chrome.tabs.sendMessage(
tab.id,
{ type: "GET_ARTICLE_TEXT" },
async ({text}) => {
if (!text) {
resultDiv.textContent = "Couldn't extract text from this page.";
return;
}
try {
const summary = await getGeminiSummary(text, summaryType, geminiApiKey);
resultDiv.textContent = summary;
} catch (error) {
resultDiv.textContent = "Gemini error: " + error.message;
}
}
);
});
});
});
async function getGeminiSummary(rawText, type, apiKey) {
const max = 20000;
const text = rawText.length > max ? rawText.slice(0, max) + "..." : rawText;
const promptMap = {
brief: `Summarize in 2-3 sentences:\n\n${text}`,
detailed: `Give a detailed summary:\n\n${text}`,
bullets: `Summarize in 5-7 bullet points (start each line with "-> "):\n\n${text}`,
};
const prompt = promptMap[type] || promptMap.brief;
const res = await fetch(`https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=${apiKey}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
contents: [{ parts: [{ text: prompt }] }],
generationConfig: { temperature: 0.2 },
}),
});
if (!res.ok) {
const { error } = await res.json();
throw new Error(error?.message || "Request failed");
}
const data = await res.json();
return data.candidates?.[0]?.content?.parts?.[0]?.text ?? "No summary.";
}
document.getElementById("copy-btn").addEventListener("click",()=>{
const txt=document.getElementById("result").innerText
if(!txt) return
navigator.clipboard.writeText(txt).then(()=>{
const btn=document.getElementById("copy-btn")
const old=btn.textContent
btn.textContent="Copied!"
setTimeout(()=>(btn.textContent=old),2000)
})
})