-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
126 lines (123 loc) · 7.58 KB
/
content.js
File metadata and controls
126 lines (123 loc) · 7.58 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
!function() {
"use strict";
window, window.top;
document.documentElement.dataset.sentienceExtensionId = chrome.runtime.id, window.addEventListener("message", event => {
var data;
if (event.source === window) switch (event.data.type) {
case "SENTIENCE_SCREENSHOT_REQUEST":
data = event.data, chrome.runtime.sendMessage({
action: "captureScreenshot",
options: data.options
}, response => {
window.postMessage({
type: "SENTIENCE_SCREENSHOT_RESULT",
requestId: data.requestId,
screenshot: response?.success ? response.screenshot : null,
error: response?.error
}, "*");
});
break;
case "SENTIENCE_SNAPSHOT_REQUEST":
!function(data) {
const startTime = performance.now();
let responded = !1;
const timeoutId = setTimeout(() => {
if (!responded) {
responded = !0;
const duration = performance.now() - startTime;
window.postMessage({
type: "SENTIENCE_SNAPSHOT_RESULT",
requestId: data.requestId,
error: "WASM processing timeout - background script may be unresponsive",
duration: duration
}, "*");
}
}, 2e4);
try {
chrome.runtime.sendMessage({
action: "processSnapshot",
rawData: data.rawData,
options: data.options
}, response => {
if (responded) return;
responded = !0, clearTimeout(timeoutId);
const duration = performance.now() - startTime;
chrome.runtime.lastError ? window.postMessage({
type: "SENTIENCE_SNAPSHOT_RESULT",
requestId: data.requestId,
error: `Chrome runtime error: ${chrome.runtime.lastError.message}`,
duration: duration
}, "*") : response?.success ? window.postMessage({
type: "SENTIENCE_SNAPSHOT_RESULT",
requestId: data.requestId,
elements: response.result.elements,
raw_elements: response.result.raw_elements,
duration: duration
}, "*") : window.postMessage({
type: "SENTIENCE_SNAPSHOT_RESULT",
requestId: data.requestId,
error: response?.error || "Processing failed",
duration: duration
}, "*");
});
} catch (error) {
if (!responded) {
responded = !0, clearTimeout(timeoutId);
const duration = performance.now() - startTime;
window.postMessage({
type: "SENTIENCE_SNAPSHOT_RESULT",
requestId: data.requestId,
error: `Failed to send message: ${error.message}`,
duration: duration
}, "*");
}
}
}(event.data);
break;
case "SENTIENCE_SHOW_OVERLAY":
!function(data) {
const {elements: elements, targetElementId: targetElementId} = data;
if (!elements || !Array.isArray(elements)) return;
removeOverlay();
const host = document.createElement("div");
host.id = OVERLAY_HOST_ID, host.style.cssText = "\n position: fixed !important;\n top: 0 !important;\n left: 0 !important;\n width: 100vw !important;\n height: 100vh !important;\n pointer-events: none !important;\n z-index: 2147483647 !important;\n margin: 0 !important;\n padding: 0 !important;\n ",
document.body.appendChild(host);
const shadow = host.attachShadow({
mode: "closed"
}), maxImportance = Math.max(...elements.map(e => e.importance || 0), 1);
elements.forEach(element => {
const bbox = element.bbox;
if (!bbox) return;
const isTarget = element.id === targetElementId, isPrimary = element.visual_cues?.is_primary || !1, importance = element.importance || 0;
let color;
color = isTarget ? "#FF0000" : isPrimary ? "#0066FF" : "#00FF00";
const importanceRatio = maxImportance > 0 ? importance / maxImportance : .5, borderOpacity = isTarget ? 1 : isPrimary ? .9 : Math.max(.4, .5 + .5 * importanceRatio), fillOpacity = .2 * borderOpacity, borderWidth = isTarget ? 2 : isPrimary ? 1.5 : Math.max(.5, Math.round(2 * importanceRatio)), hexOpacity = Math.round(255 * fillOpacity).toString(16).padStart(2, "0"), box = document.createElement("div");
if (box.style.cssText = `\n position: absolute;\n left: ${bbox.x}px;\n top: ${bbox.y}px;\n width: ${bbox.width}px;\n height: ${bbox.height}px;\n border: ${borderWidth}px solid ${color};\n background-color: ${color}${hexOpacity};\n box-sizing: border-box;\n opacity: ${borderOpacity};\n pointer-events: none;\n `,
importance > 0 || isPrimary) {
const badge = document.createElement("span");
badge.textContent = isPrimary ? `⭐${importance}` : `${importance}`, badge.style.cssText = `\n position: absolute;\n top: -18px;\n left: 0;\n background: ${color};\n color: white;\n font-size: 11px;\n font-weight: bold;\n padding: 2px 6px;\n font-family: Arial, sans-serif;\n border-radius: 3px;\n opacity: 0.95;\n white-space: nowrap;\n pointer-events: none;\n `,
box.appendChild(badge);
}
if (isTarget) {
const targetIndicator = document.createElement("span");
targetIndicator.textContent = "🎯", targetIndicator.style.cssText = "\n position: absolute;\n top: -18px;\n right: 0;\n font-size: 16px;\n pointer-events: none;\n ",
box.appendChild(targetIndicator);
}
shadow.appendChild(box);
}), overlayTimeout = setTimeout(() => {
removeOverlay();
}, 5e3);
}(event.data);
break;
case "SENTIENCE_CLEAR_OVERLAY":
removeOverlay();
}
});
const OVERLAY_HOST_ID = "sentience-overlay-host";
let overlayTimeout = null;
function removeOverlay() {
const existing = document.getElementById(OVERLAY_HOST_ID);
existing && existing.remove(), overlayTimeout && (clearTimeout(overlayTimeout),
overlayTimeout = null);
}
}();