-
Notifications
You must be signed in to change notification settings - Fork 30
/
prevent-bab.js
106 lines (98 loc) · 2.8 KB
/
prevent-bab.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
/* eslint-disable consistent-return, no-eval */
import { hit } from '../helpers';
/**
* @scriptlet prevent-bab
*
* @description
* Prevents BlockAdblock script from detecting an ad blocker.
*
* Related UBO scriptlet:
* https://github.com/gorhill/uBlock/wiki/Resources-Library#bab-defuserjs-
*
* It also can be used as `$redirect` sometimes.
* See [redirect description](../wiki/about-redirects.md#prevent-bab).
*
* ### Syntax
*
* ```adblock
* example.org#%#//scriptlet('prevent-bab')
* ```
*
* @added v1.0.4.
*/
export function preventBab(source) {
const nativeSetTimeout = window.setTimeout;
const babRegex = /\.bab_elementid.$/;
const timeoutWrapper = (callback, ...args) => {
if (typeof callback !== 'string' || !babRegex.test(callback)) {
return nativeSetTimeout.apply(window, [callback, ...args]);
}
hit(source);
};
window.setTimeout = timeoutWrapper;
const signatures = [
['blockadblock'],
['babasbm'],
[/getItem\('babn'\)/],
[
'getElementById',
'String.fromCharCode',
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
'charAt',
'DOMContentLoaded',
'AdBlock',
'addEventListener',
'doScroll',
'fromCharCode',
'<<2|r>>4',
'sessionStorage',
'clientWidth',
'localStorage',
'Math',
'random',
],
];
const check = (str) => {
if (typeof str !== 'string') {
return false;
}
for (let i = 0; i < signatures.length; i += 1) {
const tokens = signatures[i];
let match = 0;
for (let j = 0; j < tokens.length; j += 1) {
const token = tokens[j];
const found = token instanceof RegExp ? token.test(str) : str.includes(token);
if (found) {
match += 1;
}
}
if (match / tokens.length >= 0.8) {
return true;
}
}
return false;
};
const nativeEval = window.eval;
const evalWrapper = (str) => {
if (!check(str)) {
return nativeEval(str);
}
hit(source);
const bodyEl = document.body;
if (bodyEl) {
bodyEl.style.removeProperty('visibility');
}
const el = document.getElementById('babasbmsgx');
if (el) {
el.parentNode.removeChild(el);
}
};
window.eval = evalWrapper.bind(window);
}
export const preventBabNames = [
'prevent-bab',
// there are no aliases for this scriptlet
];
// eslint-disable-next-line prefer-destructuring
preventBab.primaryName = preventBabNames[0];
preventBab.injections = [hit];