-
Notifications
You must be signed in to change notification settings - Fork 2
/
ads-dom-remover-runner.js
executable file
·339 lines (297 loc) · 10.6 KB
/
ads-dom-remover-runner.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// ==UserScript==
// @name Ads DOM Remover Runner
// @namespace sagiegurari
// @version 0.14
// @author Sagie Gur-Ari
// @description Library - Removes Ad Containers from DOM (doesn't replace adblocker extension, but blocks dynamic content which the adblocker fails to block by removing whole sections from the HTML DOM.)
// @homepage https://github.com/sagiegurari/userscripts-ads-dom-remover
// @supportURL https://github.com/sagiegurari/userscripts-ads-dom-remover/issues
// @grant none
// @license MIT License
// ==/UserScript==
(function initADR() {
'use strict';
/**
* This class invokes the ads removing process based on provided options.
*
* @author Sagie Gur-Ari
* @class ADRService
* @public
* @param {object} $ - The jquery library
* @param {object} [options] - The process options
* @param {function} [options.getSelectorDefinitions] - Returns all selector definitions per host
* @param {number} [options.loops=10] - The amount of loops to run (will be invoked twice)
* @param {number} [options.interval=250] - Time in millies between each loop
* @param {number} [options.secondLoopInterval=2500] - Time in millies between each bulk of loops
* @param {function} [options.onDone] - Invoked at the end of all loops
*/
var Service = function ($, options) {
this.$ = $;
options = options || {};
this.loops = options.loops || 10;
this.interval = options.interval || 250;
this.secondLoopInterval = options.secondLoopInterval || 2500;
this.onDone = options.onDone || this.noop;
var hostname = document.location.hostname;
if (window.adrRunner && window.adrRunner.mockHostname) {
hostname = window.adrRunner.mockHostname;
}
var getSelectorDefinitions = options.getSelectorDefinitions || this.noop;
this.selectorDefinitions = getSelectorDefinitions() || {};
// find default selectors
this.defaultSelectors = null;
var index;
var id;
this.ids = Object.keys(this.selectorDefinitions);
for (index = 0; index < this.ids.length; index++) {
id = this.ids[index];
this.selectorDefinitions[id].id = id;
if (this.selectorDefinitions[id].hostNames === true) {
this.defaultSelectors = this.selectorDefinitions[id];
}
}
this.defaultSelectors = this.defaultSelectors || [];
this.state = {
intervalID: null,
counter: 0,
secondLoop: false
};
var selectors = this.getSelectors(hostname);
if (selectors) {
if ((!Array.isArray(selectors)) && selectors.selectors) {
if (selectors.id) {
console.debug(
'[user script][Ads DOM Remover][hideElements] Using Selectors:',
selectors.id
);
}
if (selectors.options) {
this.loops = selectors.options.loops || this.loops;
this.interval = selectors.options.interval || this.interval;
}
selectors = selectors.selectors;
}
this.state.currentSelectors = selectors;
}
};
/**
* Empty function
*
* @function
* @memberof! ADRService
* @private
* @returns {undefined} Always undefined
*/
Service.prototype.noop = function () {
return undefined;
};
/**
* Returns the selectors to remove based on the provided host name.
*
* @function
* @memberof! ADRService
* @private
* @param {string} hostName - The current host name
* @returns {Array} Array of selectors (or objects) to remove from the DOM
*/
Service.prototype.getSelectors = function (hostName) {
var selectors;
try {
var definitionIndex;
var hostNameIndex;
var id;
var hostNames;
for (definitionIndex = 0; definitionIndex < this.ids.length; definitionIndex++) {
id = this.ids[definitionIndex];
hostNames = this.selectorDefinitions[id].hostNames;
if ((typeof hostNames === 'string') && (hostName.indexOf(hostNames) !== -1)) {
selectors = this.selectorDefinitions[id];
} else if (Array.isArray(hostNames)) {
for (hostNameIndex = 0; hostNameIndex < hostNames.length; hostNameIndex++) {
if (hostName.indexOf(hostNames[hostNameIndex]) !== -1) {
selectors = this.selectorDefinitions[id];
break;
}
}
}
if (selectors) {
break;
}
}
} catch (error) {
console.error(
'[user script][Ads DOM Remover][getSelectors] Error:',
error
);
}
if (!selectors) {
selectors = this.defaultSelectors;
}
return selectors || [];
};
/**
* Hides the elements by removing them from the DOM completely.
*
* @function
* @memberof! ADRService
* @private
* @returns {boolean} True if any element was removed during this invocation
*/
Service.prototype.hideElements = function () {
var self = this;
var found = false;
var selectors = self.state.currentSelectors || [];
selectors.forEach(function (selector) {
var selectorString = selector.selector || selector;
var $element;
try {
$element = self.$(selectorString);
} catch (error) {
console.error(
'[user script][Ads DOM Remover][hideElements] Error while running selector:',
selectorString,
error
);
}
if ($element && $element.length) {
found = true;
if (selector.fineTuneSelector && (typeof selector.fineTuneSelector === 'function')) {
$element = selector.fineTuneSelector($element);
}
if (selector.pre && (typeof selector.pre === 'function')) {
selector.pre($element);
}
var remove = true;
if (selector.filter && (typeof selector.filter === 'function')) {
$element = selector.filter($element);
remove = !!$element.length;
}
if (remove) {
$element.removeAttr('style');
$element.css(
'display',
'none !important'
);
$element.remove();
console.debug(
'[user script][Ads DOM Remover][hideElements] Found:',
selector,
'count:',
$element.length,
'in website and removed it.'
);
} else {
console.debug(
'[user script][Ads DOM Remover][hideElements] Found:',
selector,
'count:',
$element.length,
'in website but not removing.'
);
}
}
});
return found;
};
/**
* Invoked each interval to run the main library logic.
*
* @function
* @memberof! ADRService
* @private
*/
Service.prototype.actionLoop = function () {
this.state.counter++;
var stopInterval = this.state.intervalID && (this.state.counter > this.loops);
console.debug(
'[user script][Ads DOM Remover][actionLoop] Running loop:',
this.state.counter,
'state:',
this.state,
'stop interval:',
stopInterval
);
this.hideElements();
if (stopInterval) {
console.debug(
'[user script][Ads DOM Remover][actionLoop] Clearing interval ID:',
this.state.intervalID
);
clearInterval(this.state.intervalID);
this.state.intervalID = null;
if (!this.state.secondLoop) {
this.startActionLoop(
false,
this.secondLoopInterval
);
} else {
this.onDone();
}
}
};
/**
* Starts the interval invocations.
*
* @function
* @memberof! ADRService
* @private
* @param {boolean} firstTime - If this function was called for the first time
* @param {number} delay - The delay to wait before setting the interval
*/
Service.prototype.startActionLoop = function (firstTime, delay) {
var self = this;
self.state.secondLoop = !firstTime;
self.state.counter = 0;
setTimeout(
function () {
self.state.intervalID = setInterval(
function () {
self.actionLoop();
},
self.interval
);
setTimeout(
function () {
self.hideElements();
},
15000
);
},
delay
);
};
/**
* Starts the service run.
*
* @function
* @memberof! ADRService
* @private
*/
Service.prototype.start = function () {
this.startActionLoop(
true,
10
);
};
/**
* Starts the service run.
*
* @function
* @namespace ADRRunner
* @public
* @param {object} $ - The jquery library
* @param {object} options - The process options
* @param {function} options.getSelectorDefinitions - Returns all selector definitions per host
* @param {number} [options.loops=10] - The amount of loops to run (will be invoked twice)
* @param {number} [options.interval=250] - Time in millies between each loop
*/
window.adrRunner = function adrRunner($, options) {
if ($ && (typeof $ === 'function') && options && options.getSelectorDefinitions && (typeof options.getSelectorDefinitions === 'function')) {
var service = new Service(
$,
options
);
service.start();
}
};
}());