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