-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathadsblockerdetector.js
192 lines (176 loc) · 5.16 KB
/
adsblockerdetector.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
/*
* Ads Blocker Detector
*
* Author: PhDr. Matej Lednár, PhD.
* Website: work.mldgroup.com
* GitHub: https://github.com/matejlednar/adsblockerdetector
* Libraries required: jQuery
*/
// ads blocker detector
var _abd = {
_timeout: false, // timeout checker
_config: {
messages: {
"sk": "Prosím vypnite ads blocker v opačnom prípade nebude obsah stránky zobrazený.",
"en": "Please disable your ads blocker or this content won't be visible."
},
language: "all", // String - language | all
autoDetection: true, // Boolean - true | false
removeContent: true, // Boolean - true | false
callback: null, // Function - optional, only for detectBlocker() call
css: "external", // String - inline | external
timeout: 1000
},
/**
* Creates message from configuration
*
* @returns {String}
*/
_createMessage: function () {
var conf = this._config;
var language = conf.language;
var tableStyle = "";
var tdStyle = "";
var message = "";
if (this._config.css === "inline") {
tableStyle = "style='width:100%;height:100%;text-align:center'";
tdStyle = "style='vertical-align:middle'";
}
message = "<table class='ads-blocker-detector' " + tableStyle + "><tr><td " + tdStyle + ">";
if (language === "all") {
for (var prop in conf.messages) {
message += "<h1 class='abd-message'>" + conf.messages[prop] + "</h1>";
}
} else {
message += "<h1>" + conf.messages[language] + "</h1>";
}
message += "</td></tr></table>";
return message;
},
/**
* Default action
*
* If removeContent is true, remove displayed content.
*
* User can override default action with callback definition
*
* @returns {undefined}
*/
_performAction: function () {
var conf = this._config;
var body = document.body;
var html = document.getElementsByTagName("html")[0];
var div = document.createElement("div");
if (conf.removeContent) {
body.innerHTML = "";
}
if (conf.css === "external") {
html.classList.add("ads-blocker-detector");
body.classList.add("ads-blocker-detector");
div.classList.add("ads-blocker-detector");
}
if (conf.css === "inline") {
div.style.position = "absolute";
div.style.height = "100%";
div.style.width = "100%";
div.style.top = "0";
div.style.left = "0";
body.style.position = "relative";
body.style.height = "100%";
body.style.width = "100%";
html.style.height = "100%";
html.style.width = "100%";
html.style.overflow = "hidden";
}
div.innerHTML = this._createMessage();
body.appendChild(div);
},
/**
* Core
*
* Add blocker detection
*
* @returns {Boolean}
*/
_detectBlocker: function () {
var ad = $(".adsbygoogle")[0];
var detected = false;
var height = getComputedStyle(ad.parentNode).height;
if (parseInt(height) === 0) {
detected = true;
}
height = getComputedStyle(ad).height;
if (parseInt(height) === 0) {
detected = true;
}
var display = getComputedStyle(ad).display;
if (display === "none") {
detected = true;
}
var visibility = getComputedStyle(ad).visibility;
if (visibility === "hidden") {
detected = true;
}
if (ad.innerHTML === "") {
detected = true;
}
return detected;
},
/**
* Set configuration property
*
* @param {String} property
* @param {String} value
* @returns {undefined}
*/
set: function (property, value) {
this._config[property] = value;
},
/**
* Get configuration property
*
* @param {String} property
* @returns {_abd._config}
*/
get: function (property) {
return this._config[property];
},
/**
* Automatic start Ad Blocker detection
*
* @returns {undefined}
*/
start: function () {
var detected = this._detectBlocker();
if (this._config._timeout) {
if (this._config.callback) {
this._config.callback(detected);
return;
}
if (detected) {
this._performAction();
}
}
},
/**
* Detection after timeout without action
* Returns only true or false
*
* @returns {Boolean}
*/
detectBlocker: function () {
if (this._config._timeout) {
return this._detectBlocker();
} else {
console.warn("Wait for timeout please.");
}
}
};
$(document).ready(function () {
setTimeout(function () {
if (_abd._config.autoDetection) {
_abd._config._timeout = true;
_abd.start();
}
}, _abd._config.timeout);
});