Skip to content

Commit 171f465

Browse files
committed
fixed issue where found links weren't working
1 parent fa1922b commit 171f465

File tree

1 file changed

+56
-39
lines changed

1 file changed

+56
-39
lines changed

content.js

Lines changed: 56 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -59,59 +59,76 @@ function displayPreloader(sectionTitle, domain) {
5959
function handlePolicyLinks() {
6060
const keywords = ['privacy', 'terms', 'return', 'shipping', 'legal', 'cookie'];
6161
const currentDomain = rootDomain(new URL(window.location.href).hostname);
62-
let linksMap = {};
6362
let summarizedLinks = JSON.parse(localStorage.getItem('summarizedLinks') || '{}');
6463

6564
// get chrome.storage.local of auto_summaries, and if it exists, set it to autoSummaries, else set to false
6665
chrome.storage.local.get(['autoSummaries'], function (result) {
6766
let autoSummaries = result.autoSummaries || false;
68-
if (autoSummaries) {
69-
keywords.forEach(keyword => {
70-
const foundLinks = Array.from(document.querySelectorAll('a')).filter(link => {
71-
return (link.href.toLowerCase().includes(keyword) || link.innerText.toLowerCase().includes(keyword));
72-
});
73-
74-
foundLinks.forEach(link => {
75-
const linkDomain = rootDomain(new URL(link.href, window.location.origin).hostname);
76-
if (linkDomain === currentDomain) {
77-
if (!linksMap[keyword]) {
78-
linksMap[keyword] = { href: link.href, text: link.innerText.trim() };
79-
80-
if (!summarizedLinks[link.href] && link.innerText.trim() !== "") {
81-
const summaryRequestData = {
82-
action: "generateSummary",
83-
url: link.href,
84-
policyName: link.innerText.trim()
85-
};
86-
chrome.runtime.sendMessage(summaryRequestData);
87-
displayPreloader(link.innerText.trim(), currentDomain);
88-
console.log("Sent summary request for " + link.innerText.trim());
89-
summarizedLinks[link.href] = true;
90-
localStorage.setItem('summarizedLinks', JSON.stringify(summarizedLinks));
91-
}
92-
}
67+
console.log("Auto summaries: " + autoSummaries);
68+
keywords.forEach(keyword => {
69+
const foundLinks = Array.from(document.querySelectorAll('a')).filter(link => {
70+
return (link.href.toLowerCase().includes(keyword) || link.innerText.toLowerCase().includes(keyword));
71+
});
72+
73+
foundLinks.forEach(link => {
74+
const linkDomain = rootDomain(new URL(link.href, window.location.origin).hostname);
75+
if (linkDomain === currentDomain) {
76+
if (!summarizedLinks[link.href] && link.innerText.trim() !== "" && autoSummaries) {
77+
const summaryRequestData = {
78+
action: "generateSummary",
79+
url: link.href,
80+
policyName: link.innerText.trim()
81+
};
82+
chrome.runtime.sendMessage(summaryRequestData);
83+
displayPreloader(link.innerText.trim(), currentDomain);
84+
console.log("Sent summary request for " + link.innerText.trim());
85+
summarizedLinks[link.href] = true;
86+
localStorage.setItem('summarizedLinks', JSON.stringify(summarizedLinks));
9387
}
94-
});
88+
}
9589
});
96-
}
90+
});
9791
});
98-
99-
100-
if (Object.keys(linksMap).length > 0) {
101-
return Object.values(linksMap);
102-
} else {
103-
return [];
104-
}
10592
}
10693

107-
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
94+
95+
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
10896
if (request.action === "findLinks") {
109-
const links = handlePolicyLinks();
110-
sendResponse({ links: links });
111-
return true;
97+
const keywords = ['privacy', 'term', 'return', 'shipping', 'legal', 'cookie']; // Add more keywords as needed
98+
let linksMap = {};
99+
const currentDomain = rootDomain(new URL(window.location.href).hostname);
100+
101+
// Helper function to check if domains match considering subdomains
102+
const isDomainMatch = (linkDomain, currentDomain) => {
103+
return linkDomain.includes(currentDomain) || currentDomain.includes(linkDomain);
104+
};
105+
106+
keywords.forEach(keyword => {
107+
const foundLinks = Array.from(document.querySelectorAll('a')).filter(link => {
108+
return (link.href.toLowerCase().includes(keyword) || link.innerText.toLowerCase().includes(keyword));
109+
});
110+
111+
for (let i = foundLinks.length - 1; i >= 0; i--) {
112+
const linkDomain = rootDomain(new URL(foundLinks[i].href, window.location.origin).hostname);
113+
if (isDomainMatch(linkDomain, currentDomain)) {
114+
linksMap[keyword] = { href: foundLinks[i].href, text: foundLinks[i].innerText.trim() };
115+
break;
116+
}
117+
}
118+
});
119+
120+
let links = Object.keys(linksMap).map(keyword => ({
121+
keyword: keyword,
122+
href: linksMap[keyword].href,
123+
text: linksMap[keyword].text
124+
}));
125+
sendResponse({links: links});
112126
}
113127
});
114128

129+
130+
131+
115132
detectCheckboxes();
116133
detectTextConsent();
117134
handlePolicyLinks();

0 commit comments

Comments
 (0)