RU AdList JS Fixes

try to take over the world!

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

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