-
Notifications
You must be signed in to change notification settings - Fork 12
/
abDetector.js
80 lines (76 loc) · 1.88 KB
/
abDetector.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
/**
* ==============
* abDetector.js
* ==============
*
* @author: Nick Rameau
* @github: http://github.com/R4meau
* @twitter: http://twitter.com/R4meau
* @contact: [email protected]
*
*
*/
window.onload = () => {
const iframe = document.createElement('iframe');
const randomNum = Math.floor(Math.random() * (10000 - 123 + 1)) + 123;
const protocol = window.location.protocol;
/*
*
* Creates a fake iframe, with a fake random url
* an id and gives it the minimum size
*
*/
iframe.src = `${protocol}//google.com/${randomNum}/ads.html`;
iframe.height = ".1px";
iframe.width = ".1px";
iframe.id = 'some-ad';
/*
*
* Appends the iframe to the body only if it loaded successfully
* or the request got aborted due to some error inside the iframe (404)
*
*/
getRequest(iframe, response => { // On Success
document.body.appendChild(iframe);
}, (xhr, status) => { // On error
if (status === 0) // Request aborted
document.body.appendChild(iframe);
});
/*
*
* Takes 500 ms to run the code
* Selects the iframe from the body
* Runs some tests to know if it's being hidden or not
*
*/
setTimeout(() => {
const someAd = document.getElementById('some-ad');
if (someAd === null ||
someAd.style.display == "none" ||
someAd.style.display == "hidden" ||
someAd.style.visibility == "hidden" ||
someAd.offsetHeight === 0)
document.getElementById('ab-message').style.display = 'block';
else
someAd.remove();
}, 500);
};
/*
*
* Uses XmlHttpRequest to load the iframe
* Takes a success and an error function
* and calls them respectively
*
*/
function getRequest(iframe, success, error) {
const xhr = new XMLHttpRequest();
xhr.open("GET", iframe.src);
xhr.onreadystatechange = () => {
if (xhr.readyState == 4)
if (xhr.status == 200) // Loaded successfully
success(xhr.responseText);
else // For any other error
error(xhr, xhr.status);
};
xhr.send();
}