-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
104 lines (99 loc) · 3.67 KB
/
background.js
File metadata and controls
104 lines (99 loc) · 3.67 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
import init, { analyze_page_with_options, analyze_page, prune_for_api } from "../pkg/sentience_core.js";
let wasmReady = !1, wasmInitPromise = null;
async function initWASM() {
if (!wasmReady) return wasmInitPromise || (wasmInitPromise = (async () => {
try {
globalThis.js_click_element = () => {}, await init(), wasmReady = !0;
} catch (error) {
throw error;
}
})(), wasmInitPromise);
}
async function handleScreenshotCapture(_tabId, options = {}) {
try {
const {format: format = "png", quality: quality = 90} = options;
return await chrome.tabs.captureVisibleTab(null, {
format: format,
quality: quality
});
} catch (error) {
throw new Error(`Failed to capture screenshot: ${error.message}`);
}
}
async function handleSnapshotProcessing(rawData, options = {}) {
const startTime = performance.now();
try {
if (!Array.isArray(rawData)) throw new Error("rawData must be an array");
if (rawData.length > 1e4 && (rawData = rawData.slice(0, 1e4)), await initWASM(),
!wasmReady) throw new Error("WASM module not initialized");
let analyzedElements, prunedRawData;
try {
const wasmPromise = new Promise((resolve, reject) => {
try {
let result;
result = options.limit || options.filter ? analyze_page_with_options(rawData, options) : analyze_page(rawData),
resolve(result);
} catch (e) {
reject(e);
}
});
analyzedElements = await Promise.race([ wasmPromise, new Promise((_, reject) => setTimeout(() => reject(new Error("WASM processing timeout (>18s)")), 18e3)) ]);
} catch (e) {
const errorMsg = e.message || "Unknown WASM error";
throw new Error(`WASM analyze_page failed: ${errorMsg}`);
}
try {
prunedRawData = prune_for_api(rawData);
} catch (e) {
prunedRawData = rawData;
}
performance.now();
return {
elements: analyzedElements,
raw_elements: prunedRawData
};
} catch (error) {
performance.now();
throw error;
}
}
initWASM().catch(err => {}), chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
try {
return "captureScreenshot" === request.action ? (handleScreenshotCapture(sender.tab.id, request.options).then(screenshot => {
sendResponse({
success: !0,
screenshot: screenshot
});
}).catch(error => {
sendResponse({
success: !1,
error: error.message || "Screenshot capture failed"
});
}), !0) : "processSnapshot" === request.action ? (handleSnapshotProcessing(request.rawData, request.options).then(result => {
sendResponse({
success: !0,
result: result
});
}).catch(error => {
sendResponse({
success: !1,
error: error.message || "Snapshot processing failed"
});
}), !0) : (sendResponse({
success: !1,
error: "Unknown action"
}), !1);
} catch (error) {
try {
sendResponse({
success: !1,
error: `Fatal error: ${error.message || "Unknown error"}`
});
} catch (e) {}
return !1;
}
}), self.addEventListener("error", event => {
event.preventDefault();
}), self.addEventListener("unhandledrejection", event => {
event.preventDefault();
});