-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.js
237 lines (225 loc) · 8.84 KB
/
filter.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
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
let filter = 5;
let timout = true;
function filterPosts() {
// get page url
if (filter < 0) return;
if (!timout) return;
filter = filter - 1;
setTimeout(() => {
timout = true;
}, 50);
timout = false;
setTimeout(() => {
filter = 5;
}, 500);
const url = window.location.href;
chrome.storage.local.get(
[
"minViewsPerMinute",
"maxTimePassed",
"hideLikedPosts",
"maxReplyCount",
"hideLikedPosts",
"hideViewsPerMinute",
"activateFilterPosts",
],
(data) => {
// console.log("data", data);
const minViewsPerMinute = data.minViewsPerMinute || 0;
const maxTimePassed = data.maxTimePassed || Infinity;
const hideLikedPosts = data.hideLikedPosts || false;
const maxReplyCount = data.maxReplyCount || Infinity;
const hideViewsPerMinute = data.hideViewsPerMinute || false;
// console.log(
// "minViewsPerMinute",
// minViewsPerMinute,
// "maxTimePassed",
// maxTimePassed,
// "hideLikedPosts",
// hideLikedPosts,
// "maxReplyCount",
// maxReplyCount,
// "hideLikedPosts",
// hideLikedPosts,
// "hideViewsPerMinute",
// hideViewsPerMinute,
// "activateFilterPosts",
// data.activateFilterPosts
// );
const posts = document.querySelectorAll("article");
posts.forEach((post) => {
if (post.querySelector("#views-per-minute")) return;
const postStatsEl = post.querySelector('[role="group"]');
const statsText = postStatsEl?.getAttribute("aria-label") || "";
if (!statsText) return;
const statsArray = statsText.split(", ");
// get the number of likes, retweets, and quote tweets and replies
let likes = 0;
let retweets = 0;
let views = 0;
let replies = 0;
let liked = false;
statsArray.forEach((stat) => {
if (stat.includes("likes") || stat.includes("like")) {
likes = parseInt(stat);
} else if (stat.includes("retweet") || stat.includes("retweet")) {
retweets = parseInt(stat);
} else if (stat.includes("reply") || stat.includes("replies")) {
replies = parseInt(stat);
} else if (stat.includes("views") || stat.includes("view")) {
views = parseInt(stat);
} else if (stat.includes("Liked")) {
liked = true;
}
});
// console.log("likes", likes, "retweets", retweets, "replies", replies);
// hide liked posts on lists and communities
if (
(url.includes("https://twitter.com/i/lists") ||
url.includes("https://twitter.com/i/communities") ||
url.includes("https://twitter.com/home")) &&
liked &&
hideLikedPosts
) {
post.remove();
}
const timeEl = post.querySelector("time");
if (!timeEl) return;
const time = timeEl?.getAttribute("datetime") || "";
const timePassed = getTimePassed(time);
const timePassedInMinutes = timePassed / 1000 / 60;
const viewsPerMinute = Math.round(views / timePassedInMinutes);
post.style.position = "relative";
if (!hideViewsPerMinute) {
const vewsPerMinuteEl = document.createElement("div");
vewsPerMinuteEl.id = "views-per-minute";
vewsPerMinuteEl.innerHTML = viewsPerMinute + " views / min";
vewsPerMinuteEl.classList.add("views-per-minute");
vewsPerMinuteEl.style.fontFamily =
" BlinkMacSystemFont, Segoe UI, Roboto, Helvetica, Arial, sans-serif;";
vewsPerMinuteEl.style.position = "absolute";
vewsPerMinuteEl.style.top = "0";
vewsPerMinuteEl.style.right = "4rem";
vewsPerMinuteEl.style.color = "blue";
if (viewsPerMinute > 20) vewsPerMinuteEl.style.color = "green";
if (viewsPerMinute > 40) vewsPerMinuteEl.style.color = "gold";
if (viewsPerMinute > 60) vewsPerMinuteEl.style.color = "orange";
if (viewsPerMinute > 100) vewsPerMinuteEl.style.color = "red";
// vewsPerMinuteEl.style.color = "white";
vewsPerMinuteEl.style.padding = "5px";
vewsPerMinuteEl.style.borderBottomLeftRadius = "10px";
vewsPerMinuteEl.style.borderBottomRightRadius = "10px";
vewsPerMinuteEl.style.fontFamily = "Arial, sans-serif";
vewsPerMinuteEl.style.fontWeight = "bold";
if (viewsPerMinute) post.appendChild(vewsPerMinuteEl);
}
if (!data.activateFilterPosts) return;
if (
url.includes("https://twitter.com/home") ||
url.includes("https://twitter.com/explore") ||
url.includes("https://twitter.com/i/lists") ||
url.includes("https://twitter.com/i/communities")
) {
if (viewsPerMinute < minViewsPerMinute) {
post.remove();
} else if (
timePassedInMinutes / 60 > maxTimePassed ||
replies > maxReplyCount
) {
if (url.includes("https://twitter.com/home")) {
post.remove();
}
post.style.opacity = "0";
// if (timePassedInMinutes > 60 * 10) {
// post.style.opacity = "0";
// }
}
}
});
}
);
}
function getTimePassed(time) {
const currentTime = new Date();
const postTime = new Date(time);
const timePassed = currentTime - postTime;
return timePassed;
}
function init() {
// Remove payment check and always run functionality
document.addEventListener("scroll", () => {
getAndHideAds();
filterPosts();
});
new PerformanceObserver((entryList) => {
filterPosts();
getAndHideAds();
}).observe({ type: "largest-contentful-paint", buffered: true });
}
init();
document.addEventListener("load", () => init());
var adsHidden = 0;
var adSelector = "div[data-testid=placementTracking]";
var trendSelector = "div[data-testid=trend]";
var userSelector = "div[data-testid=UserCell]";
var articleSelector = "article[data-testid=tweet]";
var sponsoredSvgPath =
"M20.75 2H3.25C2.007 2 1 3.007 1 4.25v15.5C1 20.993 2.007 22 3.25 22h17.5c1.243 0 2.25-1.007 2.25-2.25V4.25C23 3.007 21.993 2 20.75 2zM17.5 13.504c0 .483-.392.875-.875.875s-.875-.393-.875-.876V9.967l-7.547 7.546c-.17.17-.395.256-.62.256s-.447-.086-.618-.257c-.342-.342-.342-.896 0-1.237l7.547-7.547h-3.54c-.482 0-.874-.393-.874-.876s.392-.875.875-.875h5.65c.483 0 .875.39.875.874v5.65z";
var sponsoredBySvgPath =
"M19.498 3h-15c-1.381 0-2.5 1.12-2.5 2.5v13c0 1.38 1.119 2.5 2.5 2.5h15c1.381 0 2.5-1.12 2.5-2.5v-13c0-1.38-1.119-2.5-2.5-2.5zm-3.502 12h-2v-3.59l-5.293 5.3-1.414-1.42L12.581 10H8.996V8h7v7z";
var youMightLikeSvgPath =
"M12 1.75c-5.11 0-9.25 4.14-9.25 9.25 0 4.77 3.61 8.7 8.25 9.2v2.96l1.15-.17c1.88-.29 4.11-1.56 5.87-3.5 1.79-1.96 3.17-4.69 3.23-7.97.09-5.54-4.14-9.77-9.25-9.77zM13 14H9v-2h4v2zm2-4H9V8h6v2z";
var adsSvgPath =
"M19.498 3h-15c-1.381 0-2.5 1.12-2.5 2.5v13c0 1.38 1.119 2.5 2.5 2.5h15c1.381 0 2.5-1.12 2.5-2.5v-13c0-1.38-1.119-2.5-2.5-2.5zm-3.502 12h-2v-3.59l-5.293 5.3-1.414-1.42L12.581 10H8.996V8h7v7z";
var peopleFollowSvgPath =
"M17.863 13.44c1.477 1.58 2.366 3.8 2.632 6.46l.11 1.1H3.395l.11-1.1c.266-2.66 1.155-4.88 2.632-6.46C7.627 11.85 9.648 11 12 11s4.373.85 5.863 2.44zM12 2C9.791 2 8 3.79 8 6s1.791 4 4 4 4-1.79 4-4-1.791-4-4-4z";
var xAd = ">Ad<";
var removePeopleToFollow = false;
const promotedTweetTextSet = new Set(["Promoted Tweet", "プロモツイート"]);
function getAds() {
return Array.from(document.querySelectorAll("div")).filter(function (el) {
var filteredAd;
if (el.innerHTML.includes(sponsoredSvgPath)) {
filteredAd = el;
} else if (el.innerHTML.includes(sponsoredBySvgPath)) {
filteredAd = el;
} else if (el.innerHTML.includes(youMightLikeSvgPath)) {
filteredAd = el;
} else if (el.innerHTML.includes(adsSvgPath)) {
filteredAd = el;
} else if (
removePeopleToFollow &&
el.innerHTML.includes(peopleFollowSvgPath)
) {
filteredAd = el;
} else if (el.innerHTML.includes(xAd)) {
filteredAd = el;
} else if (promotedTweetTextSet.has(el.innerText)) {
// TODO: bring back multi-lingual support from git history
filteredAd = el;
}
return filteredAd;
});
}
function hideAd(ad) {
if (ad.closest(adSelector) !== null) {
// Promoted tweets
ad.closest(adSelector).remove();
adsHidden += 1;
} else if (ad.closest(trendSelector) !== null) {
ad.closest(trendSelector).remove();
adsHidden += 1;
} else if (ad.closest(userSelector) !== null) {
ad.closest(userSelector).remove();
adsHidden += 1;
} else if (ad.closest(articleSelector) !== null) {
ad.closest(articleSelector).remove();
adsHidden += 1;
} else if (promotedTweetTextSet.has(ad.innerText)) {
ad.remove();
adsHidden += 1;
}
}
function getAndHideAds() {
getAds().forEach(hideAd);
}