-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
215 lines (190 loc) · 7.01 KB
/
script.js
File metadata and controls
215 lines (190 loc) · 7.01 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
const video = document.getElementById("webcam");
const captureBtn = document.getElementById("capture");
const retakeBtn = document.getElementById("retake");
const capturedImage = document.getElementById("capturedImage");
const extractedText = document.getElementById("extractedText");
const searchText = document.getElementById("searchText");
const searchBtn = document.getElementById("search");
const darkModeToggle = document.getElementById("darkModeToggle");
const copyClipboardBtn = document.getElementById("copyClipboard");
const toggleCameraBtn = document.getElementById("toggleCamera");
const aiSummaryBtn = document.getElementById("aiSummaryBtn");
const aiSummarySection = document.getElementById("aiSummarySection");
const aiSummaryContent = document.getElementById("aiSummaryContent");
const modelInfo = document.getElementById("modelInfo");
const modelName = document.getElementById("modelName");
let mediaStream = null;
const API_URL = "http://localhost:3000";
// Initialize
document.addEventListener("keydown", (e) => {
if (e.key === "Enter" && captureBtn.style.display !== "none") {
captureBtn.click();
}
});
// Camera initialization
async function initializeCamera() {
try {
mediaStream = await navigator.mediaDevices.getUserMedia({
video: { facingMode: "environment" },
});
video.srcObject = mediaStream;
captureBtn.disabled = false;
toggleCameraBtn.textContent = "📷";
} catch (err) {
alert("Please allow camera access");
toggleCameraBtn.disabled = true;
}
}
// Capture image
captureBtn.addEventListener("click", async () => {
const canvas = document.createElement("canvas");
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
canvas.getContext("2d").drawImage(video, 0, 0);
const imageData = canvas.toDataURL("image/png");
capturedImage.src = imageData;
video.style.display = "none";
capturedImage.style.display = "block";
try {
extractedText.textContent = "Processing image...";
const response = await fetch(`/api/ocr`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ image: imageData.split(",")[1] }),
});
const data = await response.json();
extractedText.textContent = data.text || "No text found";
} catch (error) {
extractedText.textContent = "Error processing image";
}
captureBtn.style.display = "none";
retakeBtn.style.display = "inline-block";
});
// Retake photo
retakeBtn.addEventListener("click", () => {
initializeCamera();
capturedImage.style.display = "none";
video.style.display = "block";
captureBtn.style.display = "inline-block";
retakeBtn.style.display = "none";
extractedText.textContent = "Your text will appear here...";
searchText.value = "";
aiSummarySection.style.display = "none";
});
// Camera toggle
toggleCameraBtn.addEventListener("click", () => {
if (mediaStream) {
mediaStream.getTracks().forEach((track) => track.stop());
mediaStream = null;
video.style.display = "none";
capturedImage.style.display = "none";
toggleCameraBtn.textContent = "📷";
captureBtn.disabled = true;
} else {
initializeCamera();
}
});
// Copy to clipboard
copyClipboardBtn.addEventListener("click", () => {
const textToCopy = extractedText.textContent;
navigator.clipboard
.writeText(textToCopy)
.then(() => {
// Show copy feedback
const feedback = document.createElement("div");
feedback.innerHTML = '<i class="fas fa-check"></i> Text copied!';
feedback.style.position = "fixed";
feedback.style.bottom = "20px";
feedback.style.left = "50%";
feedback.style.transform = "translateX(-50%)";
feedback.style.padding = "10px 15px";
feedback.style.backgroundColor = "#2ecc71";
feedback.style.color = "white";
feedback.style.borderRadius = "8px";
feedback.style.zIndex = "1000";
feedback.style.boxShadow = "0 4px 10px rgba(0,0,0,0.2)";
feedback.style.animation = "fadeIn 0.3s ease";
document.body.appendChild(feedback);
setTimeout(() => {
feedback.style.animation = "fadeOut 0.5s ease forwards";
setTimeout(() => {
document.body.removeChild(feedback);
}, 500);
}, 1500);
})
.catch((err) => {
console.error("Failed to copy text:", err);
});
});
// Search functionality
searchBtn.addEventListener("click", () => {
const query = searchText.value.trim();
if (query)
window.open(
`https://www.google.com/search?q=${encodeURIComponent(query)}`,
"_blank"
);
});
// Text selection
extractedText.addEventListener("mouseup", () => {
const selection = window.getSelection().toString().trim();
if (selection) searchText.value = selection;
});
// Dark mode toggle
darkModeToggle.addEventListener("click", () => {
document.body.classList.toggle("dark-mode");
localStorage.setItem(
"darkMode",
document.body.classList.contains("dark-mode")
);
darkModeToggle.textContent = document.body.classList.contains("dark-mode")
? "☀️"
: "🌙";
});
// Initialize dark mode
if (localStorage.getItem("darkMode") === "true") {
document.body.classList.add("dark-mode");
darkModeToggle.textContent = "☀️";
}
// AI Summary functionality - Always uses "fast" model
aiSummaryBtn.addEventListener("click", async () => {
const textToSummarize = searchText.value.trim() || extractedText.textContent;
if (!textToSummarize || textToSummarize === "Your text will appear here...") {
alert("Please capture text first or select text to summarize");
return;
}
// Show loading state
aiSummarySection.style.display = "block";
aiSummaryContent.innerHTML =
'<div class="loading-spinner"></div> Generating summary...';
modelInfo.classList.remove("visible");
// Scroll to summary section
aiSummarySection.scrollIntoView({ behavior: "smooth", block: "start" });
try {
const response = await fetch(`/api/summarize`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
text: textToSummarize,
modelType: "fast", // Always use fast model
}),
});
const data = await response.json();
if (response.ok) {
// Update model info
modelName.textContent = "fast";
modelInfo.classList.add("visible");
// Convert Markdown to HTML
const html = marked.parse(data.summary);
aiSummaryContent.innerHTML = html;
} else {
aiSummaryContent.innerHTML = `Error: ${
data.error || "Failed to generate summary"
}`;
}
} catch (error) {
console.error("AI Summary Error:", error);
aiSummaryContent.innerHTML = "Error: Failed to connect to the server.";
}
});
initializeCamera();