-
-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathblockBots.js
More file actions
162 lines (131 loc) · 5.55 KB
/
Copy pathblockBots.js
File metadata and controls
162 lines (131 loc) · 5.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
// Copyright (c) 2024-2026 nich (@nichxbt). Licensed under the Apache License, Version 2.0.
// scripts/blockBots.js
// Browser console script for detecting and blocking bot accounts on X/Twitter
// Paste in DevTools console on x.com/USERNAME/followers
// by nichxbt
(() => {
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
// =============================================
// CONFIGURATION
// =============================================
const CONFIG = {
maxBlocks: 10, // Max bots to block
dryRun: true, // SET FALSE TO EXECUTE
delay: 3000, // ms between blocks
scrollDelay: 2000, // ms to wait after scroll
maxScrollAttempts: 15, // Give up after N scrolls with no new users
thresholds: {
minReasons: 2, // Min reasons to flag as bot
digitRatio: 0.5, // Username digit ratio threshold
longNameDigits: 0.3, // Digit ratio for usernames > 14 chars
},
};
// =============================================
const download = (data, filename) => {
const a = document.createElement('a');
a.href = URL.createObjectURL(new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' }));
a.download = filename;
document.body.appendChild(a);
a.click();
a.remove();
console.log(`📥 Downloaded: ${filename}`);
};
const processed = new Set();
const detected = [];
let blocked = 0;
const extractBio = (cell) => {
const testId = cell.querySelector('[data-testid="UserDescription"]');
if (testId?.textContent?.trim()) return testId.textContent.trim();
const autoDir = cell.querySelector('[dir="auto"]:not([data-testid])');
if (autoDir?.textContent?.trim()?.length >= 10) return autoDir.textContent.trim();
return '';
};
const scoreBot = (cell) => {
const nameEl = cell.querySelector('[data-testid="User-Name"]');
if (!nameEl) return { isBot: false, reasons: [], username: null };
const fullText = nameEl.textContent || '';
const reasons = [];
// Default avatar check
const avatar = cell.querySelector('img[src*="default_profile"]');
if (avatar) reasons.push('default avatar');
// Username analysis
const usernameMatch = fullText.match(/@(\w+)/);
const username = usernameMatch ? usernameMatch[1] : null;
if (username) {
const digitRatio = (username.match(/\d/g) || []).length / username.length;
if (digitRatio > CONFIG.thresholds.digitRatio) reasons.push(`${(digitRatio * 100).toFixed(0)}% digits`);
if (username.length > 14 && digitRatio > CONFIG.thresholds.longNameDigits) reasons.push('long name + digits');
}
// No bio
const bio = extractBio(cell);
if (!bio) reasons.push('no bio');
// Zero followers/following
const spans = cell.querySelectorAll('span');
for (const span of spans) {
const t = span.textContent.toLowerCase();
if (t.includes('0 followers')) reasons.push('0 followers');
if (t.includes('0 following')) reasons.push('0 following');
}
return {
isBot: reasons.length >= CONFIG.thresholds.minReasons,
reasons,
username,
};
};
const run = async () => {
console.log('🤖 BLOCK BOTS — XActions by nichxbt');
console.log(`⚙️ Max blocks: ${CONFIG.maxBlocks} | Dry run: ${CONFIG.dryRun}`);
let scrollAttempts = 0;
while (blocked < CONFIG.maxBlocks && scrollAttempts < CONFIG.maxScrollAttempts) {
const cells = document.querySelectorAll('[data-testid="UserCell"]');
let foundNew = false;
for (const cell of cells) {
if (blocked >= CONFIG.maxBlocks) break;
const linkEl = cell.querySelector('a[href^="/"]');
const userId = linkEl?.href || '';
if (processed.has(userId)) continue;
processed.add(userId);
foundNew = true;
const result = scoreBot(cell);
if (!result.isBot || !result.username) continue;
detected.push(result);
console.log(`🤖 Bot: @${result.username} — ${result.reasons.join(', ')}`);
if (CONFIG.dryRun) {
blocked++;
continue;
}
// Block the bot
const moreBtn = cell.querySelector('[data-testid="userActions"]');
if (!moreBtn) continue;
moreBtn.click();
await sleep(800);
const menuItems = document.querySelectorAll('[role="menuitem"]');
let blockItem = null;
for (const item of menuItems) {
if (/\bblock\b/i.test(item.textContent)) { blockItem = item; break; }
}
if (!blockItem) { document.body.click(); await sleep(300); continue; }
blockItem.click();
await sleep(600);
const confirmBtn = document.querySelector('[data-testid="confirmationSheetConfirm"]');
if (confirmBtn) {
confirmBtn.click();
await sleep(500);
blocked++;
console.log(`🚫 Blocked @${result.username} [${blocked}/${CONFIG.maxBlocks}]`);
}
await sleep(CONFIG.delay);
}
if (!foundNew) scrollAttempts++; else scrollAttempts = 0;
window.scrollTo(0, document.body.scrollHeight);
await sleep(CONFIG.scrollDelay);
}
console.log(`\n✅ Done! Bots detected: ${detected.length} | Scanned: ${processed.size}`);
console.log(`📊 Bot rate: ${((detected.length / Math.max(processed.size, 1)) * 100).toFixed(1)}%`);
if (detected.length > 0) {
detected.forEach((b, i) => console.log(` ${i + 1}. @${b.username} — ${b.reasons.join(', ')}`));
download(detected, `xactions-bots-${new Date().toISOString().slice(0, 10)}.json`);
}
};
run();
})();