RU AdList JS Fixes

try to take over the world!

目前为 2016-06-23 提交的版本。查看 最新版本

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