|
1 | 1 | /* |
2 | | - * BlockAdBlock 3.1.1 |
| 2 | + * BlockAdBlock 3.2.0 |
3 | 3 | * Copyright (c) 2015 Valentin Allaire <valentin.allaire@sitexw.fr> |
4 | 4 | * Released under the MIT license |
5 | 5 | * https://github.com/sitexw/BlockAdBlock |
|
13 | 13 | loopCheckTime: 50, |
14 | 14 | loopMaxNumber: 5, |
15 | 15 | baitClass: 'pub_300x250 pub_300x250m pub_728x90 text-ad textAd text_ad text_ads text-ads text-ad-links', |
16 | | - baitStyle: 'width: 1px !important; height: 1px !important; position: absolute !important; left: -10000px !important; top: -1000px !important;' |
| 16 | + baitStyle: 'width: 1px !important; height: 1px !important; position: absolute !important; left: -10000px !important; top: -1000px !important;', |
| 17 | + debug: false |
17 | 18 | }; |
18 | 19 | this._var = { |
19 | | - version: '3.1.1', |
| 20 | + version: '3.2.0', |
20 | 21 | bait: null, |
21 | 22 | checking: false, |
22 | 23 | loop: null, |
|
30 | 31 | var eventCallback = function() { |
31 | 32 | setTimeout(function() { |
32 | 33 | if(self._options.checkOnLoad === true) { |
| 34 | + if(self._options.debug === true) { |
| 35 | + self._log('onload->eventCallback', 'A check loading is launched'); |
| 36 | + } |
33 | 37 | if(self._var.bait === null) { |
34 | 38 | self._creatBait(); |
35 | 39 | } |
|
49 | 53 | BlockAdBlock.prototype._var = null; |
50 | 54 | BlockAdBlock.prototype._bait = null; |
51 | 55 |
|
| 56 | + BlockAdBlock.prototype._log = function(method, message) { |
| 57 | + console.log('[BlockAdBlock]['+method+'] '+message); |
| 58 | + }; |
| 59 | + |
52 | 60 | BlockAdBlock.prototype.setOption = function(options, value) { |
53 | 61 | if(value !== undefined) { |
54 | 62 | var key = options; |
|
57 | 65 | } |
58 | 66 | for(var option in options) { |
59 | 67 | this._options[option] = options[option]; |
| 68 | + if(this._options.debug === true) { |
| 69 | + this._log('setOption', 'The option "'+option+'" he was assigned to "'+options[option]+'"'); |
| 70 | + } |
60 | 71 | } |
61 | 72 | return this; |
62 | 73 | }; |
|
74 | 85 | this._var.bait.offsetWidth; |
75 | 86 | this._var.bait.clientHeight; |
76 | 87 | this._var.bait.clientWidth; |
| 88 | + |
| 89 | + if(this._options.debug === true) { |
| 90 | + this._log('_creatBait', 'Bait has been created'); |
| 91 | + } |
77 | 92 | }; |
78 | 93 | BlockAdBlock.prototype._destroyBait = function() { |
79 | 94 | window.document.body.removeChild(this._var.bait); |
80 | 95 | this._var.bait = null; |
| 96 | + |
| 97 | + if(this._options.debug === true) { |
| 98 | + this._log('_destroyBait', 'Bait has been removed'); |
| 99 | + } |
81 | 100 | }; |
82 | 101 |
|
83 | 102 | BlockAdBlock.prototype.check = function(loop) { |
84 | 103 | if(loop === undefined) { |
85 | 104 | loop = true; |
86 | 105 | } |
87 | 106 |
|
| 107 | + if(this._options.debug === true) { |
| 108 | + this._log('check', 'An audit was requested '+(loop===true?'with a':'without')+' loop'); |
| 109 | + } |
| 110 | + |
88 | 111 | if(this._var.checking === true) { |
| 112 | + if(this._options.debug === true) { |
| 113 | + this._log('check', 'A check was canceled because there is already an ongoing'); |
| 114 | + } |
89 | 115 | return false; |
90 | 116 | } |
91 | 117 | this._var.checking = true; |
|
101 | 127 | self._checkBait(loop); |
102 | 128 | }, this._options.loopCheckTime); |
103 | 129 | } |
104 | | - this._checkBait(loop); |
| 130 | + setTimeout(function() { |
| 131 | + self._checkBait(loop); |
| 132 | + }, 1); |
| 133 | + if(this._options.debug === true) { |
| 134 | + this._log('check', 'A check is in progress ...'); |
| 135 | + } |
105 | 136 |
|
106 | 137 | return true; |
107 | 138 | }; |
|
130 | 161 | } |
131 | 162 | } |
132 | 163 |
|
| 164 | + if(this._options.debug === true) { |
| 165 | + this._log('_checkBait', 'A check ('+(this._var.loopNumber+1)+'/'+this._options.loopMaxNumber+' ~'+(1+this._var.loopNumber*this._options.loopCheckTime)+'ms) was conducted and detection is '+(detected===true?'positive':'negative')); |
| 166 | + } |
| 167 | + |
133 | 168 | if(loop === true) { |
134 | 169 | this._var.loopNumber++; |
135 | 170 | if(this._var.loopNumber >= this._options.loopMaxNumber) { |
136 | | - clearInterval(this._var.loop); |
137 | | - this._var.loop = null; |
138 | | - this._var.loopNumber = 0; |
| 171 | + this._stopLoop(); |
139 | 172 | } |
140 | 173 | } |
141 | 174 |
|
142 | 175 | if(detected === true) { |
143 | | - if(loop === true) { |
144 | | - this._var.checking = false; |
145 | | - } |
| 176 | + this._stopLoop(); |
146 | 177 | this._destroyBait(); |
147 | 178 | this.emitEvent(true); |
148 | | - } else if(this._var.loop === null || loop === false) { |
149 | 179 | if(loop === true) { |
150 | 180 | this._var.checking = false; |
151 | 181 | } |
| 182 | + } else if(this._var.loop === null || loop === false) { |
152 | 183 | this._destroyBait(); |
153 | 184 | this.emitEvent(false); |
| 185 | + if(loop === true) { |
| 186 | + this._var.checking = false; |
| 187 | + } |
| 188 | + } |
| 189 | + }; |
| 190 | + BlockAdBlock.prototype._stopLoop = function(detected) { |
| 191 | + clearInterval(this._var.loop); |
| 192 | + this._var.loop = null; |
| 193 | + this._var.loopNumber = 0; |
| 194 | + |
| 195 | + if(this._options.debug === true) { |
| 196 | + this._log('_stopLoop', 'A loop has been stopped'); |
154 | 197 | } |
155 | 198 | }; |
156 | 199 |
|
157 | 200 | BlockAdBlock.prototype.emitEvent = function(detected) { |
| 201 | + if(this._options.debug === true) { |
| 202 | + this._log('emitEvent', 'An event with a '+(detected===true?'positive':'negative')+' detection was called'); |
| 203 | + } |
| 204 | + |
158 | 205 | var fns = this._var.event[(detected===true?'detected':'notDetected')]; |
159 | 206 | for(var i in fns) { |
| 207 | + if(this._options.debug === true) { |
| 208 | + this._log('emitEvent', 'Call function '+(parseInt(i)+1)+'/'+fns.length); |
| 209 | + } |
160 | 210 | if(fns.hasOwnProperty(i)) { |
161 | 211 | fns[i](); |
162 | 212 | } |
|
169 | 219 | BlockAdBlock.prototype.clearEvent = function() { |
170 | 220 | this._var.event.detected = []; |
171 | 221 | this._var.event.notDetected = []; |
| 222 | + |
| 223 | + if(this._options.debug === true) { |
| 224 | + this._log('clearEvent', 'The event list has been cleared'); |
| 225 | + } |
172 | 226 | }; |
173 | 227 |
|
174 | 228 | BlockAdBlock.prototype.on = function(detected, fn) { |
175 | 229 | this._var.event[(detected===true?'detected':'notDetected')].push(fn); |
| 230 | + if(this._options.debug === true) { |
| 231 | + this._log('on', 'A type of event "'+(detected===true?'detected':'notDetected')+'" was added'); |
| 232 | + } |
| 233 | + |
176 | 234 | return this; |
177 | 235 | }; |
178 | 236 | BlockAdBlock.prototype.onDetected = function(fn) { |
|
0 commit comments