RU AdList JS Fixes

try to take over the world!

当前为 2016-06-20 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name RU AdList JS Fixes
  3. // @namespace ruadlist_js_fixes
  4. // @version 0.8.1
  5. // @description try to take over the world!
  6. // @author lainverse & dimisa
  7. // @match *://*/*
  8. // @grant none
  9. // @run-at document-start
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. if (!/firefox/i.test(navigator.userAgent)) { // scripts for non-Firefox browsers
  16. // https://greasyfork.org/scripts/19144-websuckit/
  17. (function() {
  18. // check if the browser supports Proxy and WebSocket
  19. if (!window.Proxy || !window.WebSocket) return;
  20. var to_block = [
  21. '||brokeloy.com^',
  22. '||et-code.ru^',
  23. '||hghit.com^',
  24. '||mail.ru^',
  25. '||marketgid.com^',
  26. '||mxtads.com^',
  27. '||torvind.com^',
  28. '||trafmag.com^'
  29. ], masks = [];
  30. to_block.forEach(function(m){
  31. masks.push(new RegExp(
  32. (m.substr(0,2) === '||' ? '^wss?:\/+([^/.]+\\.)*' : '') +
  33. m.replace(/\|\||\^/g,'').replace(/[./]/g, function(ch){
  34. return '\\' + ch;
  35. }) +
  36. (m.substr(-1) === '^' ? '[:/]' : ''), 'i'));
  37. });
  38. var ws = window.WebSocket;
  39. window.WebSocket = new Proxy(ws, {
  40. construct: function(e,i){
  41. console.log('Opening WebSocket on', i[0], '…');
  42. var j = masks.length;
  43. while(j--)
  44. if (masks[j].test(i[0])) {
  45. console.log("Nope! Not gonna happen!");
  46. return {};
  47. }
  48. return new e(i[0],i[1]);
  49. }
  50. });
  51. })();
  52. // https://greasyfork.org/scripts/14720-it-s-not-important
  53. (function(){
  54. var imptt = /((display|(margin|padding)(-top|-bottom)?)\s*:[^;!]*)!\s*important/ig,
  55. rplsf = function(str,grp){return grp;};
  56.  
  57. function unimportanter(el, si) {
  58. if (!imptt.test(si) || el.style.display == 'none')
  59. return 0; // get out if we have nothing to do here
  60. var so = si.replace(imptt, rplsf), ret = 0;
  61. if (si != so) {
  62. ret = 1;
  63. el.setAttribute('style', so);
  64. }
  65. return ret;
  66. }
  67.  
  68. function logger(c) {
  69. if (c) console.log('Some page elements became a bit less important.');
  70. }
  71.  
  72. function checkTarget(m, c) {
  73. var si = m.getAttribute ? m.getAttribute('style') : null;
  74. if (si && si.indexOf('!') > -1)
  75. c+=unimportanter(m, si);
  76. return c;
  77. }
  78.  
  79. function checkNodes(m, c) {
  80. var i = m.length;
  81. while(i--)
  82. c = checkTarget(m[i], c);
  83. return c;
  84. }
  85.  
  86. var observer = new MutationObserver(function(mutations) {
  87. setTimeout(function(m) {
  88. var i = m.length, c = 0;
  89. while(i--) {
  90. if (m[i].target)
  91. c = checkTarget(m[i].target, c);
  92. if (m[i].addedNodes.length)
  93. c = checkNodes(m[i].addedNodes, c);
  94. }
  95. logger(c);
  96. },0,mutations);
  97. });
  98.  
  99. observer.observe(document, { childList : true, attributes : true, attributeFilter : ['style'], subtree : true });
  100.  
  101. window.addEventListener ("load", function(){
  102. var c = 0, imp = document.querySelectorAll('[style*="!"]'), i = imp.length;
  103. while(i--) {
  104. c+= checkTarget(imp[i], c);
  105. }
  106. logger(c);
  107. }, false);
  108. })();
  109. }
  110.  
  111. if (/^https?:\/\/(news\.yandex\.|(www\.)?yandex\.[^/]+\/(yand)?search[/?])/i.test(window.location.href))
  112. // https://greasyfork.org/en/scripts/809-no-yandex-ads
  113. document.addEventListener ("DOMContentLoaded", function() {
  114. // Generic ads removal and fixes
  115. (function(s){
  116. if (s) s.style.marginTop='0';
  117. })(document.querySelector('.serp-header'));
  118. (function(s, i){
  119. i = s.length;
  120. while(i--)
  121. s[i].parentNode.removeChild(s[i]);
  122. })(document.querySelectorAll('.serp-adv__head + .serp-item'), 0);
  123. (function(s){
  124. for (var l = 0; l < s.length; l++) s[l].parentNode.removeChild(s[l]);
  125. })(document.querySelectorAll(['#adbanner',
  126. '.serp-adv',
  127. '.b-spec-adv',
  128. 'div[class*="serp-adv__"]'].join(',')));
  129.  
  130. // Search ads
  131. var removeAds = function() {
  132. var s = document.querySelectorAll(['.serp-block',
  133. '.serp-item',
  134. '.search-item'].join(','));
  135. for (var l = 0; l < s.length; l++) {
  136. var i = s[l].querySelector(['.label',
  137. '.serp-item__label',
  138. '.document__provider-name'].join(','));
  139. if (!i) continue;
  140. if (i.textContent.indexOf('Реклама') > -1 || i.textContent.indexOf('Яндекс.Директ') > -1){
  141. s[l].parentNode.removeChild(s[l]);
  142. console.log('Ads removed.');
  143. }
  144. }
  145. };
  146.  
  147. // News ads
  148. var removeNewsAds = function() {
  149. var s = document.querySelectorAll(['.story[id]',
  150. '.document[id]',
  151. '.story__group[id]'].join(','));
  152. for (var l = 0; l < s.length; l++)
  153. if (window.getComputedStyle(s[l]).position === 'absolute') {
  154. s[l].parentNode.removeChild(s[l]);
  155. console.log('Ads removed.');
  156. }
  157. };
  158. // News fixes
  159. var removePageAdsClass = function() {
  160. if (document.body.classList.contains("b-page_ads_yes")){
  161. document.body.classList.remove("b-page_ads_yes");
  162. console.log('Page ads class removed.');
  163. }
  164. };
  165.  
  166. // Attaches observer to the page elements which Yandex updates via AJAX to display new search or news results
  167. var pageUpdateObserver = function(func, obj, params) {
  168. if (obj)
  169. new MutationObserver(func).observe(obj,(params?params:{childList:true}));
  170. };
  171.  
  172. if (window.location.hostname.search(/^news\./i) === 0) {
  173. pageUpdateObserver(removeNewsAds, document.querySelector('BODY'));
  174. pageUpdateObserver(removePageAdsClass, document.body, {attributes:true, attributesFilter:['class']});
  175. removeNewsAds();
  176. removePageAdsClass();
  177. } else {
  178. pageUpdateObserver(removeAds, document.querySelector('.main__content'));
  179. removeAds();
  180. }
  181. });
  182. else
  183. // all the other cases
  184. document.addEventListener ("DOMContentLoaded", function() {
  185. // function to search and remove nodes by conten
  186. // selector - standard CSS selector to define set of nodes to check
  187. // words - regular expression to check content of the suspicious nodes
  188. // params - object with multiple extra parameters:
  189. // .parent - parent node to remove if content is found in the child node
  190. // .siblings - number of simling nodes to remove (excluding text nodes)
  191. function scissors (selector, words, scope, params) {
  192. var nodes = scope.querySelectorAll(selector),
  193. i = nodes.length,
  194. toRemove = [];
  195.  
  196. while (i--)
  197. if (words.test(nodes[i].innerHTML) || !nodes[i].childNodes.length) {
  198. var node = nodes[i],
  199. siblings = Math.abs(params.siblings) || 0,
  200. iterFunc = params.siblings > 0 ? 'nextSibling' : 'previousSibling';
  201. // drill up to the specified parent node if required
  202. if (params.parent)
  203. while(node !== scope &&
  204. node.tagName.toLowerCase() !== params.parent)
  205. node = node.parentNode;
  206. if (node === scope)
  207. break;
  208. toRemove.push(node);
  209. // add multiple nodes if defined more than one sibling
  210. while (siblings) {
  211. node = node[iterFunc];
  212. toRemove.push(node);
  213. if (node.tagName) siblings--; //don't count text nodes
  214. }
  215. }
  216. i = toRemove.length;
  217. while(i--)
  218. toRemove[i].parentNode.removeChild(toRemove[i]);
  219.  
  220. return toRemove.length;
  221. }
  222.  
  223. // function to perform multiple checks if ads inserted with a delay
  224. // by default does 30 checks withing a 3 seconds unless nonstop mode specified
  225. // also does 1 extra check when a page completely loads
  226. // selector and words - passed dow to scissors
  227. // params - object with multiple extra parameters:
  228. // .root - selector to narrow down scope to scan
  229. // .watch - if true then check will be performed continuously
  230. // .parent - passed down to scissors
  231. // .siblings - passed down to scissors
  232. function gardener(selector, words, params) {
  233. params = params || {};
  234. var scope = document.body,
  235. nonstop = false;
  236. // narrow dowsn scope to a specific element
  237. if (params.root) {
  238. scope = scope.querySelector(params.root);
  239. if (!scope) // exit if the root element is not present on the page
  240. return 0;
  241. }
  242. // add watch mode if required
  243. if (params.watch) {
  244. if (window.MutationObserver) {
  245. var o = new MutationObserver(function(ms){
  246. ms.forEach(function(m){
  247. if (m.addedNodes.length)
  248. scissors(selector, words, scope, params);
  249. });
  250. });
  251. o.observe(document.querySelector(params.root),
  252. {childList:true, subtree: true});
  253. } else nonstop = true;
  254. }
  255. // watch for a full page load to do one extra cut
  256. window.addEventListener('load',function(){
  257. scissors(selector, words, scope, params);
  258. });
  259. // do multiple cuts until ads removed
  260. function cut(sci, s, w, sc, p, i) {
  261. if (i > 0) i--;
  262. if (i && !sci(s, w, sc, p))
  263. setTimeout(cut, 100, sci, s, w, sc, p, i);
  264. }
  265. cut(scissors, selector, words, scope, params, (nonstop ? -1 : 30));
  266. }
  267.  
  268. var scripts = {};
  269. scripts['fs.to'] = function() {
  270. function skipClicker(i) {
  271. if (!i) return;
  272. var skip = document.querySelector('.b-aplayer-banners__close');
  273. if (skip)
  274. skip.click();
  275. else
  276. setTimeout(skipClicker, 100, i-1);
  277. }
  278. setTimeout(skipClicker, 100, 30);
  279.  
  280. var divs = document.getElementsByTagName('div');
  281. var re = /\w{1,5}\d{1,5}\w{1,5}\d{1,5}/;
  282. for(var i = 0; i < divs.length; i++)
  283. if(re.test(divs[i].className))
  284. divs[i].style.display = 'none';
  285.  
  286. var style = document.head.appendChild( document.createElement('style') );
  287. style.type = 'text/css';
  288.  
  289. style.sheet.insertRule([
  290. '.b-aplayer-teasers > a',
  291. '.b-player-popup__content > div[class][style="position: relative;"]',
  292. 'div[class^="b-adproxy"]',
  293. 'div[id^="admixer_async_"]'
  294. ].join(',')+'{display:none!important}', 0);
  295.  
  296. if (/\/view_iframe\//i.test(document.location.pathname)) {
  297. var p = document.querySelector('#player:not([preload="auto"])'),
  298. m = document.querySelector('.main'),
  299. adStepper = function(p) {
  300. if (p.currentTime < p.duration)
  301. p.currentTime++;
  302. },
  303. cl = function(p) {
  304. function skipListener() {
  305. p.pause();
  306. if (!p.classList.contains('m-hidden'))
  307. p.classList.add('m-hidden');
  308. setTimeout(adStepper, 1000, p);
  309. }
  310. p.addEventListener('timeupdate', skipListener, false);
  311. },
  312. o = new MutationObserver(function (mut) {
  313. mut.forEach(function (e) {
  314. for (var i = 0; i < e.addedNodes.length; i++) {
  315. if (e.addedNodes[i].id === 'player' &&
  316. e.addedNodes[i].nodeName === 'VIDEO' &&
  317. e.addedNodes[i].getAttribute('preload') != 'auto') {
  318. cl(e.addedNodes[i]);
  319. }
  320. }
  321. });
  322. });
  323. if (p.nodeName === 'VIDEO')
  324. cl(p);
  325. else
  326. o.observe(m, {childList: true});
  327. }
  328. };
  329. scripts['brb.to'] = scripts['fs.to'];
  330. scripts['cxz.to'] = scripts['fs.to'];
  331.  
  332. scripts['fishki.net'] = function() {
  333. gardener('.main-post', /543769|Реклама/);
  334. };
  335.  
  336. scripts['hdrezka.me'] = function() {
  337. gardener('div[id][onclick][onmouseup][onmousedown]', /onmouseout/i);
  338.  
  339. };
  340.  
  341. scripts['yap.ru'] = function() {
  342. var words = /member1438|Administration/;
  343. gardener('form > table[id^="p_row_"]', words);
  344. gardener('tr > .holder.newsbottom', words, {parent:'tr', siblings:-2});
  345. };
  346. scripts['yaplakal.com'] = scripts['yap.ru'];
  347.  
  348. scripts['auto.ru'] = function() {
  349. var words = /Реклама|Яндекс.Директ|yandex_ad_/;
  350. var userAdsListAds = [
  351. '.listing-list > .listing-item',
  352. '.listing-item_type_fixed.listing-item'
  353. ];
  354. var catalogAds = [
  355. 'div[class*="layout_catalog-inline"]',
  356. 'div[class$="layout_horizontal"]'
  357. ];
  358. var otherAds = [
  359. '.advt_auto',
  360. '.sidebar-block',
  361. '.pager-listing + div[class]',
  362. '.card > div[class][style]',
  363. '.sidebar > div[class]',
  364. '.main-page__section + div[class]',
  365. '.listing > tbody'];
  366. gardener(userAdsListAds.join(','), words, {root:'.listing-wrap', watch:true});
  367. gardener(catalogAds.join(','), words, {root:'.catalog__page,.content__wrapper', watch:true});
  368. gardener(otherAds.join(','), words);
  369. };
  370.  
  371. scripts['online.anidub.com'] = function() {
  372. var script = document.createElement('script');
  373. script.type = "text/javascript";
  374. script.innerHTML = "function ogonekstart1() {}";
  375. document.getElementsByTagName('head')[0].appendChild(script);
  376.  
  377. var style = document.createElement('style');
  378. style.type = 'text/css';
  379. style.appendChild(document.createTextNode('.background {background: none!important;}'));
  380. style.appendChild(document.createTextNode('.background > script + div, .background > script ~ div:not([id]):not([class]) + div[id][class] {display:none!important}'));
  381. document.head.appendChild(style);
  382. };
  383.  
  384. var domain = document.domain;
  385. while (domain.indexOf('.') + 1) {
  386. if (domain in scripts) {
  387. scripts[domain]();
  388. break;
  389. }
  390. domain = domain.slice(domain.indexOf('.') + 1);
  391. }
  392. });
  393. })();