-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdsBlock.user.js
More file actions
237 lines (179 loc) · 6.32 KB
/
AdsBlock.user.js
File metadata and controls
237 lines (179 loc) · 6.32 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
(function () {
'use strict';
class AdConfig {
static STYLE_ID = 'ads_block';
static SELECTORS = [
'#diep-io_728x90_2',
'#diep-io_300x250',
'#diep-io_300x250.active',
'.aa-unit.active'
];
static BLOCK_KEYWORDS = [
'doubleclick.net',
'googlesyndication',
'securepubads',
'pagead',
'adservice',
'adserver',
'adsystem',
'adnxs',
'adform',
'amazon-ads',
'ads',
'adsrvr',
'amp4ads',
'casalemedia',
'taboola',
'outbrain',
'rubiconproject'
];
static AMP_MARKERS = [
'i-amphtml-inabox',
'i-amphtml-singledoc',
'i-amphtml-standalone',
'amp4ads',
'i-amphtml-iframed'
];
}
class StyleManager {
static inject() {
if (document.getElementById(AdConfig.STYLE_ID)) return;
const style = document.createElement('style');
style.id = AdConfig.STYLE_ID;
style.textContent = `
${AdConfig.SELECTORS.join(',')} {
display:none !important;
visibility:hidden !important;
opacity:0 !important;
pointer-events:none !important;
}
`;
(document.head || document.documentElement).appendChild(style);
}
}
class AdDetector {
static isAdNode(node) {
if (!node || node.nodeType !== 1) return false;
const id = node.id || '';
if (id.startsWith('diep-io_728x90') || id.startsWith('diep-io_300x250')) return true;
if (node.matches?.(AdConfig.SELECTORS.join(','))) return true;
if (node.querySelector?.(AdConfig.SELECTORS.join(','))) return true;
return false;
}
static remove(node) {
if (!node) return false;
if (this.isAdNode(node)) {
node.remove?.() || node.parentNode?.removeChild(node);
return true;
}
return false;
}
}
class DOMInterceptor {
static patchDOM() {
this.patch('appendChild');
this.patch('insertBefore');
this.patch('replaceChild');
}
static patch(method) {
const original = Node.prototype[method];
Node.prototype[method] = function (...args) {
const node = args[0];
if (AdDetector.isAdNode(node)) return node;
return original.apply(this, args);
};
}
static patchAttributes() {
const original = Element.prototype.setAttribute;
Element.prototype.setAttribute = function (name, value) {
if (name === 'id') {
if (value.startsWith('diep-io_728x90') || value.startsWith('diep-io_300x250')) return;
}
if (name === 'class') {
const classes = value.split(/\s+/);
if (classes.includes('aa-unit') && classes.includes('active')) {
this.remove?.();
return;
}
}
return original.call(this, name, value);
};
}
}
class DOMWatcher {
static start() {
const observer = new MutationObserver(mutations => {
for (const mutation of mutations) {
for (const node of mutation.addedNodes) {
if (AdDetector.remove(node)) continue;
node.querySelectorAll?.(AdConfig.SELECTORS.join(',')).forEach(n => AdDetector.remove(n));
}
}
});
observer.observe(
document.documentElement,
{ childList: true, subtree: true }
);
}
}
class NetworkBlocker {
static isAMP() {
const cls = document.documentElement.className;
return AdConfig.AMP_MARKERS.some(marker => cls.includes(marker));
}
static isBlocked(url) {
if (!url) return false;
const s = String(url).toLowerCase();
return AdConfig.BLOCK_KEYWORDS.some(k => s.includes(k));
}
static patchFetch() {
const originalFetch = window.fetch;
window.fetch = function (resource, init) {
const url = resource?.url || resource;
if (NetworkBlocker.isAMP() && NetworkBlocker.isBlocked(url)) {
return Promise.resolve(new Response('', { status: 204 }));
}
return originalFetch.apply(this, arguments);
};
}
static patchXHR() {
const originalOpen = XMLHttpRequest.prototype.open;
const originalSend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.open = function (method, url) {
if (NetworkBlocker.isAMP() && NetworkBlocker.isBlocked(url)) this.blocked = true;
return originalOpen.apply(this, arguments);
};
XMLHttpRequest.prototype.send = function () {
if (this.blocked) {
this.abort();
return;
}
return originalSend.apply(this, arguments);
};
}
static patchIframe() {
const proto = HTMLIFrameElement?.prototype;
if (!proto) return;
const original = proto.setAttribute;
proto.setAttribute = function (name, value) {
if ((name === 'src' || name === 'srcdoc') && NetworkBlocker.isBlocked(value)) return;
return original.call(this, name, value);
};
}
static init() {
this.patchFetch();
this.patchXHR();
this.patchIframe();
}
}
class AdBlocker {
static init() {
StyleManager.inject();
DOMInterceptor.patchDOM();
DOMInterceptor.patchAttributes();
DOMWatcher.start();
NetworkBlocker.init();
}
}
AdBlocker.init();
})();