-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
162 lines (158 loc) · 11.1 KB
/
content.js
File metadata and controls
162 lines (158 loc) · 11.1 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
!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 || "object" != typeof elements || "number" != typeof elements.length) return;
const elementsArray = Array.isArray(elements) ? elements : Array.from(elements);
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(...elementsArray.map(e => e.importance || 0), 1);
elementsArray.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();
break;
case "SENTIENCE_SHOW_GRID_OVERLAY":
!function(data) {
const {grids: grids, targetGridId: targetGridId} = data;
if (!grids || !Array.isArray(grids)) 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"
});
grids.forEach(grid => {
const bbox = grid.bbox;
if (!bbox) return;
const isTarget = grid.grid_id === targetGridId, isDominant = !0 === grid.is_dominant;
let color = "#9B59B6";
isTarget ? color = "#FF0000" : isDominant && (color = "#FF8C00");
const borderStyle = isTarget ? "solid" : "dashed", borderWidth = isTarget ? 3 : isDominant ? 2.5 : 2, opacity = isTarget ? 1 : isDominant ? .9 : .8, fillOpacity = .1 * opacity, hexOpacity = Math.round(255 * fillOpacity).toString(16).padStart(2, "0"), box = document.createElement("div");
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 ${borderStyle} ${color};\n background-color: ${color}${hexOpacity};\n box-sizing: border-box;\n opacity: ${opacity};\n pointer-events: none;\n `;
let labelText = grid.label ? `Grid ${grid.grid_id}: ${grid.label}` : `Grid ${grid.grid_id}`;
grid.is_dominant && (labelText = `⭐ ${labelText} (dominant)`);
const badge = document.createElement("span");
if (badge.textContent = labelText, 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), 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);
}
});
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);
}
}();