-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoveadfb.user.js
More file actions
253 lines (220 loc) · 8.44 KB
/
removeadfb.user.js
File metadata and controls
253 lines (220 loc) · 8.44 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// ==UserScript==
// @name Facebook Feed Cleaner
// @namespace http://oiioioiiioooioio.download
// @version 1.1
// @description Cleans up the Facebook feed by removing sponsored and suggested posts
// @author NEXT
// @match *://*.facebook.com/*
// @grant GM_setValue
// @grant GM_getValue
// ==/UserScript==
(function () {
"use strict";
function randomString(length) {
let result = "";
let characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
let show_log = false;
let show_count_message = true;
let count_list = [
{ name: "sponsor", value: GM_getValue("sponsor", 0) },
{ name: "sidebar", value: GM_getValue("sidebar", 0) },
{ name: "suggest", value: GM_getValue("suggest", 0) },
{ name: "reel", value: GM_getValue("reel", 0) },
];
let processSponsor = GM_getValue("processSponsor", true);
let processSidebar = GM_getValue("processSidebar", true);
let processSuggest = GM_getValue("processSuggest", true);
let processReel = GM_getValue("processReel", true);
let focusInEvent = new Event("focusin", {
view: window,
bubbles: true,
cancelable: true,
});
let focusOutEvent = new Event("focusout", {
view: window,
bubbles: true,
cancelable: true,
});
function retrieveConfiguration() {
processSponsor = JSON.parse(localStorage.getItem("sponsor")) ?? true;
processSidebar = JSON.parse(localStorage.getItem("sidebar")) ?? true;
processSuggest = JSON.parse(localStorage.getItem("suggest")) ?? true;
processReel = JSON.parse(localStorage.getItem("reel")) ?? true;
cleanFeed();
}
function removeSponsoredPost() {
if (!processSponsor) return;
let feed = document.querySelector("div[role='main']");
if (!feed) return;
let aTagArray = feed.querySelectorAll(
'a[role="link"][target="_blank"]:not([href*="https:"]):not([routed=\'true\'])'
);
if (aTagArray != null) {
aTagArray.forEach((aTag) => {
aTag.setAttribute("routed", "true");
aTag.dispatchEvent(focusInEvent);
aTag.dispatchEvent(focusOutEvent);
});
}
window.setTimeout(function () {
let targetArray = feed.querySelectorAll("a[href*='/ads/about/']");
if (targetArray != null) {
targetArray.forEach((target) => {
let removeTarget = target.closest("div[role='article']");
if (removeTarget) {
if (show_count_message) count_list[0].value++;
if (!show_log) return removeTarget.remove();
removeTarget.style.display = "none";
let message = document.createElement("div");
message.innerHTML = "Sponsored post removed : Ref " + randomString(5);
message.style.color = "red";
message.style.fontSize = "12px";
message.style.margin = "10px";
removeTarget.parentNode.insertBefore(message, removeTarget);
removeTarget.removeAttribute("role");
}
});
}
}, 500);
}
function removeSuggestedPost() {
if (!processSuggest) return;
let feed = document.querySelector("div[role='main']");
if (!feed) return;
let target = feed.querySelectorAll("h4:not([reviewed='true'])");
target.forEach(function (t) {
t.setAttribute("reviewed", "true");
if (
t.lastChild !== null &&
t.lastChild.lastChild !== null &&
t.lastChild.lastChild.role === "button"
) {
let removeTarget = t.closest("div[aria-posinset]");
if (removeTarget) {
if (show_count_message) count_list[2].value++;
if (!show_log) return removeTarget.remove();
removeTarget.style.display = "none";
let message = document.createElement("div");
message.innerHTML = "Suggested post removed : Ref " + randomString(5);
message.style.color = "red";
message.style.fontSize = "12px";
message.style.margin = "10px";
removeTarget.parentNode.insertBefore(message, removeTarget);
}
} else if (
t.parentNode.parentNode.nextSibling !== null &&
t.parentNode.parentNode.nextSibling.firstChild !== null &&
t.parentNode.parentNode.nextSibling.firstChild.firstChild !== null &&
t.parentNode.parentNode.nextSibling.firstChild.firstChild.childNodes !==
null
) {
let nodes =
t.parentNode.parentNode.nextSibling.firstChild.firstChild.childNodes;
for (let i = 0; i < nodes.length; i++) {
if (nodes[i].firstElementChild == null) {
let removeTarget = t.closest("div[aria-posinset]");
if (removeTarget) {
if (show_count_message) count_list[2].value++;
if (!show_log) return removeTarget.remove();
removeTarget.style.display = "none";
let message = document.createElement("div");
message.innerHTML = "Suggested post removed : Ref " + randomString(5);
message.style.color = "red";
message.style.fontSize = "12px";
message.style.margin = "10px";
removeTarget.parentNode.insertBefore(message, removeTarget);
}
break;
}
}
}
});
}
function removeReel() {
if (!processReel) return;
let feed = document.querySelector("div[role='main']");
if (!feed) return;
let target = feed.querySelectorAll(
"a[href*='/reel/']:not([reviewed='true'])"
);
target.forEach(function (t) {
t.setAttribute("reviewed", "true");
let removeTarget = t.closest("div[aria-posinset]");
if (removeTarget) {
if (show_count_message) count_list[3].value++;
if (!show_log) return removeTarget.remove();
removeTarget.style.display = "none";
let message = document.createElement("div");
message.innerHTML = "Reel removed";
message.style.color = "red";
message.style.fontSize = "12px";
message.style.margin = "10px";
removeTarget.parentNode.insertBefore(message, removeTarget);
}
});
}
function removeRightSidebarAdvertisement() {
if (!processSidebar) return;
let noRail = document.querySelector("div[data-pagelet='NoRail']");
if (noRail != null) {
let rightSidebarTarget = noRail.querySelector(
":scope > div[data-visualcompletion='ignore-dynamic']"
);
if (rightSidebarTarget != null) {
rightSidebarTarget.parentNode
.querySelectorAll(
":scope > div:not([data-visualcompletion='ignore-dynamic'])"
)
.forEach(function (e) {
if (show_count_message) count_list[1].value++;
e.style.display = "none";
let message = document.createElement("div");
message.innerHTML = "Right sidebar ad removed : Ref " + randomString(5);
message.style.color = "red";
message.style.fontSize = "12px";
message.style.margin = "10px";
e.parentNode.insertBefore(message, e);
});
}
}
}
function showCountRemove() {
let footer = document.querySelector("footer[aria-label='Facebook']");
if (footer != null) {
let count = count_list.reduce((acc, cur) => acc + cur.value, 0);
let existingMessage = footer.querySelector(".count-message");
if (existingMessage) {
existingMessage.innerHTML = `Removed Posts: Sponsored(${count_list[0].value}), Sidebar(${count_list[1].value}), Suggested(${count_list[2].value}), Reel(${count_list[3].value})`;
} else {
let message = document.createElement("div");
message.className = "count-message";
message.innerHTML = `Removed Posts: Sponsored(${count_list[0].value}), Sidebar(${count_list[1].value}), Suggested(${count_list[2].value}), Reel(${count_list[3].value})`;
message.style.color = "red";
message.style.fontSize = "12px";
message.style.marginTop = "10px";
footer.appendChild(message);
}
}
}
function cleanFeed() {
removeSponsoredPost();
removeSuggestedPost();
removeReel();
removeRightSidebarAdvertisement();
if (show_count_message) showCountRemove();
}
retrieveConfiguration();
setInterval(function () {
cleanFeed();
}, 500);
window.addEventListener("storage", function (e) {
retrieveConfiguration();
});
})();