-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwitter_Filter
221 lines (189 loc) · 7.08 KB
/
Twitter_Filter
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
// ==UserScript==
// @name Twitter Ratio & Keyword Filter
// @namespace http://tampermonkey.net/
// @version 0.4
// @description Hide tweets from accounts based on keywords or suspicious follower/following ratios (excluding mutuals + CSV whitelisted)
// @author You
// @match *://*.twitter.com/*
// @match *://*.x.com/*
// @include *://*.twitter.com/*
// @include *://*.x.com/*
// @grant GM_log
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
// 1. Comma-separated list of banned words to check in handle/description
const bannedWordsList = "groyper,nafo";
const bannedWords = bannedWordsList.split(',')
.map(w => w.trim().toLowerCase())
.filter(Boolean);
// 2. Comma-separated CSV of whitelisted handles (lowercase them for convenience)
// Example: "coolDude,SomeFriend,AnotherPal"
const whitelistedHandlesCSV = "someVIP,anotherVIP";
const whitelistedHandles = new Set(
whitelistedHandlesCSV.split(',')
.map(h => h.trim().toLowerCase())
.filter(Boolean)
);
const settings = {
follow_limit: 100, // minimum follower threshold
};
let blf_exception_log = [];
function log_exception(e) {
while (blf_exception_log.length >= 10) {
blf_exception_log.shift();
}
blf_exception_log.push(e);
console.log('log_exception() got exception: ', e);
}
class TwitterUser {
constructor(id, handle, name, followers, friends_count, we_follow, followed_by, description) {
this.id = id;
this.handle = handle;
this.name = name;
this.followers = followers;
this.friends_count = friends_count;
this.we_follow = we_follow;
this.followed_by = followed_by;
this.description = description || "";
}
}
// Intercept Twitter API responses
const oldXHROpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function() {
if (arguments.length >= 2) {
const url = arguments[1];
// Hook various Twitter API endpoints
if (
url.includes('/HomeTimeline') ||
url.includes('/HomeLatestTimeline') ||
url.includes('/TweetDetail') ||
url.includes('/search/adaptive.json') ||
url.includes('/notifications/all.json') ||
url.includes('/notifications/mentions.json')
) {
if (!this._hooked) {
this._hooked = true;
hookXHRResponse(this);
}
}
}
return oldXHROpen.apply(this, arguments);
};
function hookXHRResponse(xhr) {
const getter = function() {
delete xhr.responseText;
let response = xhr.responseText;
try {
let json = JSON.parse(response);
filterContent(json);
response = JSON.stringify(json);
} catch (e) {
log_exception(e);
}
Object.defineProperty(xhr, 'responseText', {
value: response,
writable: false
});
return response;
};
Object.defineProperty(xhr, 'responseText', {
get: getter,
configurable: true
});
}
/**
* Return an array of reasons to hide the user, or an empty array if no hide.
*/
function getHideReasons(user) {
// 1. Skip checks if we follow them or they follow us
if (user.we_follow || user.followed_by) {
return [];
}
// 2. Skip if handle is in CSV-based whitelist
if (whitelistedHandles.has(user.handle.toLowerCase())) {
return [];
}
let reasons = [];
// a) Check banned words in handle or description
const handleDesc = (user.handle + " " + user.description).toLowerCase();
for (const w of bannedWords) {
if (handleDesc.includes(w)) {
reasons.push(`matched banned keyword: "${w}"`);
}
}
// b) Check if following >= 10x the number of followers
if (user.followers > 0 && user.friends_count >= 10 * user.followers) {
reasons.push("follows >= 10x more accounts than followers");
}
// c) Check if they have fewer than follow_limit followers
if (user.followers < settings.follow_limit) {
reasons.push(`has fewer than ${settings.follow_limit} followers`);
}
return reasons;
}
function hideTweet(tweetResults) {
if (tweetResults.result && tweetResults.result.__typename === 'Tweet') {
tweetResults.result.__typename = '';
}
}
function filterContent(json) {
// Process timeline data
if (json.data) {
const instructions = (
json.data.home?.home_timeline_urt?.instructions ||
json.data.threaded_conversation_with_injections_v2?.instructions ||
[]
);
instructions.forEach(instruction => {
if (instruction.type === 'TimelineAddEntries') {
instruction.entries.forEach(entry => {
processEntry(entry);
});
}
});
}
}
function processEntry(entry) {
if (!entry.content) return;
if (entry.content.entryType === 'TimelineTimelineItem') {
processTimelineItem(entry.content.itemContent);
} else if (entry.content.entryType === 'TimelineTimelineModule') {
entry.content.items?.forEach(item => {
processTimelineItem(item.item.itemContent);
});
}
}
function processTimelineItem(itemContent) {
if (!itemContent || itemContent.itemType !== 'TimelineTweet') return;
const tweetResults = itemContent.tweet_results;
if (!tweetResults?.result) return;
const userData = extractUserData(tweetResults.result);
if (!userData) return;
const reasons = getHideReasons(userData);
if (reasons.length > 0) {
hideTweet(tweetResults);
console.log(
`Filtered tweet from @${userData.handle} (followers: ${userData.followers}, following: ${userData.friends_count}). Reasons: ${reasons.join("; ")}`
);
}
}
function extractUserData(tweetData) {
if (!tweetData.core?.user_results?.result) return null;
const userObj = tweetData.core.user_results.result;
const legacyData = userObj.legacy;
if (!legacyData) return null;
return new TwitterUser(
userObj.rest_id,
legacyData.screen_name,
legacyData.name,
legacyData.followers_count,
legacyData.friends_count,
legacyData.following,
legacyData.followed_by,
legacyData.description
);
}
console.log('Twitter Ratio & Keyword Filter with CSV Whitelist: Loaded');
})();