-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathselector-capture.js
More file actions
379 lines (326 loc) · 11.2 KB
/
Copy pathselector-capture.js
File metadata and controls
379 lines (326 loc) · 11.2 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
368
369
370
371
372
373
374
375
376
377
378
379
(() => {
const browserAPI = typeof browser !== "undefined" ? browser : chrome;
const MAX_SELECTORS_DEFAULT = 10;
let isCapturing = false;
let currentWebhookId = null;
let selectorSet = new Set();
let selectorLimit = MAX_SELECTORS_DEFAULT;
let highlightBox = null;
let tooltip = null;
let lastHoveredElement = null;
const TOOLTIP_ID = "webhook-trigger-selector-tooltip";
const HIGHLIGHT_ID = "webhook-trigger-selector-highlight";
const normalizeSelectors = (selectors) => {
if (!Array.isArray(selectors)) return [];
return selectors
.map((value) => (typeof value === "string" ? value.trim() : ""))
.filter((value) => value.length > 0);
};
const persistSelectorLocally = async (selector) => {
if (!currentWebhookId || !selector) return;
try {
const data = await browserAPI.storage.sync.get("webhooks");
const webhooks = Array.isArray(data?.webhooks) ? data.webhooks : [];
const index = webhooks.findIndex((wh) => wh.id === currentWebhookId);
if (index === -1) {
return;
}
const selectors = normalizeSelectors(webhooks[index].selectors);
if (selectors.includes(selector)) {
return;
}
if (selectors.length >= selectorLimit) {
return;
}
selectors.push(selector);
webhooks[index] = { ...webhooks[index], selectors };
await browserAPI.storage.sync.set({ webhooks });
} catch (error) {
// Failed to persist selector locally
}
};
const sendRuntimeMessage = (payload) => {
const message = { origin: "content-script", ...payload };
try {
const result = browserAPI.runtime.sendMessage(message);
if (result && typeof result.then === "function" && typeof result.catch === "function") {
result.catch((error) => {
// Runtime message rejected
});
}
} catch (error) {
// Ignore messaging errors when popup/background is unavailable
}
};
const createHighlightElements = () => {
if (!highlightBox) {
highlightBox = document.createElement("div");
highlightBox.id = HIGHLIGHT_ID;
Object.assign(highlightBox.style, {
position: "fixed",
pointerEvents: "none",
border: "2px solid #2563eb",
backgroundColor: "rgba(37, 99, 235, 0.15)",
zIndex: "2147483646",
transition: "all 0.1s ease",
});
document.documentElement.appendChild(highlightBox);
}
if (!tooltip) {
tooltip = document.createElement("div");
tooltip.id = TOOLTIP_ID;
Object.assign(tooltip.style, {
position: "fixed",
pointerEvents: "none",
padding: "6px 10px",
backgroundColor: "#2563eb",
color: "#fff",
borderRadius: "4px",
fontSize: "12px",
zIndex: "2147483647",
boxShadow: "0 2px 10px rgba(0,0,0,0.2)",
maxWidth: "280px",
lineHeight: "1.4",
});
tooltip.innerText = browserAPI?.i18n?.getMessage
? browserAPI.i18n.getMessage("selectorCaptureTooltip") || "Click elements to capture their text. Press Esc to stop."
: "Click elements to capture their text. Press Esc to stop.";
document.documentElement.appendChild(tooltip);
}
};
const removeHighlightElements = () => {
if (highlightBox && highlightBox.parentNode) {
highlightBox.parentNode.removeChild(highlightBox);
}
if (tooltip && tooltip.parentNode) {
tooltip.parentNode.removeChild(tooltip);
}
highlightBox = null;
tooltip = null;
};
const updateHighlightPosition = (element, pointerEvent) => {
if (!highlightBox || !element) return;
const rect = element.getBoundingClientRect();
Object.assign(highlightBox.style, {
top: `${rect.top}px`,
left: `${rect.left}px`,
width: `${rect.width}px`,
height: `${rect.height}px`,
display: "block",
});
if (tooltip) {
const padding = 8;
let top = pointerEvent.clientY + padding;
let left = pointerEvent.clientX + padding;
if (left + tooltip.offsetWidth > window.innerWidth) {
left = pointerEvent.clientX - tooltip.offsetWidth - padding;
}
if (top + tooltip.offsetHeight > window.innerHeight) {
top = pointerEvent.clientY - tooltip.offsetHeight - padding;
}
tooltip.style.top = `${Math.max(top, 0)}px`;
tooltip.style.left = `${Math.max(left, 0)}px`;
}
};
const clearHighlight = () => {
if (highlightBox) {
highlightBox.style.display = "none";
}
if (tooltip) {
tooltip.style.display = "none";
}
};
const normalizeElement = (node) => {
if (!node) return null;
return node.nodeType === Node.TEXT_NODE ? node.parentElement : node;
};
const shouldIgnoreElement = (element) => {
if (!element) return true;
if (element === document.documentElement || element === document.body) return false;
if (element.id === HIGHLIGHT_ID || element.id === TOOLTIP_ID) return true;
if (element.closest(`#${HIGHLIGHT_ID}`) || element.closest(`#${TOOLTIP_ID}`)) return true;
return false;
};
const buildSelector = (element) => {
if (!element || element.nodeType !== Node.ELEMENT_NODE) return null;
if (element.id) {
return `#${CSS.escape(element.id)}`;
}
const parts = [];
let el = element;
while (el && el.nodeType === Node.ELEMENT_NODE && el !== document.documentElement) {
let selector = el.tagName.toLowerCase();
if (el.classList.length > 0) {
selector += [...el.classList].slice(0, 3).map(cls => `.${CSS.escape(cls)}`).join("");
}
const siblings = el.parentElement
? [...el.parentElement.children].filter(child => child.tagName === el.tagName)
: [];
if (siblings.length > 1) {
const index = siblings.indexOf(el) + 1;
selector += `:nth-of-type(${index})`;
}
parts.unshift(selector);
el = el.parentElement;
}
parts.unshift("html");
const selectorString = parts.join(" > ");
try {
const matches = document.querySelectorAll(selectorString);
if (matches.length === 1) {
return selectorString;
}
} catch (_) {
// Invalid selector built, fallback to shorter strategy
}
// Fallback: rely on full path without uniqueness check
return selectorString;
};
const extractPlainText = (element) => {
if (!element) return "";
const text = element.innerText || element.textContent || "";
return text.replace(/\s+/g, " ").trim();
};
const stopCapture = (reason = "completed") => {
if (!isCapturing) return;
isCapturing = false;
currentWebhookId = null;
selectorSet = new Set();
selectorLimit = MAX_SELECTORS_DEFAULT;
lastHoveredElement = null;
document.removeEventListener("mousemove", handleMouseMove, true);
document.removeEventListener("mouseover", handleMouseMove, true);
document.removeEventListener("click", handleClick, true);
document.removeEventListener("keydown", handleKeydown, true);
clearHighlight();
removeHighlightElements();
sendRuntimeMessage({ type: "SELECTOR_CAPTURE_ENDED", reason });
};
const handleMouseMove = (event) => {
if (!isCapturing) return;
let element = normalizeElement(event.target);
if (shouldIgnoreElement(element)) {
element = null;
}
lastHoveredElement = element;
if (element) {
createHighlightElements();
tooltip.style.display = "block";
updateHighlightPosition(element, event);
} else {
clearHighlight();
}
};
const handleClick = (event) => {
if (!isCapturing) return;
event.preventDefault();
event.stopPropagation();
event.stopImmediatePropagation();
const element = normalizeElement(event.target);
if (!element || shouldIgnoreElement(element)) {
return;
}
if (selectorSet.size >= selectorLimit) {
sendRuntimeMessage({ type: "SELECTOR_CAPTURE_ERROR", reason: "limit-reached" });
stopCapture("limit-reached");
return;
}
const selector = buildSelector(element);
if (!selector) {
sendRuntimeMessage({ type: "SELECTOR_CAPTURE_ERROR", reason: "no-selector" });
return;
}
if (selectorSet.has(selector)) {
sendRuntimeMessage({ type: "SELECTOR_CAPTURE_ERROR", reason: "duplicate", selector });
return;
}
const textContent = extractPlainText(element);
if (!textContent) {
sendRuntimeMessage({ type: "SELECTOR_CAPTURE_ERROR", reason: "empty-text", selector });
return;
}
selectorSet.add(selector);
persistSelectorLocally(selector);
removeHighlightElements();
lastHoveredElement = null;
sendRuntimeMessage({
type: "SELECTOR_CAPTURED",
selector,
textContent,
webhookId: currentWebhookId,
remaining: Math.max(selectorLimit - selectorSet.size, 0),
});
if (selectorSet.size >= selectorLimit) {
stopCapture("limit-reached");
}
};
const handleKeydown = (event) => {
if (!isCapturing) return;
if (event.key === "Escape") {
event.preventDefault();
stopCapture("cancelled");
}
};
const startCapture = async ({ webhookId, existingSelectors = [], maxSelectors = MAX_SELECTORS_DEFAULT }) => {
if (!webhookId) {
return { ok: false, error: "missing-webhook" };
}
if (isCapturing) {
stopCapture("replaced");
}
isCapturing = true;
currentWebhookId = webhookId;
selectorSet = new Set(Array.isArray(existingSelectors) ? existingSelectors : []);
selectorLimit = typeof maxSelectors === "number" && maxSelectors > 0 ? maxSelectors : MAX_SELECTORS_DEFAULT;
createHighlightElements();
document.addEventListener("mousemove", handleMouseMove, true);
document.addEventListener("mouseover", handleMouseMove, true);
document.addEventListener("click", handleClick, true);
document.addEventListener("keydown", handleKeydown, true);
return { ok: true, remaining: Math.max(selectorLimit - selectorSet.size, 0) };
};
const getSelectorContent = (selectors = []) => {
if (!Array.isArray(selectors) || selectors.length === 0) {
return [];
}
return selectors.map((selector) => {
try {
const element = document.querySelector(selector);
return extractPlainText(element);
} catch (error) {
console.warn("Failed to query selector", selector, error);
return "";
}
});
};
browserAPI.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (!message || typeof message !== "object" || !message.type) {
return undefined;
}
if (message.type === "START_SELECTOR_CAPTURE") {
startCapture(message).then(sendResponse);
return true;
}
if (message.type === "STOP_SELECTOR_CAPTURE") {
stopCapture("stopped");
sendResponse({ ok: true });
return false;
}
if (message.type === "GET_SELECTOR_CONTENT") {
try {
const selectorContent = getSelectorContent(message.selectors);
sendResponse({ ok: true, selectorContent });
} catch (error) {
sendResponse({ ok: false, error: error?.message || "failed-to-get-selector-content" });
}
return false;
}
return undefined;
});
// Clean up when the page is unloaded
window.addEventListener("pagehide", () => {
if (isCapturing) {
stopCapture("page-hidden");
}
});
})();