Greasy Fork is available in English.

No Yandex Ads

Removes ads in Yandex search results.

La data de 16-02-2016. Vezi ultima versiune.

// ==UserScript==
// @name        No Yandex Ads
// @namespace   lainverse_no_yandex_ads
// @description Removes ads in Yandex search results.
// @author      lainverse
// @license     CC BY-SA
// @version     5.8
// @include     /^https?://(news\.yandex\.|(www\.)?yandex\.[^/]+/(yand)?search[/?])/
// @grant       none
// ==/UserScript==
/* jshint -W084, esnext: true*/
(function(){
    'use strict';

    // Generic ads removal and fixes
    (function(s){
        if (s) s.style.marginTop='0';
    })(document.querySelector('.serp-header'));
    (function(s){
        while(s = document.querySelector('.serp-adv__head+.serp-item'))
            s.parentNode.removeChild(s);
    })();
    (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
    function removeAds() {
        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
    function removeNewsAds() {
        var s = document.querySelectorAll(['.story[id]',
                                           '.document[id]',
                                           '.story__group[id]'].join(','));
        for (var l = 0; l < s.length; l++)
            if (window.getComputedStyle(s[l]).position === 'absolute') {
                s[l].parentNode.removeChild(s[l]);
                console.log('Ads removed.');                
            }
    }
    // News fixes
    function removePageAdsClass() {
        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
    function pageUpdateObserver(func, obj, params) {
        if (obj)
            new MutationObserver(func).observe(obj,(params?params:{childList:true}));
    }

    if (window.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();
    }
})();