No Yandex Ads

Удаляет рекламу из результатов поиска Яндекс. Removes ads in Yandex search results.

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

// ==UserScript==
// @name        No Yandex Ads
// @namespace   lainverse_no_yandex_ads
// @description Удаляет рекламу из результатов поиска Яндекс. Removes ads in Yandex search results.
// @author      lainverse
// @license     CC BY-SA
// @version     6.0
// @include     /^https?://(news\.yandex\.|(www\.)?yandex\.[^/]+/(yand)?search[/?])/
// @grant       none
// ==/UserScript==

(function(){
    'use strict';

    // Generic ads removal and fixes
    function removeGenericAds() {
        var s, i;
        s = document.querySelector('.serp-header');
        if (s) {
            s.style.marginTop='0';
        }
        s = document.querySelectorAll('.serp-adv__head + .serp-item');
        i = s.length;
        while(i--) {
            s[i].parentNode.removeChild(s[i]);
        }
        s = document.querySelectorAll('#adbanner, .serp-adv, .b-spec-adv, div[class*="serp-adv__"]');
        i = s.length;
        while(i--) {
            s[i].parentNode.removeChild(s[i]);
        }
    }
    removeGenericAds();

    // Search ads
    function removeAds() {
        var s = document.querySelectorAll('.serp-block, .serp-item, .search-item');
        var i = s.length, item;
        while (i--) {
            item = s[i].querySelector('.label, .serp-item__label, .document__provider-name');
            if (item && (
                item.textContent.indexOf('Реклама') > -1 ||
                item.textContent.indexOf('Яндекс.Директ') > -1)) {
                s[i].parentNode.removeChild(s[i]);
                console.log('Ads removed.');
            }
        }
    }
    // News ads
    function removeNewsAds() {
        var s = document.querySelectorAll('.story[id], .document[id], .story__group[id]');
        var l = s.length;
        while (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) {
            var o = new MutationObserver(func);
            o.observe(obj,(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();
    }
})();