chrono / BlockYaAds

// ==UserScript==
// @name BlockYaAds
// @description Block all ads on yandex.ru
// @version 0.4.6.1
// @author Chrono
// @license MIT
// @match           http*://*.yandex.ru/*
// @run-at document-start
// @grant none
// @require http://code.jquery.com/jquery-3.5.1.min.js
// ==/UserScript==

(function () {
  var numElements = 0;

  function removeElements(selector) {
    //console.log("removeEl");
    var number = 0;
    //console.log(selector);
    document.querySelectorAll(selector).forEach(function (el) {
      //el.remove();
      //el.style.display = "none";
      el.querySelectorAll("*").forEach(function (el_inner) {
        el_inner.style.display = "none";
      });
      number++;
    });

    return number;
  }

  function removeElements1(el) {
    //console.log("removeEl");
    var number = 0;
    el.querySelectorAll("*").forEach(function (el_inner) {
      el_inner.style.display = "none";
    });
  }

  function start() {
    var flag = 0;
    var divs = document.getElementsByTagName("div");
    var getUrl = window.location;
    var baseUrl = getUrl.host;
    //console.log("start")

    if (baseUrl == "docviewer.yandex.ru") {
      document.querySelectorAll("*").forEach(function (el) {

        el.classList.forEach(function (clazz) {
          if (clazz == "js-doc-html") {
            var inner_elements = el.querySelectorAll("*");
            inner_elements.forEach(function loop(el1) {
              if (loop.stop) {
                return;
              }
              if ((el1.tagName == "DIV") && el1.classList.length == 3) {
                removeElements("." + el1.classList[0]);
                loop.stop = true;
              }
            });
          }
        });

        if (el.tagName.length > 10) {
          //console.log(el.tagName);
          el.style.display = "none";
        }
      });
    }
    //console.log(baseUrl);
    for (var i = 0; i < divs.length; i++) {
      var div = divs[i];

      if (window.location.pathname == "/images/search") {
        var style = div.getAttribute("style");
        if (style == "margin-bottom: 4px;" || div.offsetHeight == 72) {
          div.classList.forEach(function (el) {
            removeElements("." + el);
          });
        }
      }

      div.classList.forEach(function (el) {
        if (el == "ComposeDoneDirect")
          removeElements("." + el);

        if (!el.startsWith("ns-view-"))
          return;

        // try to match something like "ns-view-iQFFMqorofX1_aGNQGLsWXQ"

        // match all classes but the list below
        flag = 1;
        var els = [
          "ns-view-abook",
          "ns-view-advanced",
          "ns-view-app",
          "ns-view-attach",
          "ns-view-browser",
          "ns-view-collect",
          "ns-view-compose",
          "ns-view-contact-actions",
          "ns-view-container",
          "ns-view-copy",
          "ns-view-fake",
          "ns-view-fill",
          "ns-view-folders",
          "ns-view-footer",
          "ns-view-header",
          "ns-view-id",
          "ns-view-infoline",
          "ns-view-labels",
          "ns-view-layout",
          "ns-view-left",
          "ns-view-loading",
          "ns-view-mail",
          "ns-view-main",
          "ns-view-message",
          "ns-view-message-react",
          "ns-view-messages",
          "ns-view-mops",
          "ns-view-notifi",
          "ns-view-opt-in-subs-box",
          "ns-view-opt-in-subs-view",
          "ns-view-page",
          "ns-view-ps-header",
          "ns-view-quick",
          "ns-view-react-left-column",
          "ns-view-react-main-buttons",
          "ns-view-react-quick-reply",
          "ns-view-right",
          "ns-view-service",
          "ns-view-setup-abook",
          "ns-view-setup-left",
          "ns-view-sidebar-with-widgets",
          "ns-view-sidebar-with-widgets-box",
          "ns-view-system-alert-container",
          "ns-view-skin",
          "ns-view-tech",
          "ns-view-themes",
          "ns-view-themes",
          "ns-view-thread",
          "ns-view-timeline",
          "ns-view-tip",
          "ns-view-toolbar",
          "ns-view-view"
        ];

        for (var j = 0; j < els.length; j++) {
          if (el.startsWith(els[j])) {
            flag = 0;
            return;
          }
        }

        if (!flag)
          return;

        if (flag) {
          console.log("removed " + el);
          numElements = removeElements("." + el);

        }
      })
    }
  }

  $(document).ready(function () {
    var target = document.body;
    var config = {
      attributes: true,
      attributeOldValue: true,
      characterData: true,
      characterDataOldValue: true,
      childList: true,
      subtree: true
    };

    observer.observe(target, config);
  });

  var observer = new MutationObserver(start);

})();