forked from kheina-com/Blue-Blocker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared.js
More file actions
323 lines (294 loc) · 9.55 KB
/
shared.js
File metadata and controls
323 lines (294 loc) · 9.55 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
var s = document.createElement("script");
s.src = chrome.runtime.getURL("inject.js");
s.id = "injected-blue-block-xhr";
s.type = "text/javascript";
// s.onload = function() {
// this.remove();
// };
(document.head || document.documentElement).appendChild(s);
export const DefaultOptions = {
// by default, spare as many people as possible
// let the user decide if they want to be stricter
blockFollowing: false,
blockFollowers: false,
skipVerified: true,
skipAffiliated: true,
skip1Mplus: true,
blockNftAvatars: false
};
// when parsing a timeline response body, these are the paths to navigate in the json to retrieve the "instructions" object
// the key to this object is the capture group from the request regex in inject.js
export const InstructionsPaths = {
HomeLatestTimeline: [
"data",
"home",
"home_timeline_urt",
"instructions",
],
HomeTimeline: [
"data",
"home",
"home_timeline_urt",
"instructions",
],
UserTweets: [
"data",
"user",
"result",
"timeline_v2",
"timeline",
"instructions",
],
TweetDetail: [
"data",
"threaded_conversation_with_injections_v2",
"instructions",
],
};
// this is the path to retrieve the user object from the individual tweet
export const UserObjectPath = [
"tweet_results",
"result",
"tweet",
"core",
"user_results",
"result",
];
export const IgnoreTweetTypes = new Set([
"TimelineTimelineCursor",
]);
export const Headers = [
"authorization",
"x-csrf-token",
"x-twitter-active-user",
"x-twitter-auth-type",
"x-twitter-client-language",
];
var options = { };
export function SetOptions(items) {
options = items;
}
const ReasonBlueVerified = 0;
const ReasonNftAvatar = 1;
const ReasonMap = {
[ReasonBlueVerified]: "Twitter Blue verified",
[ReasonNftAvatar]: "NFT avatar",
};
const BlockQueue = [];
const BlockCache = new Set();
let BlockInterval = undefined;
export function ClearCache() {
BlockCache.clear();
}
function QueueBlockUser(user, user_id, headers, reason) {
if (BlockCache.has(user_id)) {
return;
}
BlockCache.add(user_id);
BlockQueue.push({user, user_id, headers, reason});
console.log(`queued ${user.legacy.name} (@${user.legacy.screen_name}) for a block due to ${ReasonMap[reason]}.`);
if (BlockInterval === undefined) {
BlockInterval = setInterval(CheckBlockQueue, 5000);
}
}
function CheckBlockQueue() {
if (BlockQueue.length === 0) {
clearInterval(BlockInterval);
BlockInterval = undefined;
return;
}
const {user, user_id, headers, reason} = BlockQueue.shift();
BlockUser(user, user_id, headers, reason);
}
function BlockUser(user, user_id, headers, reason, attempt=1) {
const formdata = new FormData();
formdata.append("user_id", user_id);
const ajax = new XMLHttpRequest();
ajax.addEventListener('load', event => console.log(`blocked ${user.legacy.name} (@${user.legacy.screen_name}) due to ${ReasonMap[reason]}.`), false);
ajax.addEventListener('error', error => {
console.error('error:', error);
if (attempt < 3) {
BlockUser(user, user_id, headers, reason, attempt + 1);
} else {
console.error(`failed to block ${user.legacy.name} (@${user.legacy.screen_name}):`, user);
}
}, false);
ajax.open('POST', "https://twitter.com/i/api/1.1/blocks/create.json");
for (const header of Headers) {
ajax.setRequestHeader(header, headers[header]);
}
ajax.send(formdata);
}
export function BlockBlueVerified(user, headers) {
// since we can be fairly certain all user objects will be the same, break this into a separate function
if (user.legacy.blocking) {
return;
}
if (user.is_blue_verified) {
if (
// group for block-following option
!options.blockFollowing && (user.legacy.following || user.super_following)
) {
console.log(`did not block Twitter Blue verified user ${user.legacy.name} (@${user.legacy.screen_name}) because you follow them.`);
}
else if (
// group for block-followers option
!options.blockFollowers && user.legacy.followed_by
) {
console.log(`did not block Twitter Blue verified user ${user.legacy.name} (@${user.legacy.screen_name}) because they follow you.`);
}
else if (
// group for skip-verified option
options.skipVerified && (user.legacy.verified || user.legacy.verified_type)
) {
console.log(`did not block Twitter Blue verified user ${user.legacy.name} (@${user.legacy.screen_name}) because they are verified through other means.`);
}
else if (
// verified via an affiliated organisation instead of blue
options.skipAffiliated && user.affiliates_highlighted_label.label
) {
console.log(`did not block Twitter Blue verified user ${user.legacy.name} (@${user.legacy.screen_name}) because they are verified through an affiliated organisation.`);
}
else if (
// verified by follower count
options.skip1Mplus && (user.legacy.followers_count > 1000000 || !user.legacy.followers_count)
) {
console.log(`did not block Twitter Blue verified user ${user.legacy.name} (@${user.legacy.screen_name}) because they have over a million followers and Elon is an idiot.`);
}
else {
QueueBlockUser(user, String(user.rest_id), headers, ReasonBlueVerified);
}
}
if (options.blockNftAvatars && user.has_nft_avatar) {
if (
// group for block-following option
!options.blockFollowing && (user.legacy.following || user.super_following)
) {
console.log(`did not block user with NFT avatar ${user.legacy.name} (@${user.legacy.screen_name}) because you follow them.`);
}
else if (
// group for block-followers option
!options.blockFollowers && user.legacy.followed_by
) {
console.log(`did not block user with NFT avatar ${user.legacy.name} (@${user.legacy.screen_name}) because they follow you.`);
}
else {
QueueBlockUser(user, String(user.rest_id), headers, ReasonNftAvatar);
}
}
}
function HandleTweetObject(obj, headers) {
let ptr = obj;
for (const key of UserObjectPath) {
if (ptr.hasOwnProperty(key)) {
ptr = ptr[key];
}
}
if (ptr.__typename !== "User") {
console.error("could not parse tweet", obj);
return;
}
BlockBlueVerified(ptr, headers);
}
export function ParseTimelineTweet(tweet, headers) {
if(tweet.itemType=="TimelineTimelineCursor") {
return;
}
// Handle retweets and quoted tweets (check the retweeted user, too)
if(tweet?.tweet_results?.result?.quoted_status_result) {
HandleTweetObject(tweet.tweet_results.result.quoted_status_result.result, headers);
} else if(tweet?.tweet_results?.result?.legacy?.retweeted_status_result) {
HandleTweetObject(tweet.tweet_results.result.legacy.retweeted_status_result.result, headers);
}
HandleTweetObject(tweet, headers);
}
export function HandleInstructionsResponse(e, body) {
// pull the "instructions" object from the tweet
let instructions = body;
try {
for (const key of InstructionsPaths[e.detail.parsedUrl[1]]) {
instructions = instructions[key];
}
}
catch (e) {
console.error("failed to parse response body for instructions object", e, body);
return;
}
// "instructions" should be an array, we need to iterate over it to find the "TimelineAddEntries" type
let tweets = undefined;
let isAddToModule = false;
for (const value of instructions) {
if (value.type === "TimelineAddEntries" || value.type === "TimelineAddToModule") {
tweets = value;
isAddToModule = value.type === "TimelineAddToModule";
break;
}
}
if (tweets === undefined) {
console.error("response object does not contain an instruction to add entries", body);
return;
}
if (isAddToModule) {
// wrap AddToModule info so the handler can treat it the same (and unwrap it below)
tweets.entries = [{
content: {
entryType: "TimelineTimelineModule",
items: tweets.moduleItems
}
}];
}
// tweets object should now contain an array of all returned tweets
for (const tweet of tweets.entries) {
// parse each tweet for the user object
switch (tweet?.content?.entryType) {
case null:
console.error("tweet structure does not match expectation", tweet);
break;
case "TimelineTimelineItem":
if (tweet.content.itemContent.itemType=="TimelineTweet") {
ParseTimelineTweet(tweet.content.itemContent, e.detail.request.headers);
}
break;
case "TimelineTimelineModule":
for (const innerTweet of tweet.content.items) {
ParseTimelineTweet(innerTweet.item.itemContent, e.detail.request.headers)
}
break;
default:
if (!IgnoreTweetTypes.has(tweet.content.entryType)) {
console.error(`unexpected tweet type found: ${tweet.content.entryType}`, tweet);
}
}
}
if (isAddToModule) {
tweets.moduleItems = tweets.entries[0]?.content?.items || [];
delete tweets.entries;
}
}
export function HandleHomeTimeline(e, body) {
// This API endpoint currently does not deliver information required for
// block filters (in particular, it's missing affiliates_highlighted_label).
// So if the user has set the "skip users verified by other means" options,
// this function must be skipped, however, it is still mostly covered by the
// instructions responses
if (options.skipAffiliated) return;
// so this url straight up gives us an array of users, so just use that lmao
for (const [user_id, user] of Object.entries(body.globalObjects.users)) {
// the user object is a bit different, so reshape it a little
BlockBlueVerified({
is_blue_verified: user.ext_is_blue_verified,
has_nft_avatar: user.ext_has_nft_avatar,
legacy: {
blocking: user.blocking,
followed_by: user.followed_by,
following: user.following,
name: user.name,
screen_name: user.screen_name,
verified: user.verified,
verified_type: user.ext_verified_type,
},
super_following: user.ext?.superFollowMetadata?.r?.ok?.superFollowing,
rest_id: user_id,
}, e.detail.request.headers)
}
}