-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
93 lines (84 loc) · 2.48 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
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
const MAX_RETRIES = 2;
const TIMEOUT = 5000;
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "getPageContent") {
const cloneDoc = document.cloneNode(true);
// replace content with comment
function replaceContentWithComment(cloneDoc, tagName, commentText) {
try {
const elements = cloneDoc.getElementsByTagName(tagName);
for (let i = elements.length - 1; i >= 0; i--) {
const comment = cloneDoc.createComment(commentText);
elements[i].parentNode.replaceChild(comment, elements[i]);
}
} catch (error) {
console.error(`Error replacing ${tagName} content:`, error);
}
}
// remove base64 images
function removeBase64Images(cloneDoc) {
try {
const images = cloneDoc.getElementsByTagName("img");
for (let i = images.length - 1; i >= 0; i--) {
if (images[i].src.startsWith("data:image")) {
const comment = cloneDoc.createComment(
"Base64 image removed"
);
images[i].parentNode.replaceChild(comment, images[i]);
}
}
const elements = cloneDoc.querySelectorAll(
'[style*="url(data:image"]'
);
for (let i = elements.length - 1; i >= 0; i--) {
const style = elements[i].getAttribute("style");
const newStyle = style.replace(
/url\(data:image[^)]+\)/g,
"/* Base64 image removed */"
);
elements[i].setAttribute("style", newStyle);
}
} catch (error) {
console.error("Error removing base64 images:", error);
}
}
function removeSpecificContent(cloneDoc) {
replaceContentWithComment(
cloneDoc,
"script",
"Script content removed"
);
replaceContentWithComment(
cloneDoc,
"style",
"Style content removed"
);
replaceContentWithComment(cloneDoc, "svg", "SVG content removed");
removeBase64Images(cloneDoc);
}
function getPageContentWithRetries(retryCount = 0) {
try {
removeSpecificContent(cloneDoc);
const modifiedHTML = cloneDoc.documentElement.outerHTML;
const pageContent = {
url: window.location.href,
domContent:
modifiedHTML ||
window.document.documentElement.outerHTML,
};
sendResponse(pageContent);
} catch (error) {
console.error("Error getting page content:", error);
if (retryCount < MAX_RETRIES) {
setTimeout(() => {
getPageContentWithRetries(retryCount + 1);
}, TIMEOUT);
} else {
sendResponse({ error: "Failed to get page content" });
}
}
}
getPageContentWithRetries();
}
return true;
});