-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
62 lines (52 loc) · 1.92 KB
/
content.js
File metadata and controls
62 lines (52 loc) · 1.92 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
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "getPolicy") {
const policyText = extractPrivacyPolicy();
sendResponse({ text: policyText });
}
if (request.action === "getHeadings") {
const headings = Array.from(document.querySelectorAll("h1, h2, h3, h4"))
.map(h => h.innerText.trim())
.filter(text => text.length > 0);
sendResponse({ headings });
}
if (request.action === "scrollToSectionByText") {
const searchText = request.text.toLowerCase();
const headings = document.querySelectorAll("h1, h2, h3, h4");
let bestMatch = null;
let highestScore = 0;
headings.forEach(h => {
const headingText = h.textContent.trim().toLowerCase();
const score = getTextSimilarity(searchText, headingText);
if (score > highestScore) {
highestScore = score;
bestMatch = h;
}
});
if (bestMatch) {
bestMatch.scrollIntoView({ behavior: "smooth", block: "start" });
bestMatch.style.transition = "background-color 0.5s";
bestMatch.style.backgroundColor = "#fff7a0";
setTimeout(() => {
bestMatch.style.backgroundColor = "";
}, 1500);
}
}
return true;
});
function extractPrivacyPolicy() {
let text = "";
const paragraphs = document.querySelectorAll("p, div, span");
paragraphs.forEach(p => {
if (p.innerText.toLowerCase().includes("privacy policy")) {
text += p.innerText + " ";
}
});
return text;
}
function getTextSimilarity(a, b) {
if (b.includes(a)) return 1;
const aWords = a.split(/\s+/);
const bWords = b.split(/\s+/);
const common = aWords.filter(word => bWords.includes(word));
return common.length / Math.max(aWords.length, bWords.length);
}