forked from kheina-com/Blue-Blocker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shared.js
221 lines (197 loc) · 6.11 KB
/
shared.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
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);
// this is the magic regex to determine if its a request we need. add new urls below
export const DefaultOptions = {
// by default, spare the people we follow from getting blocked
blockFollowing: false,
skipVerified: 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",
],
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 = 1;
const ReasonNftAvatar = 1;
const ReasonMap = {
[ReasonBlueVerified]: "Twitter Blue verified",
[ReasonNftAvatar]: "NFT avatar",
};
const BlockCache = new Set();
export function ClearCache() {
BlockCache.clear();
}
export function BlockUser(user, user_id, headers, reason, attempt=1) {
if (BlockCache.has(user_id))
{ return; }
BlockCache.add(user_id);
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.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 skip-verified option
!(!options.skipVerified || !user.legacy.verified)
) {
console.log(`did not block Twitter Blue verified user ${user.legacy.name} (@${user.legacy.screen_name}) because they are verified through other means.`);
}
else {
BlockUser(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 {
BlockUser(user, String(user.rest_id), headers, ReasonNftAvatar);
}
}
}
export function ParseTimelineTweet(tweet, headers) {
let user = tweet;
for (const key of UserObjectPath) {
if (user.hasOwnProperty(key))
{ user = user[key]; }
}
if (user.__typename !== "User") {
console.error("could not parse tweet", tweet);
return;
}
BlockBlueVerified(user, headers)
}
export function HandleInstructionsResponse(e, body) {
// pull the "instructions" object from the tweet
let tweets = body;
try {
for (const key of InstructionsPaths[e.detail.parsedUrl[1]]) {
tweets = tweets[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
for (const value of tweets) {
if (value.type === "TimelineAddEntries") {
tweets = value;
break;
}
}
if (tweets.type !== "TimelineAddEntries") {
console.error('response object does not contain "TimelineAddEntries"', body);
return;
}
// 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":
return ParseTimelineTweet(tweet.content.itemContent, e.detail.request.headers);
case "TimelineTimelineModule":
for (const innerTweet of tweet.content.items) {
ParseTimelineTweet(innerTweet.item.itemContent, e.detail.request.headers)
}
return;
default:
if (!IgnoreTweetTypes.has(tweet.content.entryType)) {
console.error(`unexpected tweet type found: ${tweet.content.entryType}`, tweet);
}
}
}
}
export function HandleHomeTimeline(e, body) {
// 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: {
name: user.name,
screen_name: user.screen_name,
following: user?.following,
verified: user?.verified,
},
super_following: user.ext?.superFollowMetadata?.r?.ok?.superFollowing,
rest_id: user_id,
}, e.detail.request.headers)
}
}