-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
80 lines (70 loc) · 2.53 KB
/
Copy pathscript.js
File metadata and controls
80 lines (70 loc) · 2.53 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
// DOM elements
const summarizeBtn = document.getElementById("summarizeBtn");
const inputText = document.getElementById("inputText");
const summaryOutput = document.getElementById("summaryOutput");
const copyBtn = document.getElementById("copyBtn");
const statusMessage = document.getElementById("statusMessage");
// Check if Chrome Built-in AI is available
async function isAIAvailable() {
return ("ai" in window) && typeof window.ai.createTextSession === "function";
}
// Dummy fallback summary (for browsers without Gemini Nano)
async function getDummySummary(text) {
return new Promise(resolve => {
setTimeout(() => {
resolve("🔹 Dummy summary (for demo purposes): " + text.slice(0, 100) + "...");
}, 600);
});
}
// Main summarization function
async function getSummary(text) {
if (await isAIAvailable()) {
try {
const session = await window.ai.createTextSession();
const prompt = `Summarize the following text in 3–5 sentences:\n\n${text}`;
const summary = await session.prompt(prompt);
return "🧠 (Chrome AI) " + summary;
} catch (err) {
console.error("Error using Chrome AI:", err);
return "❌ Error using Chrome AI. Using dummy summary instead:\n\n" + (await getDummySummary(text));
}
} else {
return getDummySummary(text);
}
}
// Handle summarize button click
summarizeBtn.addEventListener("click", async () => {
const text = inputText.value.trim();
if (!text) {
summaryOutput.innerText = "Please enter some text to summarize.";
return;
}
summaryOutput.innerText = "Summarizing...";
statusMessage.innerText = "";
const aiAvailable = await isAIAvailable();
statusMessage.innerText = aiAvailable
? "✅ Using Chrome’s Built-in AI (Gemini Nano)"
: "⚠️ Chrome AI not available — using fallback summarizer.";
const summary = await getSummary(text);
summaryOutput.innerText = summary;
});
// Handle copy button
copyBtn.addEventListener("click", () => {
const text = summaryOutput.innerText.trim();
if (!text) return;
navigator.clipboard.writeText(text).then(() => {
copyBtn.innerText = "Copied!";
setTimeout(() => {
copyBtn.innerText = "Copy Summary";
}, 1500);
}).catch(err => {
console.error("Failed to copy text:", err);
});
});
// On page load, show AI availability
window.addEventListener("load", async () => {
const aiAvailable = await isAIAvailable();
statusMessage.innerText = aiAvailable
? "✅ Chrome AI ready (Gemini Nano detected)"
: "⚠️ Chrome AI not detected — using fallback mode.";
});