-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent.js
44 lines (40 loc) · 1.51 KB
/
content.js
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
function captureHTML(timeout = 5000) {
return new Promise((resolve, reject) => {
const captureContent = () => ({
html: document.documentElement.outerHTML,
title: document.title
});
console.log(captureContent);
if (document.readyState === "complete") {
resolve(captureContent());
return;
}
const timer = setTimeout(() => {
reject(new Error("Timeout waiting for document to load"));
}, timeout);
document.addEventListener('readystatechange', function listener() {
if (document.readyState === "complete") {
document.removeEventListener('readystatechange', listener);
clearTimeout(timer);
resolve(captureContent());
}
});
});
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "captureHTML") {
captureHTML()
.then(content => sendResponse(content))
.catch(error => {
console.error("Error capturing HTML:", error);
sendResponse({ error: error.message });
});
return true; // Indicates that the response is sent asynchronously
}
});
// Optional: Add a check to re-capture the HTML if the initial attempt was made too early
document.addEventListener('readystatechange', () => {
if (document.readyState === "complete") {
chrome.runtime.sendMessage({ action: "retryCapture" });
}
});