BlockAdBlock 3.2.1


SUBMITTED BY: Guest

DATE: June 12, 2016, 1:28 a.m.

FORMAT: Text only

SIZE: 7.2 kB

HITS: 447

  1. /*
  2. * BlockAdBlock 3.2.1
  3. * Copyright (c) 2015 Valentin Allaire <valentin.allaire@sitexw.fr>
  4. * Released under the MIT license
  5. * https://github.com/sitexw/BlockAdBlock
  6. */
  7. (function(window) {
  8. var BlockAdBlock = function(options) {
  9. this._options = {
  10. checkOnLoad: false,
  11. resetOnEnd: false,
  12. loopCheckTime: 50,
  13. loopMaxNumber: 5,
  14. baitClass: 'pub_300x250 pub_300x250m pub_728x90 text-ad textAd text_ad text_ads text-ads text-ad-links',
  15. baitStyle: 'width: 1px !important; height: 1px !important; position: absolute !important; left: -10000px !important; top: -1000px !important;',
  16. debug: false
  17. };
  18. this._var = {
  19. version: '3.2.1',
  20. bait: null,
  21. checking: false,
  22. loop: null,
  23. loopNumber: 0,
  24. event: { detected: [], notDetected: [] }
  25. };
  26. if(options !== undefined) {
  27. this.setOption(options);
  28. }
  29. var self = this;
  30. var eventCallback = function() {
  31. setTimeout(function() {
  32. if(self._options.checkOnLoad === true) {
  33. if(self._options.debug === true) {
  34. self._log('onload->eventCallback', 'A check loading is launched');
  35. }
  36. if(self._var.bait === null) {
  37. self._creatBait();
  38. }
  39. setTimeout(function() {
  40. self.check();
  41. }, 1);
  42. }
  43. }, 1);
  44. };
  45. if(window.addEventListener !== undefined) {
  46. window.addEventListener('load', eventCallback, false);
  47. } else {
  48. window.attachEvent('onload', eventCallback);
  49. }
  50. };
  51. BlockAdBlock.prototype._options = null;
  52. BlockAdBlock.prototype._var = null;
  53. BlockAdBlock.prototype._bait = null;
  54. BlockAdBlock.prototype._log = function(method, message) {
  55. console.log('[BlockAdBlock]['+method+'] '+message);
  56. };
  57. BlockAdBlock.prototype.setOption = function(options, value) {
  58. if(value !== undefined) {
  59. var key = options;
  60. options = {};
  61. options[key] = value;
  62. }
  63. for(var option in options) {
  64. this._options[option] = options[option];
  65. if(this._options.debug === true) {
  66. this._log('setOption', 'The option "'+option+'" he was assigned to "'+options[option]+'"');
  67. }
  68. }
  69. return this;
  70. };
  71. BlockAdBlock.prototype._creatBait = function() {
  72. var bait = document.createElement('div');
  73. bait.setAttribute('class', this._options.baitClass);
  74. bait.setAttribute('style', this._options.baitStyle);
  75. this._var.bait = window.document.body.appendChild(bait);
  76. this._var.bait.offsetParent;
  77. this._var.bait.offsetHeight;
  78. this._var.bait.offsetLeft;
  79. this._var.bait.offsetTop;
  80. this._var.bait.offsetWidth;
  81. this._var.bait.clientHeight;
  82. this._var.bait.clientWidth;
  83. if(this._options.debug === true) {
  84. this._log('_creatBait', 'Bait has been created');
  85. }
  86. };
  87. BlockAdBlock.prototype._destroyBait = function() {
  88. window.document.body.removeChild(this._var.bait);
  89. this._var.bait = null;
  90. if(this._options.debug === true) {
  91. this._log('_destroyBait', 'Bait has been removed');
  92. }
  93. };
  94. BlockAdBlock.prototype.check = function(loop) {
  95. if(loop === undefined) {
  96. loop = true;
  97. }
  98. if(this._options.debug === true) {
  99. this._log('check', 'An audit was requested '+(loop===true?'with a':'without')+' loop');
  100. }
  101. if(this._var.checking === true) {
  102. if(this._options.debug === true) {
  103. this._log('check', 'A check was canceled because there is already an ongoing');
  104. }
  105. return false;
  106. }
  107. this._var.checking = true;
  108. if(this._var.bait === null) {
  109. this._creatBait();
  110. }
  111. var self = this;
  112. this._var.loopNumber = 0;
  113. if(loop === true) {
  114. this._var.loop = setInterval(function() {
  115. self._checkBait(loop);
  116. }, this._options.loopCheckTime);
  117. }
  118. setTimeout(function() {
  119. self._checkBait(loop);
  120. }, 1);
  121. if(this._options.debug === true) {
  122. this._log('check', 'A check is in progress ...');
  123. }
  124. return true;
  125. };
  126. BlockAdBlock.prototype._checkBait = function(loop) {
  127. var detected = false;
  128. if(this._var.bait === null) {
  129. this._creatBait();
  130. }
  131. if(window.document.body.getAttribute('abp') !== null
  132. || this._var.bait.offsetParent === null
  133. || this._var.bait.offsetHeight == 0
  134. || this._var.bait.offsetLeft == 0
  135. || this._var.bait.offsetTop == 0
  136. || this._var.bait.offsetWidth == 0
  137. || this._var.bait.clientHeight == 0
  138. || this._var.bait.clientWidth == 0) {
  139. detected = true;
  140. }
  141. if(window.getComputedStyle !== undefined) {
  142. var baitTemp = window.getComputedStyle(this._var.bait, null);
  143. if(baitTemp && (baitTemp.getPropertyValue('display') == 'none' || baitTemp.getPropertyValue('visibility') == 'hidden')) {
  144. detected = true;
  145. }
  146. }
  147. if(this._options.debug === true) {
  148. 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'));
  149. }
  150. if(loop === true) {
  151. this._var.loopNumber++;
  152. if(this._var.loopNumber >= this._options.loopMaxNumber) {
  153. this._stopLoop();
  154. }
  155. }
  156. if(detected === true) {
  157. this._stopLoop();
  158. this._destroyBait();
  159. this.emitEvent(true);
  160. if(loop === true) {
  161. this._var.checking = false;
  162. }
  163. } else if(this._var.loop === null || loop === false) {
  164. this._destroyBait();
  165. this.emitEvent(false);
  166. if(loop === true) {
  167. this._var.checking = false;
  168. }
  169. }
  170. };
  171. BlockAdBlock.prototype._stopLoop = function(detected) {
  172. clearInterval(this._var.loop);
  173. this._var.loop = null;
  174. this._var.loopNumber = 0;
  175. if(this._options.debug === true) {
  176. this._log('_stopLoop', 'A loop has been stopped');
  177. }
  178. };
  179. BlockAdBlock.prototype.emitEvent = function(detected) {
  180. if(this._options.debug === true) {
  181. this._log('emitEvent', 'An event with a '+(detected===true?'positive':'negative')+' detection was called');
  182. }
  183. var fns = this._var.event[(detected===true?'detected':'notDetected')];
  184. for(var i in fns) {
  185. if(this._options.debug === true) {
  186. this._log('emitEvent', 'Call function '+(parseInt(i)+1)+'/'+fns.length);
  187. }
  188. if(fns.hasOwnProperty(i)) {
  189. fns[i]();
  190. }
  191. }
  192. if(this._options.resetOnEnd === true) {
  193. this.clearEvent();
  194. }
  195. return this;
  196. };
  197. BlockAdBlock.prototype.clearEvent = function() {
  198. this._var.event.detected = [];
  199. this._var.event.notDetected = [];
  200. if(this._options.debug === true) {
  201. this._log('clearEvent', 'The event list has been cleared');
  202. }
  203. };
  204. BlockAdBlock.prototype.on = function(detected, fn) {
  205. this._var.event[(detected===true?'detected':'notDetected')].push(fn);
  206. if(this._options.debug === true) {
  207. this._log('on', 'A type of event "'+(detected===true?'detected':'notDetected')+'" was added');
  208. }
  209. return this;
  210. };
  211. BlockAdBlock.prototype.onDetected = function(fn) {
  212. return this.on(true, fn);
  213. };
  214. BlockAdBlock.prototype.onNotDetected = function(fn) {
  215. return this.on(false, fn);
  216. };
  217. window.BlockAdBlock = BlockAdBlock;
  218. if(window.blockAdBlock === undefined) {
  219. window.blockAdBlock = new BlockAdBlock({
  220. checkOnLoad: true,
  221. resetOnEnd: true
  222. });
  223. }
  224. })(window);

comments powered by Disqus