-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrop.js
More file actions
242 lines (205 loc) · 5.95 KB
/
Copy pathcrop.js
File metadata and controls
242 lines (205 loc) · 5.95 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
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
const elements = {
sourceTitle: document.getElementById("sourceTitle"),
sendToAi: document.getElementById("sendToAi"),
resetCrop: document.getElementById("resetCrop"),
status: document.getElementById("status"),
answer: document.getElementById("answer"),
copyAnswer: document.getElementById("copyAnswer")
};
const image = new Image();
let screenshotUrl = "";
let scale = 1;
let selection = null;
let dragStart = null;
init();
async function init() {
const session = await chrome.storage.session.get([
"latestScreenshot",
"latestSourceTitle",
"latestSourceUrl"
]);
if (!session.latestScreenshot) {
setStatus("No screenshot found. Start again from the extension popup.", true);
return;
}
screenshotUrl = session.latestScreenshot;
elements.sourceTitle.textContent = session.latestSourceTitle || "Captured page";
image.onload = () => {
fitCanvas();
draw();
setStatus("Drag on the screenshot to crop. No crop means full screenshot.");
};
image.onerror = () => setStatus("Could not load screenshot.", true);
image.src = screenshotUrl;
window.addEventListener("resize", () => {
fitCanvas();
draw();
});
canvas.addEventListener("mousedown", startCrop);
canvas.addEventListener("mousemove", updateCrop);
canvas.addEventListener("mouseup", endCrop);
canvas.addEventListener("mouseleave", endCrop);
elements.resetCrop.addEventListener("click", resetCrop);
elements.sendToAi.addEventListener("click", sendToAi);
elements.copyAnswer.addEventListener("click", copyAnswer);
}
function fitCanvas() {
const maxWidth = Math.max(320, window.innerWidth - 46);
const maxHeight = Math.max(260, window.innerHeight - 332);
scale = Math.min(maxWidth / image.naturalWidth, maxHeight / image.naturalHeight, 1);
canvas.width = Math.round(image.naturalWidth * scale);
canvas.height = Math.round(image.naturalHeight * scale);
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
if (!selection || selection.w < 2 || selection.h < 2) {
return;
}
ctx.save();
ctx.fillStyle = "rgba(15, 23, 42, 0.48)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.clearRect(selection.x, selection.y, selection.w, selection.h);
ctx.drawImage(
image,
selection.x / scale,
selection.y / scale,
selection.w / scale,
selection.h / scale,
selection.x,
selection.y,
selection.w,
selection.h
);
ctx.strokeStyle = "#1d4ed8";
ctx.lineWidth = 2;
ctx.strokeRect(selection.x + 1, selection.y + 1, selection.w - 2, selection.h - 2);
ctx.restore();
}
function startCrop(event) {
const point = canvasPoint(event);
dragStart = point;
selection = { x: point.x, y: point.y, w: 0, h: 0 };
draw();
}
function updateCrop(event) {
if (!dragStart) {
return;
}
const point = canvasPoint(event);
const x = clamp(Math.min(dragStart.x, point.x), 0, canvas.width);
const y = clamp(Math.min(dragStart.y, point.y), 0, canvas.height);
const endX = clamp(Math.max(dragStart.x, point.x), 0, canvas.width);
const endY = clamp(Math.max(dragStart.y, point.y), 0, canvas.height);
selection = {
x,
y,
w: endX - x,
h: endY - y
};
draw();
}
function endCrop() {
dragStart = null;
}
function resetCrop() {
selection = null;
draw();
setStatus("Crop cleared. Full screenshot will be sent.");
}
async function sendToAi() {
elements.sendToAi.disabled = true;
setStatus("Preparing cropped image...");
elements.answer.textContent = "Loading...";
try {
const settings = await chrome.storage.local.get([
"geminiApiKey",
"geminiModel",
"answerPrompt"
]);
if (!settings.geminiApiKey) {
throw new Error("Set your Gemini API key in the extension popup first.");
}
const imageDataUrl = buildCroppedImage();
setStatus("Sending crop to Gemini...");
const response = await chrome.runtime.sendMessage({
type: "analyze-image",
payload: {
imageDataUrl,
apiKey: settings.geminiApiKey,
model: settings.geminiModel,
prompt: settings.answerPrompt
}
});
if (!response.ok) {
throw new Error(response.error);
}
elements.answer.textContent = response.data.answer;
setStatus("Done.");
} catch (error) {
elements.answer.textContent = "Belum ada output.";
setStatus(error.message || String(error), true);
} finally {
elements.sendToAi.disabled = false;
}
}
function buildCroppedImage() {
const crop = normalizedCrop();
const output = document.createElement("canvas");
output.width = crop.w;
output.height = crop.h;
const outputCtx = output.getContext("2d");
outputCtx.drawImage(
image,
crop.x,
crop.y,
crop.w,
crop.h,
0,
0,
crop.w,
crop.h
);
return output.toDataURL("image/png");
}
function normalizedCrop() {
if (!selection || selection.w < 8 || selection.h < 8) {
return {
x: 0,
y: 0,
w: image.naturalWidth,
h: image.naturalHeight
};
}
return {
x: Math.round(selection.x / scale),
y: Math.round(selection.y / scale),
w: Math.round(selection.w / scale),
h: Math.round(selection.h / scale)
};
}
async function copyAnswer() {
const text = elements.answer.textContent.trim();
if (!text || text === "Belum ada output." || text === "Loading...") {
setStatus("No output to copy.", true);
return;
}
await navigator.clipboard.writeText(text);
setStatus("Output copied.");
}
function canvasPoint(event) {
const rect = canvas.getBoundingClientRect();
return {
x: clamp(event.clientX - rect.left, 0, canvas.width),
y: clamp(event.clientY - rect.top, 0, canvas.height)
};
}
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function setStatus(message, isError = false) {
elements.status.textContent = message;
elements.status.classList.toggle("error", isError);
}