-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
198 lines (173 loc) · 5.47 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//Global Variables
const URL = window.location.href;
//Utility function used by other functions further down in code
const classRemover = (element, className) => {
//Finds the parent element of given child element. If found, sets the display to "none"
const findParent = (element, className) => {
if (!element || !element.classList) return null;
if (element.classList?.contains(className)) {
return element;
} else {
return findParent(element.parentElement, className);
}
};
const parent = findParent(element, className);
if (parent) parent.style.display = "none";
};
//Function to remove signin popup
const signin = () => {
const signinPopup = document.querySelector(
".q-flex.qu-alignItems--center.qu-justifyContent--center.qu-overflow--hidden.qu-zIndex--blocking_wall"
);
if (!URL.endsWith("?") && signinPopup) {
const newURL = URL + "?";
//Sends message to background.js to modify the URL
chrome.runtime.sendMessage({ action: "modifyURL", newURL });
}
};
//Function to remove ads
const ads = () => {
const adsElement = document.querySelector(".q-sticky");
const adsInMainContentElement = document.querySelector(
".q-text.qu-dynamicFontSize--small.qu-color--gray_light.qu-passColorToLinks"
);
if (adsElement) adsElement.style.display = "none";
if (
adsInMainContentElement &&
adsInMainContentElement.textContent.startsWith("Ad")
)
classRemover(adsInMainContentElement, "qu-borderBottom");
};
//Function to remove promoted ads and sponsorships
const promoted = () => {
const promotedList = document.querySelectorAll(
".spacing_log_question_page_ad"
);
if (promotedList) {
for (const item of promotedList) {
item.style.display = "none";
}
}
};
//Function to remove related answers
const related = async () => {
if (URL.includes("/unanswered/")) return;
const popupList = document.querySelectorAll(
".q-text.qu-ellipsis.qu-whiteSpace--nowrap"
);
let popup;
for (const item of popupList) {
if (item.textContent.startsWith("All related")) {
popup = item;
break;
}
}
if (popup) {
popup.click();
await new Promise((resolve) => setTimeout(resolve, 2000)); // Wait for 2 seconds
const answersButton = document
.querySelectorAll(".q-text.qu-dynamicFontSize--small.qu-color--gray_dark")
.item(1);
if (answersButton) answersButton.click();
await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait for 1 second
promoted();
}
};
// Send a message to background script to get data
chrome.runtime.sendMessage({ action: "getObj" }, (response) => {
const {
signin: signinValue,
ads: adsValue,
promoted: promotedValue,
related: relatedValue,
} = response;
//Adding a loading spinner
if (signinValue || adsValue || promotedValue || relatedValue) {
const overlay = document.createElement("div");
overlay.id = "overlay";
overlay.innerHTML = `
<style>
.spinner {
width: 48px;
height: 48px;
border-radius: 50%;
position: relative;
animation: rotate 1s linear infinite
}
.spinner::before , .spinner::after {
content: "";
box-sizing: border-box;
position: absolute;
inset: 0px;
border-radius: 50%;
border: 5px solid #FFF;
animation: prixClipFix 2s linear infinite ;
}
.spinner::after{
border-color: #f52936;
animation: prixClipFix 2s linear infinite , rotate 0.5s linear infinite reverse;
inset: 6px;
}
@keyframes rotate {
0% {transform: rotate(0deg)}
100% {transform: rotate(360deg)}
}
@keyframes prixClipFix {
0% {clip-path:polygon(50% 50%,0 0,0 0,0 0,0 0,0 0)}
25% {clip-path:polygon(50% 50%,0 0,100% 0,100% 0,100% 0,100% 0)}
50% {clip-path:polygon(50% 50%,0 0,100% 0,100% 100%,100% 100%,100% 100%)}
75% {clip-path:polygon(50% 50%,0 0,100% 0,100% 100%,0 100%,0 100%)}
100% {clip-path:polygon(50% 50%,0 0,100% 0,100% 100%,0 100%,0 0)}
}
#overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
backdrop-filter: blur(15px);
flex-direction: column;
justify-content: center;
align-items: center;
z-index: 9999;
}
#overlay p {
margin-top: 10px;
font-weight: bold;
color: #f52936;
}
</style>
<div class="spinner"></div>
<p>Clearing the Clutter...</p>
`;
document.body.appendChild(overlay);
}
if (promotedValue) {
// Create a MutationObserver to watch for changes in the targetNode
const observer = new MutationObserver((mutationsList) => {
for (const mutation of mutationsList) {
if (mutation.type === "childList" && mutation.addedNodes.length > 0) {
for (const node of mutation.addedNodes) {
if (node.classList?.contains("spacing_log_question_page_ad"))
promoted();
}
}
}
});
const targetNode = document.getElementById("mainContent");
observer.observe(targetNode, {
childList: true,
subtree: true,
});
}
//Initially called when the content script fires in browser
signinValue && signin();
adsValue && ads();
promotedValue && promoted();
relatedValue && related();
//Wait for 3 seconds and remove the loading spinner
setTimeout(() => {
document.getElementById("overlay")?.remove();
}, 3000);
});