-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
140 lines (118 loc) · 4.37 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Remove articles when the page loads
console.debug("injected content.js mydealz_enhance");
let forbiddenWords = []
let filterEnabled = true
chrome.runtime.sendMessage({ action: 'getKeywords' }, (keywords) => {
console.debug("sent initial getKeywords");
forbiddenWords = keywords
removeArticles()
console.debug("removed images on pageload")
});
let userFilterEnable = false
chrome.runtime.sendMessage({ action: 'getOptions' }, (options) => {
console.debug("sent initial getOptions");
console.log(options)
hideImages(options[0])
hideUserHtml(options[1])
hideCategories(options[2])
enableGreyscale(options[3])
enableFiltering(options[4])
console.debug("set options on pageload")
});
function hideImages(input) {
const main = document.querySelector('main');
if (input) {
main.classList.add('hide-threadGrid-image');
console.debug("hiding images")
} else {
main.classList.remove('hide-threadGrid-image');
console.debug("showing images")
}
}
function hideUserHtml(input) {
const main = document.querySelector('main');
if (input) {
main.classList.add('hide-userHtml');
console.debug("hiding UserHtml")
} else {
main.classList.remove('hide-userHtml');
console.debug("showing UserHtml")
}
}
function hideCategories(input) {
const main = document.querySelector('main');
if (input) {
main.classList.add('hide-groupPromo--bg');
console.debug("hiding categories")
} else {
main.classList.remove('hide-groupPromo--bg');
console.debug("showing categories")
}
}
function enableGreyscale(input) {
const body = document.body;
if (input) {
body.classList.add('greyscale');
console.debug("enabled greyscale")
} else {
body.classList.remove('greyscale');
console.debug("disabled greyscale")
}
}
function enableFiltering(input) {
userFilterEnable = input
if (userFilterEnable) {
try {
// add hint that the threadList is filtered
let inner = '🐊 Filtered'
let child = '<a style="height: 30px;" <span class="filtered-hint">' + inner + ' </span></a>'
let filteredHint = '<li style="height: 3.25062em; display: flex; align-items: center;" class="subNavMenu-item--separator cept-sort-tab">' + child + '</li>';
document.querySelectorAll(".subNavMenu-list").forEach(ul => ul.insertAdjacentHTML('beforeend', filteredHint));
} catch {
filterEnabled = false
}
}
}
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.type === "KEYWORDS_RECEIVED") {
console.debug("received keywords: " + request.keywords)
var keywords = request.keywords;
forbiddenWords = keywords
removeArticles()
}
if (request.type == 'OPTIONS_RECEIVED') {
console.debug("received options: " + request.options);
hideImages(request.options[0])
hideUserHtml(request.options[1])
hideCategories(request.options[2])
enableGreyscale(request.options[3])
}
});
function removeArticles() {
if (filterEnabled && userFilterEnable) {
let articles = document.querySelectorAll('article')
for (let article of articles) {
// Check if article is a valid DOM element
if (article.nodeType !== Node.ELEMENT_NODE) {
continue;
}
const links = article.querySelectorAll('a[title]');
for (let link of links) {
const title = link.getAttribute('title');
if (forbiddenWords.some(word => title.toLowerCase().includes(word.toLowerCase()))) {
// Check if the ancestor of the link element with a class of "threadGrid" exists
const threadGrid = link.closest('.thread');
if (threadGrid) {
// Remove the article element that contains the threadGrid element
const article = threadGrid.closest('article');
console.info("removed article: " + title)
article.remove()
}
} else {
}
}
}
}
}
const observer = new MutationObserver(removeArticles);
observer.observe(document.body, { childList: true, subtree: true });