-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathX_Ad_Banhammer.user.js
202 lines (177 loc) · 5.71 KB
/
X_Ad_Banhammer.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
// ==UserScript==
// @name X Ad Banhammer
// @namespace http://tampermonkey.net/
// @version 1.3.0
// @description Block advertising accounts on Twitter
// @author https://github.com/hoblin
// @downloadURL https://github.com/hoblin/x-ad-banhammer/raw/main/X_Ad_Banhammer.user.js
// @updateURL https://github.com/hoblin/x-ad-banhammer/raw/main/X_Ad_Banhammer.user.js
// @match https://twitter.com/*
// @match https://x.com/*
// @grant none
// @require https://code.jquery.com/jquery-3.6.0.min.js
// ==/UserScript==
(function ($) {
"use strict";
const DEBUG = false; // Set to false to disable logging
const maxAttempts = 20; // Number of attempts to find an element before giving up
let observer;
function log(...args) {
if (DEBUG) {
console.log(...args);
}
}
// Recently after blocking an ad,
// Twitter has been showing a premium plus offer.
// This function will decline it.
function declinePremiumPlus(attempts) {
log("Declining Premium Plus offer...");
if (attempts <= 0) {
log("Premium Plus offer was not found after multiple attempts");
// Reconnect observer
setupObserver();
return;
}
// Find <div role="button" with exact text "Maybe later"
const declineButton = $('button[role="button"]')
.filter(function () {
return $(this).text().trim() === "Maybe later";
})
.get(0);
if (declineButton) {
log("Maybe later button found:", declineButton);
// Maybe later button found, proceed to the next step
simulateClick(declineButton);
// Reconnect observer after final action
setupObserver();
} else {
log("Maybe later button not found, retrying...");
// Wait for 500ms before retrying
setTimeout(() => declinePremiumPlus(attempts - 1), 500);
}
}
function confirmBlock(attempts) {
log("Confirming block...");
if (attempts <= 0) {
log("Block not confirmed after multiple attempts");
// Reconnect observer
setupObserver();
return;
}
// Find <div role="button" with exact text "Block"
const confirmationModalButton = $('button[role="button"]')
.filter(function () {
return $(this).text().trim() === "Block";
})
.get(0);
if (confirmationModalButton) {
log("Block button found:", confirmationModalButton);
// Block button found, proceed to the next step
simulateClick(confirmationModalButton);
// Decline Premium Plus offer
declinePremiumPlus(maxAttempts);
} else {
log("Block button not found, retrying...");
// Wait for 500ms before retrying
setTimeout(() => confirmBlock(attempts - 1), 500);
}
}
function openMenu(attempts) {
if (attempts <= 0) {
log("Menu not opened after multiple attempts");
// Reconnect observer
setupObserver();
return;
}
// get menu item div with role="menuitem" containing text starting with "Block"
const blockOption = $('#layers div[role="menuitem"]')
.filter(function () {
return $(this).text().trim().startsWith("Block");
})
.get(0);
if (blockOption) {
log("Block option found:", blockOption);
// Block option found, proceed to the next step
simulateClick(blockOption);
confirmBlock(maxAttempts);
} else {
log("Block option not found, retrying...");
// Wait for 500ms before retrying
setTimeout(() => openMenu(attempts - 1), 500);
}
}
function findMenuButton(adSpan, attempts) {
if (attempts <= 0) {
log("Menu button not found after multiple attempts");
// Reconnect observer
setupObserver();
return;
}
const menuButton = $(adSpan)
.parent()
.parent()
.find('button[aria-label="More"][role="button"]')
.get(0);
if (menuButton) {
// Menu button found, proceed to the next step
log("Menu button found:", menuButton);
simulateClick(menuButton);
openMenu(maxAttempts);
} else {
log("Menu button not found, retrying...");
// Wait for 500ms before retrying
setTimeout(() => findMenuButton(adSpan, attempts - 1), 500);
}
}
function simulateClick(element) {
log("Simulating click on element:", element);
const event = new MouseEvent("click", {
view: window,
bubbles: true,
cancelable: true,
});
element.dispatchEvent(event);
}
function blockAd(adSpan) {
log("Blocking ad:", adSpan);
// extract the username text from the link to the user's profile
const username = $(adSpan)
.parent()
.parent()
.parent()
.parent()
.find('a[role="link"]')
.first()
.text()
.trim();
console.log("Blocking user:", username);
// Disconnect observer to prevent loop
observer.disconnect();
// Mark the ad as being processed to avoid re-processing it
$(adSpan).text("Ad - deleting");
findMenuButton(adSpan, maxAttempts);
}
function checkForAdsAndBlock() {
// find <span> with exact text "Ad" inside <div with aria-label starting with "Timeline"
const ads = $('div[aria-label^="Timeline"] span').filter(function () {
return $(this).text().trim() === "Ad";
});
ads.each(function (index, adSpan) {
blockAd(adSpan);
});
}
function setupObserver() {
observer = new MutationObserver((mutationsList) => {
for (let mutation of mutationsList) {
if (mutation.addedNodes.length) {
checkForAdsAndBlock();
}
}
});
observer.observe(document, { childList: true, subtree: true });
}
// Initial check for ads
checkForAdsAndBlock();
// Set up the observer to check for new ads when the DOM updates
setupObserver();
})(jQuery);