-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathblacklist.user.js
272 lines (255 loc) · 9.77 KB
/
blacklist.user.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
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
// ==UserScript==
// @name Typeracer: Blacklist
// @namespace http://tampermonkey.net/
// @version 0.0.8
// @updateURL https://raw.githubusercontent.com/PoemOnTyperacer/tampermonkey/master/blacklist.user.js
// @downloadURL https://raw.githubusercontent.com/PoemOnTyperacer/tampermonkey/master/blacklist.user.js
// @description Hide guests/bots and users of your choice on maintrack/private tracks. Doesn't affect leaderboards, competitions, messages, or Race details pages
// @author poem#3305
// @match https://play.typeracer.com/*
// @match https://staging.typeracer.com/*
// @noframes
// ==/UserScript==
//settings
const DEBUG = false;
const RED_OUTLINE_MODE=false;
const WHITE_LIST_MODE = false; // if WHITE_LIST_MODE is set to true, the script will hide everyone _except_ players in the WHITE_LIST
const WHITE_LIST=[]; //eg: const WHITE_LIST=['poem','despot'];
// if not in WHITE_LIST_MODE, the script will show everyone _except_ players in the BLACK_LIST
const BLOCK_GUESTS=true; // Should the script hide guests
const BLACK_LIST=[]; //eg: const BLACK_LIST=['poem','despot'];
//no longer settings
const GUI_INTERVAL = 1000;
const ELEMENT_CONFIG = {childList: true, subtree: true, attributes: true, characterData: false};
const RANK_CONFIG = {characterData: false, attributes: false, childList: true, subtree: true};
let competitors = [];
let total_competitors=0;
let racing=false;
let standings=1;
let blocked_standings=0;
let queue=[];
let won=false;
function log(msg, highlight=false){
if(!DEBUG)
return;
if(highlight) {
console.log(("%c[TR Block] "+msg),"color:red;");
return
}
console.log('[Blacklist] '+msg)
}
//observe new player rows
function elementMutate(mutations_list) {
for(let j=0;j<mutations_list.length;j++) {
let mutation=mutations_list[j];
for(let i=0;i<mutation.addedNodes.length;i++) {
let added_node=mutation.addedNodes[i];
if(added_node.className=='row') {
if(!racing) {
log('In race');
log('competitors list length='+competitors.length+'; resetting');
disconnectRankObservers();
competitors=[];
total_competitors=0;
standings=1;
racing=true;
blocked_standings=0;
queue=[];
won=false;
}
competitors.push([added_node,null,null]);
let potential_username=added_node.querySelector('.lblUsername').innerText;
//hide any new row till the council decides its fate
if(RED_OUTLINE_MODE)
added_node.style.filter='opacity(0%)';
else if(potential_username!='you'&&potential_username!='(you)') {
added_node.style.position='absolute';
added_node.style.zIndex='-1';
added_node.style.top="-1000px";
}
//wait a second for the contents of the row to load before i process it
setTimeout(function() {onNewPlayerRow(total_competitors++);},1000);
}
}
}
}
const elementObserver = new MutationObserver(elementMutate);
elementObserver.observe(document.body, ELEMENT_CONFIG);
//decide if i wanna make the new row visible and if so bring it back from the shadow realm
function onNewPlayerRow(id) {
let row = competitors[id][0];
let labelUsername=row.querySelector('.lblUsername');
let statusLabels=document.getElementsByClassName('gameStatusLabel');
if(statusLabels.length==0) {
log('the race was cancelled since newplayerrow was called -- dropping new row');
return;
}
let statusLabel=statusLabels[0].innerText;
let username=labelUsername.innerText;
let blocked=false;
if(username[0]=='(')
username=username.slice(1,-1);
if(username=='you'||username=='(you)'||(username==''&&statusLabel=="Join this race, whenever you're ready...")) {
username='you';
log('player #'+id+' is self');
}
else if(username=='') {
username='guest_user';
log('player #'+id+' is a guest');
if(BLOCK_GUESTS) {
log('hiding guest user', true);
blocked=true;
if(RED_OUTLINE_MODE) {
row.style.border='4px solid red';
}
else
row.style.display='none';
}
}
else {
log('player #'+id+' is '+username);
if(WHITE_LIST_MODE) {
if(!WHITE_LIST.includes(username)){
log('hiding user (not whitelisted)', true);
blocked=true;
if(RED_OUTLINE_MODE) {
row.style.border='4px solid red';
}
else
row.style.display='none';
}
}
else {
if(BLACK_LIST.includes(username)) {
log('hiding blacklisted user', true);
blocked=true;
if(RED_OUTLINE_MODE) {
row.style.border='4px solid red';
}
else
row.style.display='none';
}
}
}
if(RED_OUTLINE_MODE)
row.style.filter='opacity(100%)';
if(!blocked) {
row.style.position='relative';
row.style.zIndex='0';
row.style.top="";
}
//also for each new row, start looking at the div where their rank will appear (1st 2nd etc)
let rankObserver = new MutationObserver(function(mutations_list) {rankMutate(mutations_list,id);});
rankObserver.observe(row.querySelector('.rank'), RANK_CONFIG)
competitors[id][1]=username;
competitors[id][2]=rankObserver;
}
function rankMutate(mutations_list, id) {
mutations_list.forEach(function(mutation) {
let textContent=mutation.addedNodes[0].textContent;
if(/^\d+$/.test(textContent[0])) {
let rank=parseInt(/^\d+/.exec(textContent[0])[0]);
let username=competitors[id][1];
log('player #'+id+', username='+username+', finished with rank '+rank);
competitors[id][2].disconnect();
if(username=='you') {
log('You finished');
racing=false;
}
editLabel(id,rank);
}
});
}
//if any players aren't done racing when u leave the race, stop observing their rank
function disconnectRankObservers() {
for(let i=0;i<competitors.length;i++) {
if(competitors[i][2]!=null)
competitors[i][2].disconnect();
}
}
//rewrite the ranks to exclude anyone on the blacklist
function editLabel(id,rank) {
let row = competitors[id][0];
let username = competitors[id][1];
let labelRank=row.querySelector('.rank');
if(blocked_standings+standings!=rank) { //if editLabel receives a player's rank in the wrong order, store them for later
queue.push([id,rank]);
log('queuing user #'+id+', username='+username+', rank='+rank+' received too early (standings='+standings+', blocked_standings='+blocked_standings+')');
return;
}
if((username=='guest_user'&&BLOCK_GUESTS)||(!WHITE_LIST_MODE&&BLACK_LIST.includes(username))||(WHITE_LIST_MODE&&!WHITE_LIST.includes(username)&&username!=='you')) {
log('[editlabel] '+username+' is either guest, not whitelisted, or blacklisted');
labelRank.innerText='X';
blocked_standings++;
}
else {
let output=ordinal_suffix_of(standings)+ ' Place';
if (standings==1)
output+='!';
else
output+='.';
labelRank.innerText=output;
if(username=='you'&&standings==1) {
won=true;
}
standings++;
}
for(let i=0;i<queue.length;i++) { //editLabel calls itself again for any queued player whose standing is now resolved
if(standings+blocked_standings==queue[i][1]) {
let params=queue.splice(i,1)[0];
log('standing resolved for player #'+params[0]+', username='+competitors[params[0]][1]+', rank='+params[1]+' (standings='+standings+', blocked_standings='+blocked_standings+')');
editLabel(params[0],params[1]);
return;
}
}
}
//thanks Salman A on stackoverflow
function ordinal_suffix_of(i) {
var j = i % 10,
k = i % 100;
if (j == 1 && k != 11) {
return i + "st";
}
if (j == 2 && k != 12) {
return i + "nd";
}
if (j == 3 && k != 13) {
return i + "rd";
}
return i + "th";
}
function GUIClock() {
//in case user quits before finishing the race
let gameStatusLabels = document.getElementsByClassName('gameStatusLabel');
let gameStatus = ((gameStatusLabels || [])[0] || {}).innerHTML || '';
if(racing&&((gameStatusLabels.length==0))){
racing = false;
log('Race cancelled');
}
//edit the gamestatuslabel so it doesn't snitch on the user's real rank, that would break the illusion,
if(gameStatusLabels.length!=0) {
let label=gameStatusLabels[0];
let new_label;
if(label.style.display!='none') {
new_label=label.cloneNode(true);
new_label.id='newlabel';
new_label.class='newlabel';
label.style.display='none';
label.parentNode.insertBefore(new_label, label.nextSibling);
}
else {
new_label=document.getElementById('newlabel');
}
if(gameStatus.startsWith('You finished')&&gameStatus!='You finished 1st!') {
if(won) {
log('Edited game status');
new_label.innerText='You finished 1st!';
}
else
new_label.innerText='The race has ended.';
}
else
new_label.innerText=gameStatus;
}
}
setInterval(GUIClock, GUI_INTERVAL);