RU AdList JS Fixes

try to take over the world!

As of 2016-08-07. See the latest version.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         RU AdList JS Fixes
// @namespace    ruadlist_js_fixes
// @version      1.0.26
// @description  try to take over the world!
// @author       lainverse & dimisa
// @match        *://*/*
// @grant        unsafeWindow
// @grant        window.close
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';
    var win = unsafeWindow || window,
        inIFrame = function() {
            try {
                return win.self !== win.top;
            } catch (e) {
                return true;
            }
        };

    if (!/firefox/i.test(navigator.userAgent)) { // scripts for non-Firefox browsers

        // https://greatest.deepsurf.us/scripts/19144-websuckit/
        (function() {
            // check if the browser supports Proxy and WebSocket
            if (typeof Proxy != 'function' || typeof WebSocket != 'function') return;
            var to_block = [
                '||bgrndi.com^',
                '||brokeloy.com^',
                '||dreadfula.ru^',
                '||et-code.ru^',
                '||gocdn.ru^',
                '||hghit.com^',
                '||kuveres.com^',
                '||lepubs.com^',
                '||mail.ru^',
                '||marketgid.com^',
                '||mxtads.com^',
                '||psma01.com^',
                '||psma02.com^',
                '||psma03.com^',
                '||recreativ.ru^',
                '||regpole.com^',
                '||torvind.com^',
                '||trafmag.com^',
                '||xxuhter.ru^'
            ], masks = [];
            to_block.forEach(function(m){
                masks.push(new RegExp(
                    m.replace(/([\./*+?()[]{}$])/g, '\\$1')
                    .replace(/\^(?!$)/g,'\\.?[^\\w%._-]')
                    .replace(/\^$/,'(\\.?[^\\w%._-]|$)')
                    .replace(/^\|\|/,'^wss?:\\/+([^/.]+\\.)*'),
                    'i'));
            });
            var ws = win.WebSocket;
            win.WebSocket = new Proxy(ws, {
                construct: function(e,i) {
                    var url = i[0];
                    console.log('[WSI] Opening socket on', url, '…');
                    var j = masks.length;
                    while(j--)
                        if (masks[j].test(url)) {
                            console.log("[WSI] Blocked.");
                            return new Proxy({url: url, readyState: ws.OPEN}, {
                                get: function(tgt, nm) {
                                    console.log('[WSI] Registered call to property "', nm, '"');
                                    try {
                                        if (typeof ws.prototype[nm] === 'function') {
                                            if (['close', 'send'].indexOf(nm) > -1)
                                                tgt.readyState = ws.CLOSED;
                                            return function(){};
                                        }
                                        if (typeof ws.prototype[nm] === 'number') {
                                            return ws[nm];
                                        }
                                    } catch(e) {}
                                    return tgt[nm];
                                }
                            });
                        }
                    return new e(i[0],i[1]);
                }
            });
        })();

        // https://greatest.deepsurf.us/scripts/14720-it-s-not-important
        (function(){
            var imptt = /((display|(margin|padding)(-top|-bottom)?)\s*:[^;!]*)!\s*important/ig,
                rplsf = function(str,grp){return grp;};

            function unimportanter(el, si) {
                if (!imptt.test(si) || el.style.display == 'none')
                    return 0; // get out if we have nothing to do here
                if (el.nodeName === 'IFRAME' && el.src &&
                    el.src.slice(0,17) === 'chrome-extension:')
                    return 0; // Web of Trust uses this method to add their frame
                var so = si.replace(imptt, rplsf), ret = 0;
                if (si != so) {
                    ret = 1;
                    el.setAttribute('style', so);
                }
                return ret;
            }

            function logger(c) {
                if (c) console.log('Some page elements became a bit less important.');
            }

            function checkTarget(m, c) {
                var si = m.getAttribute ? m.getAttribute('style') : null;
                if (si && si.indexOf('!') > -1)
                    c+=unimportanter(m, si);
                return c;
            }

            function checkNodes(m, c) {
                var i = m.length;
                while(i--)
                    c = checkTarget(m[i], c);
                return c;
            }

            var observer = new MutationObserver(function(mutations) {
                setTimeout(function(m) {
                    var i = m.length, c = 0;
                    while(i--) {
                        if (m[i].target)
                            c = checkTarget(m[i].target, c);
                        if (m[i].addedNodes.length)
                            c = checkNodes(m[i].addedNodes, c);
                    }
                    logger(c);
                },0,mutations);
            });

            observer.observe(document, { childList : true, attributes : true, attributeFilter : ['style'], subtree : true });

            win.addEventListener ("load", function(){
                var c = 0, imp = document.querySelectorAll('[style*="!"]'), i = imp.length;
                while(i--) {
                    c+= checkTarget(imp[i], c);
                }
                logger(c);
            }, false);
        })();

    }

    // https://greatest.deepsurf.us/en/scripts/21937-moonwalk-hdgo-kodik-fix
    (function () {
        if (!inIFrame)
            return;

        document.addEventListener ("DOMContentLoaded", function() {
            var player;
            if (win.iframe_adv_enabled !== undefined) {
                win.iframe_adv_enabled = false;
            } else if (win.banner_second !== undefined) {
                win.banner_second = 0;
                player = document.getElementById('play-player-hd');
                if (player) player.onclick = function() {
                    var adsdiv = document.getElementById("ads-player-hd");
                    adsdiv.parentNode.removeChild(adsdiv);
                    win.setPlayer();
                };
            } else if (win.MXoverrollCallback !== undefined) {
                player = document.getElementsByClassName('play_button')[0];
                if (player) player.onclick = function() {
                    win.MXoverrollCallback();
                };
            }
        },false);
    })();

    if (/^https?:\/\/(news\.yandex\.|(www\.)?yandex\.[^/]+\/(yand)?search[/?])/i.test(win.location.href))
        // https://greatest.deepsurf.us/en/scripts/809-no-yandex-ads
        document.addEventListener ("DOMContentLoaded", function() {
            // Generic ads removal and fixes
            (function(s){
                if (s) s.style.marginTop='0';
            })(document.querySelector('.serp-header'));
            (function(s, i){
                i = s.length;
                while(i--)
                    s[i].parentNode.removeChild(s[i]);
            })(document.querySelectorAll('.serp-adv__head + .serp-item'), 0);
            (function(s){
                for (var l = 0; l < s.length; l++) s[l].parentNode.removeChild(s[l]);
            })(document.querySelectorAll(['#adbanner',
                                          '.serp-adv',
                                          '.b-spec-adv',
                                          'div[class*="serp-adv__"]'].join(',')));

            // Search ads
            var removeAds = function() {
                var s = document.querySelectorAll(['.serp-block',
                                                   '.serp-item',
                                                   '.search-item'].join(','));
                for (var l = 0; l < s.length; l++) {
                    var i = s[l].querySelector(['.label',
                                                '.serp-item__label',
                                                '.document__provider-name'].join(','));
                    if (!i) continue;
                    if (i.textContent.indexOf('Реклама') > -1 || i.textContent.indexOf('Яндекс.Директ') > -1){
                        s[l].parentNode.removeChild(s[l]);
                        console.log('Ads removed.');
                    }
                }
            };

            // News ads
            var removeNewsAds = function() {
                var s = document.querySelectorAll(['.story[id]',
                                                   '.document[id]',
                                                   '.story__group[id]'].join(','));
                for (var l = 0; l < s.length; l++)
                    if (win.getComputedStyle(s[l]).position === 'absolute') {
                        s[l].parentNode.removeChild(s[l]);
                        console.log('Ads removed.');
                    }
            };
            // News fixes
            var removePageAdsClass = function() {
                if (document.body.classList.contains("b-page_ads_yes")){
                    document.body.classList.remove("b-page_ads_yes");
                    console.log('Page ads class removed.');
                }
            };

            // Attaches observer to the page elements which Yandex updates via AJAX to display new search or news results
            var pageUpdateObserver = function(func, obj, params) {
                if (obj)
                    new MutationObserver(func).observe(obj,(params?params:{childList:true}));
            };

            if (win.location.hostname.search(/^news\./i) === 0) {
                pageUpdateObserver(removeNewsAds, document.querySelector('BODY'));
                pageUpdateObserver(removePageAdsClass, document.body, {attributes:true, attributesFilter:['class']});
                removeNewsAds();
                removePageAdsClass();
            } else {
                pageUpdateObserver(removeAds, document.querySelector('.main__content'));
                removeAds();
            }
        });
    else
        // all the other cases
        document.addEventListener ("DOMContentLoaded", function() {
            // function to search and remove nodes by conten
            // selector - standard CSS selector to define set of nodes to check
            // words - regular expression to check content of the suspicious nodes
            // params - object with multiple extra parameters:
            //   .parent - parent node to remove if content is found in the child node
            //   .siblings - number of simling nodes to remove (excluding text nodes)
            function scissors (selector, words, scope, params) {
                var nodes = scope.querySelectorAll(selector),
                    i = nodes.length,
                    toRemove = [];

                while (i--)
                    if (words.test(nodes[i].innerHTML) || !nodes[i].childNodes.length) {
                        var node = nodes[i],
                            siblings = Math.abs(params.siblings) || 0,
                            iterFunc = params.siblings > 0 ? 'nextSibling' : 'previousSibling';
                        // drill up to the specified parent node if required
                        if (params.parent)
                            while(node !== scope &&
                                  node.tagName.toLowerCase() !== params.parent)
                                node = node.parentNode;
                        if (node === scope)
                            break;
                        toRemove.push(node);
                        // add multiple nodes if defined more than one sibling
                        while (siblings) {
                            node = node[iterFunc];
                            toRemove.push(node);
                            if (node.tagName) siblings--; //don't count text nodes
                        }
                    }
                i = toRemove.length;
                while(i--)
                    toRemove[i].parentNode.removeChild(toRemove[i]);

                return toRemove.length;
            }

            // function to perform multiple checks if ads inserted with a delay
            // by default does 30 checks withing a 3 seconds unless nonstop mode specified
            // also does 1 extra check when a page completely loads
            // selector and words - passed dow to scissors
            // params - object with multiple extra parameters:
            //   .root - selector to narrow down scope to scan;
            //   .observe - if true then check will be performed continuously;
            //   .parent - passed down to scissors;
            //   .siblings - passed down to scissors;
            function gardener(selector, words, params) {
                params = params || {};
                var scope = document.body,
                    nonstop = false;
                // narrow dowsn scope to a specific element
                if (params.root) {
                    scope = scope.querySelector(params.root);
                    if (!scope) // exit if the root element is not present on the page
                        return 0;
                }
                // add observe mode if required
                if (params.observe) {
                    if (typeof MutationObserver == 'function') {
                        var o = new MutationObserver(function(ms){
                            ms.forEach(function(m){
                                if (m.addedNodes.length)
                                    scissors(selector, words, scope, params);
                            });
                        });
                        o.observe(document.querySelector(params.root),
                                  {childList:true, subtree: true});
                    } else nonstop = true;
                }
                // wait for a full page load to do one extra cut
                win.addEventListener('load',function(){
                    scissors(selector, words, scope, params);
                });
                // do multiple cuts until ads removed
                function cut(sci, s, w, sc, p, i) {
                    if (i > 0) i--;
                    if (i && !sci(s, w, sc, p))
                        setTimeout(cut, 100, sci, s, w, sc, p, i);
                }
                cut(scissors, selector, words, scope, params, (nonstop ? -1 : 30));
            }

            function preventBackgroundRedirect() {
                console.log("Background redirect prevention enabled.");
                var orgOpen = window.open;
                win.open = function(){
                    var loc = arguments[0];
                    if (loc.indexOf(window.location.host) > -1 ||
                        loc.indexOf('://') == -1) {
                        window.addEventListener('unload',function(e){
                            window.close();
                        },true);
                    }
                    orgOpen.apply(window, arguments);
                };
            }

            var scripts = {};
            scripts['fs.to'] = function() {
                function skipClicker(i) {
                    if (!i) return;
                    var skip = document.querySelector('.b-aplayer-banners__close');
                    if (skip)
                        skip.click();
                    else
                        setTimeout(skipClicker, 100, i-1);
                }
                setTimeout(skipClicker, 100, 30);

                var divs = document.getElementsByTagName('div');
                var re = /\w{1,5}\d{1,5}\w{1,5}\d{1,5}/;
                for(var i = 0; i < divs.length; i++)
                    if(re.test(divs[i].className))
                        divs[i].style.display = 'none';

                var style = document.head.appendChild( document.createElement('style') );
                style.type = 'text/css';

                style.sheet.insertRule([
                    '.b-aplayer-teasers > a',
                    '.b-player-popup__content > div[class][style="position: relative;"]',
                    'div[class^="b-adproxy"]',
                    'div[id^="admixer_async_"]'
                ].join(',')+'{display:none!important}', 0);

                if (/\/view_iframe\//i.test(document.location.pathname)) {
                    var p = document.querySelector('#player:not([preload="auto"])'),
                        m = document.querySelector('.main'),
                        adStepper = function(p) {
                            if (p.currentTime < p.duration)
                                p.currentTime++;
                        },
                        adSkipper = function(f, p) {
                            f.click();
                            p.waitAfterSkip = false;
                            console.log('Пропущена.');
                        },
                        cl = function(p) {
                            var faster = document.querySelector('.b-aplayer__html5-desktop-skip');

                            function skipListener() {
                                if (p.waitAfterSkip) {
                                    console.log('Доступен быстрый пропуск рекламы…');
                                    return;
                                }
                                p.pause();
                                if (!p.classList.contains('m-hidden'))
                                    p.classList.add('m-hidden');
                                if (faster &&
                                    window.getComputedStyle(faster).display == 'block' &&
                                    !faster.querySelector('.b-aplayer__html5-desktop-skip-timer')) {
                                    p.waitAfterSkip = true;
                                    setTimeout(adSkipper, 1000, faster, p);
                                } else
                                    setTimeout(adStepper, 1000, p);
                            }

                            p.addEventListener('timeupdate', skipListener, false);
                        },
                        o = new MutationObserver(function (mut) {
                            mut.forEach(function (e) {
                                for (var i = 0; i < e.addedNodes.length; i++) {
                                    if (e.addedNodes[i].id === 'player' &&
                                        e.addedNodes[i].nodeName === 'VIDEO' &&
                                        e.addedNodes[i].getAttribute('preload') != 'auto') {
                                        cl(e.addedNodes[i]);
                                    }
                                }
                            });
                        });
                    if (p.nodeName === 'VIDEO')
                        cl(p);
                    else
                        o.observe(m, {childList: true});
                }
            };
            scripts['brb.to'] = scripts['fs.to'];
            scripts['cxz.to'] = scripts['fs.to'];

            scripts['drive2.ru'] = function() {
                gardener('.g-column-wide > .c-block', /Реклама|\.relap\./i);
            };

            scripts['pikabu.ru'] = function() {
                gardener('.story', /story__sponsor|profile\/ads"/i, {root:'.stories,.page-story', observe: true});
            };

            scripts['fishki.net'] = function() {
                gardener('.main-post', /543769|Реклама/);
            };

            scripts['hdrezka.me'] = function() {
                gardener('div[id][onclick][onmouseup][onmousedown]', /onmouseout/i);
            };

            scripts['naruto-base.tv'] = function() {
                gardener('div[id^="entryID"]', /\/espritgames.ru\//i);
            };

            scripts['rustorka.com'] = function() {
                var s = document.head.childNodes, i = s.length;
                if (i < 5)
                    while(i--)
                        if (s[i].httpEquiv && s[i].httpEquiv=='refresh')
                            window.close();
                gardener('span[class],ul[class],span[id],ul[id]', /\/\d{12,}\.php/i, {root:'#sidebar1', observe:true});
                gardener('div[id][style*="!important"]', /!important/i);
            };

            scripts['yap.ru'] = function() {
                var words = /member1438|Administration/;
                gardener('form > table[id^="p_row_"]', words);
                gardener('tr > .holder.newsbottom', words, {parent:'tr', siblings:-2});
            };
            scripts['yaplakal.com'] = scripts['yap.ru'];

            scripts['reactor.cc'] = preventBackgroundRedirect;
            scripts['joyreactor.cc'] = preventBackgroundRedirect;
            scripts['pornreactor.cc'] = preventBackgroundRedirect;

            scripts['auto.ru'] = function() {
                var words = /Реклама|Яндекс.Директ|yandex_ad_/;
                var userAdsListAds = [
                    '.listing-list > .listing-item',
                    '.listing-item_type_fixed.listing-item'
                ];
                var catalogAds = [
                    'div[class*="layout_catalog-inline"]',
                    'div[class$="layout_horizontal"]'
                ];
                var otherAds = [
                    '.advt_auto',
                    '.sidebar-block',
                    '.pager-listing + div[class]',
                    '.card > div[class][style]',
                    '.sidebar > div[class]',
                    '.main-page__section + div[class]',
                    '.listing > tbody'];
                gardener(userAdsListAds.join(','), words, {root:'.listing-wrap', observe:true});
                gardener(catalogAds.join(','), words, {root:'.catalog__page,.content__wrapper', observe:true});
                gardener(otherAds.join(','), words);
            };

            scripts['online.anidub.com'] = function() {
                var script = document.createElement('script');
                script.type = "text/javascript";
                script.innerHTML = "function ogonekstart1() {}";
                document.getElementsByTagName('head')[0].appendChild(script);

                var style = document.createElement('style');
                style.type = 'text/css';
                style.appendChild(document.createTextNode('.background {background: none!important;}'));
                style.appendChild(document.createTextNode('.background > script + div, .background > script ~ div:not([id]):not([class]) + div[id][class] {display:none!important}'));
                document.head.appendChild(style);
            };

            scripts['rsload.net'] = function() {
                window.addEventListener('load', function() {
                    var dis = document.querySelector('.cb-disable');
                    if (dis) dis.click();
                });
                document.addEventListener('click',function(e){
                    var t = e.target;
                    if (t && t.href && /:\/\/\d+\.\d+\.\d+\.\d+\//.test(t.href)) {
                        t.href = t.href.replace('://','://rsload.net:rsload.net@');
                    }
                });
            };

            var domain = document.domain;
            while (domain.indexOf('.') + 1) {
                if (domain in scripts) {
                    scripts[domain]();
                    break;
                }
                domain = domain.slice(domain.indexOf('.') + 1);
            }
        });
})();