AdBlock+ Press Alt+Shift+B

AdBlock+. Press Alt+Shift+B for blocking ads and Alt+Shift+U for unblocking. Press Alt+Shift+E for editing styles.

  1. // ==UserScript==
  2. // @name AdBlock+ Press Alt+Shift+B
  3. // @editor Raphahxb (xboxbr)
  4. // @version 1.01
  5. // @include *
  6. // @description AdBlock+. Press Alt+Shift+B for blocking ads and Alt+Shift+U for unblocking. Press Alt+Shift+E for editing styles.
  7. // @namespace https://greasyfork.org/users/7000
  8. // @ujs:download
  9. // ==/UserScript==
  10.  
  11. (function(){
  12. var style, enabled = false, prefix = 'ujs_adblock';
  13. var none = '{display: none !important;}', highlight = '{background-color: #FF5555 !important; outline: 1px solid #FF1111 !important; opacity: 0.7 !important;}';
  14.  
  15. var getValue = function(name){
  16. if(window.localStorage){
  17. return window.localStorage.getItem(name) || '';
  18. }
  19. else{
  20. var eq = name+'=', ca = document.cookie.split(';');
  21. for(var i = ca.length; i--;){
  22. var c = ca[i];
  23. while(c.charAt(0) == ' ')c = c.slice(1);
  24. if(c.indexOf(eq) == 0)return unescape(c.slice(eq.length));
  25. };
  26. return '';
  27. }
  28. };
  29. var setValue = function(name, value, del){
  30. if(window.localStorage){
  31. if(del){window.localStorage.removeItem(name)}else{window.localStorage.setItem(name, value)};
  32. }
  33. else{
  34. if(document.cookie.split(';').length < 30 && document.cookie.length-escape(getValue(name)).length+escape(value).length < 4000){
  35. var date = new Date();
  36. date.setTime(date.getTime()+((del ? -1 : 10*365)*24*60*60*1000));
  37. document.cookie = name+'='+escape(value)+'; expires='+date.toGMTString()+'; path=/';
  38. }
  39. else{
  40. alert('Cookies is full!');
  41. }
  42. }
  43. };
  44. var addStyle = function(css){
  45. var s = document.createElement('style');
  46. s.setAttribute('type', 'text/css');
  47. s.setAttribute('style', 'display: none !important;');
  48. s.appendChild(document.createTextNode(css));
  49. return (document.getElementsByTagName('head')[0] || document.documentElement).appendChild(s);
  50. };
  51. var replaceStyle = function(ele, css){
  52. if(ele){
  53. if(ele.firstChild)ele.removeChild(ele.firstChild);
  54. ele.appendChild(document.createTextNode(css));
  55. }
  56. };
  57. var splitCss = function(css){
  58. var rez = [];
  59. css.replace(/(([\w#:.~>+()\s-]+|\*|\[.*?\])+)\s*(,|$)/g, function(s, m){rez.push(m.replace(/^\s+|\s+$/g, ''))});
  60. return rez;
  61. };
  62. var clearCss = function(css){
  63. var a = splitCss(css);
  64. for(var i = a.length; i--;){
  65. var rule = a[i]+'>';
  66. for(var j = a.length; j--;){
  67. if(a[j].indexOf(rule) == 0)a.splice(j, 1);
  68. }
  69. };
  70. return a.join(',');
  71. };
  72. var delCss = function(css, del){
  73. var a = splitCss(css);
  74. if(del){
  75. for(var i = a.length; i--;){
  76. if(del.indexOf(a[i]) == 0)a.splice(i, 1);
  77. }
  78. }
  79. else{
  80. a.pop();
  81. };
  82. return a.join(',');
  83. };
  84. var getAtt = function(el, tags){
  85. var rez = '';
  86. if(el.attributes){
  87. var r = new RegExp('^('+tags+')$');
  88. for(var i = 0, a; a = el.attributes[i]; i++){
  89. var n = a.nodeName.toLowerCase();
  90. if(r.test(n))rez += '['+n+'=\x22'+a.nodeValue.replace(/[\x22\x5C]/g, '\\$&')+'\x22]';
  91. }
  92. };
  93. return rez;
  94. };
  95. var getNth = function(el){
  96. var nth, n = 0, p = el.parentNode;
  97. for(var i = 0, c; c = p.childNodes[i]; i++){if(c.nodeType == 1){n++; if(c == el)nth = n}};
  98. return (!nth || n < 2) ? '' : ':nth-child('+nth+')';
  99. };
  100. var getCssRules = function(el, wide){
  101. var att, tag, rez = [];
  102. var ver = window.postMessage ? 9.5 : (window.getSelection ? 9 : 8);
  103. while(el){
  104. if(el.nodeType == 1){
  105. att = getAtt(el, 'src') || getAtt(el, 'href');
  106. tag = el.nodeName;
  107. if(att){
  108. rez.unshift(tag+((wide && ver >= 9) ? att.replace(/^(\[\w+)(=\x22[^?#]+\/).*(\x22\])$/, '$1^$2$3') : att));
  109. break;
  110. }
  111. else{
  112. rez.unshift(tag+getAtt(el, 'id|class|height|width|color|bgcolor'+(ver >= 9 ? '|align|valign|type' : ''))+((wide || ver < 9.5 || /^(html|body)$/i.test(tag)) ? '' : getNth(el)));
  113. }
  114. };
  115. el = el.parentNode;
  116. };
  117. return rez.join('>');
  118. };
  119. var setBlockStyle = function(){
  120. if(document.documentElement instanceof HTMLHtmlElement){
  121. var css = getValue(prefix);
  122. if(css)style = addStyle(css+none);
  123. return true;
  124. }
  125. };
  126.  
  127. var editStyles = function(){
  128. var rez = prompt(window.navigator.language == 'ru' ? '\u041F\u043E\u0436\u0430\u043B\u0443\u0439\u0441\u0442\u0430, \u043E\u0442\u0440\u0435\u0434\u0430\u043A\u0442\u0438\u0440\u0443\u0439\u0442\u0435 \u0441\u0442\u0438\u043B\u0438:' : 'Please, edit styles:', getValue(prefix));
  129. if(rez != null){
  130. setValue(prefix, rez);
  131. if(rez)rez += none;
  132. if(style){replaceStyle(style, rez)}else{style = addStyle(rez)};
  133. }
  134. };
  135. var unblockEle = function(latest){
  136. var css = getValue(prefix);
  137. if(enabled || !style || !css)return;
  138.  
  139. var remove = function(){
  140. document.removeEventListener('click', click, false);
  141. document.removeEventListener('keyup', press, false);
  142. enabled = false;
  143. };
  144. var click = function(ev){
  145. ev.preventDefault();
  146. var oldCss = getValue(prefix);
  147. var css = delCss(oldCss, getCssRules(ev.target, false));
  148. if(css == oldCss)css = delCss(oldCss, getCssRules(ev.target, true));
  149. if(css != oldCss){
  150. setValue(prefix, css);
  151. replaceStyle(style, css ? css+none : '');
  152. remove();
  153. }
  154. };
  155. var press = function(ev){
  156. if(ev.keyCode == 27){
  157. var css = getValue(prefix);
  158. replaceStyle(style, css ? css+none : '');
  159. remove();
  160. }
  161. };
  162.  
  163. if(latest){
  164. css = delCss(css);
  165. setValue(prefix, css);
  166. replaceStyle(style, css ? css+none : '');
  167. }
  168. else{
  169. enabled = true;
  170. replaceStyle(style, css+highlight);
  171. document.addEventListener('click', click, false);
  172. document.addEventListener('keyup', press, false);
  173. }
  174. };
  175. var blockEle = function(wide){
  176. if(enabled)return;
  177. var ele = '', outline = '', border = '', bgColor = '', title = '', reObjects = /^(iframe|object|embed)$/i;
  178.  
  179. var remove = function(){
  180. document.removeEventListener('mouseover', over, false);
  181. document.removeEventListener('mouseout', out, false);
  182. document.removeEventListener('click', click, false);
  183. document.removeEventListener('keyup', press, false);
  184. enabled = false;
  185. };
  186. var over = function(ev){
  187. ele = ev.target;
  188. title = ele.title;
  189. ele.title = 'Tag: '+ele.nodeName+(ele.id ? ', ID: '+ele.id : '')+(ele.className ? ', Class: '+ele.className : '');
  190. if(reObjects.test(ele.nodeName)){
  191. border = ele.style.border;
  192. ele.style.border = '2px solid #306EFF';
  193. }
  194. else{
  195. outline = ele.style.outline;
  196. ele.style.outline = '1px solid #306EFF';
  197. bgColor = ele.style.backgroundColor;
  198. ele.style.backgroundColor = '#C6DEFF';
  199. }
  200. };
  201. var out = function(){
  202. if(ele){
  203. ele.title = title;
  204. if(reObjects.test(ele.nodeName)){
  205. ele.style.border = border;
  206. }
  207. else{
  208. ele.style.outline = outline;
  209. ele.style.backgroundColor = bgColor;
  210. }
  211. }
  212. };
  213. var click = function(ev){
  214. if(ele){
  215. ev.preventDefault();
  216. out();
  217. remove();
  218. var css = getCssRules(ele, wide || ev.altKey);
  219. var tmpCss = addStyle(css+highlight);
  220. css = prompt(window.navigator.language == 'ru' ? '\u0417\u0430\u0431\u043B\u043E\u043A\u0438\u0440\u043E\u0432\u0430\u0442\u044C \u0434\u0430\u043D\u043D\u044B\u0439 \u044D\u043B\u0435\u043C\u0435\u043D\u0442?' : 'Block this element(s)?', css);
  221. if(css){
  222. var oldCss = getValue(prefix);
  223. if(oldCss)css = clearCss(oldCss+','+css);
  224. setValue(prefix, css);
  225. if(style){replaceStyle(style, css+none)}else{style = addStyle(css+none)};
  226. };
  227. tmpCss.parentNode.removeChild(tmpCss);
  228. }
  229. };
  230. var press = function(ev){
  231. if(ev.keyCode == 27){
  232. out();
  233. remove();
  234. }
  235. };
  236.  
  237. enabled = true;
  238. document.addEventListener('mouseover', over, false);
  239. document.addEventListener('mouseout', out, false);
  240. document.addEventListener('click', click, false);
  241. document.addEventListener('keyup', press, false);
  242. };
  243.  
  244.  
  245. // Set style at loading page (with workaround for Opera 8)
  246. if(!setBlockStyle())window.addEventListener('load', setBlockStyle, false);
  247.  
  248. // Hotkeys
  249. document.addEventListener('keydown', function(e){
  250. if(e.shiftKey && !e.ctrlKey && e.altKey){
  251. switch(e.keyCode){
  252. // Edit styles with Alt+Shift+E
  253. case 69: editStyles(); break;
  254. // Unblock elements with Alt+Shift+U
  255. case 85: unblockEle(); break;
  256. // Block element with Alt+Shift+B
  257. case 66: blockEle(); break;
  258. // Unblock latest element with Alt+Shift+L
  259. case 76: unblockEle(true); break;
  260. // Block elements (don't use nth-child) with Alt+Shift+W
  261. case 87: blockEle(true); break;
  262. }
  263. }
  264. }, false);
  265.  
  266. // For buttons
  267. window.navigator.ujs_adblock = {block: blockEle, edit: editStyles, unblock: unblockEle};
  268. })();