-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
55 lines (46 loc) · 1.71 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
let observer;
browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'disable') {
if (observer) {
observer.disconnect();
}
// Remove any hiding styles that were applied
const elements = document.body.getElementsByTagName('*');
for (const element of elements) {
if (element.style.display === 'none') {
element.style.display = '';
}
}
}
});
async function hideElements() {
const { enabled, websiteGroups, wordGroups, assignments } = await browser.storage.sync.get([
'enabled',
'websiteGroups',
'wordGroups',
'assignments'
]);
if (!enabled) return;
const currentUrl = window.location.hostname;
const applicableWebsiteGroups = Object.keys(websiteGroups).filter(group =>
websiteGroups[group].some(url => currentUrl.includes(url))
);
// Include 'All websites' group
applicableWebsiteGroups.push('All websites');
const applicableWordGroups = applicableWebsiteGroups.flatMap(group => assignments[group] || []);
const keywords = applicableWordGroups.flatMap(group => wordGroups[group] || []);
const elements = document.body.getElementsByTagName('*');
for (const element of elements) {
if (element.childNodes.length === 1 && element.childNodes[0].nodeType === Node.TEXT_NODE) {
const text = element.textContent.toLowerCase();
if (keywords.some(keyword => text.includes(keyword.toLowerCase()))) {
element.style.display = 'none';
}
}
}
}
// Initial check when the page loads
hideElements();
// Set up a MutationObserver to check for dynamically added content
observer = new MutationObserver(hideElements);
observer.observe(document.body, { childList: true, subtree: true });