// ==UserScript==
// @name         Ads DOM Remover
// @namespace    sagiegurari
// @version      0.43
// @author       Sagie Gur-Ari
// @description  Removes Ad Containers from DOM (doesn't replace adblocker extension, but blocks dynamic content which the adblocker fails to block by removing whole sections from the HTML DOM.)
// @homepage     https://github.com/sagiegurari/userscripts-ads-dom-remover
// @supportURL   https://github.com/sagiegurari/userscripts-ads-dom-remover/issues
// @match        http://www.ynet.co.il/home/*
// @match        http://www.ynet.co.il/articles/*
// @match        http://www.calcalist.co.il/*
// @require      https://code.jquery.com/jquery-2.2.2.min.js
// @grant        none
// @license      MIT License
// ==/UserScript==
/* jshint -W097 */
'use strict';

(function run($) {
    var service = {
        state: {
            intervalID: null,
            counter: 0,
            secondLoop: false
        },
        getSelectors: function (hostName) {
            return [
                '#colorbox',
                '#cboxOverlay',
                '#ads.premium',
                'img[src*="dynamicyield"]',
                'div.MSCmainContent',
                '[id*="arketingCarouse"]',
                '[id*="arketingRecommended"]',
                '.mainVerticalArticleSharingLinks',
                '.OUTBRAIN',
                '.topBannerWrap',
                {
                    selector: 'iframe',
                    filter: function ($element) {
                        return !$element.parent().hasClass('news_ticker_iframe');
                    }
                },
                {
                    selector: 'div.B2b.block div',
                    pre: function ($element) {
                        $element.parent().css({
                            height: '1px'
                        });
                    }
                }
            ];
        },
        hideElements: function () {
            var found = false;

            var selectors = this.getSelectors(document.location.hostname);

            selectors.forEach(function (selector) {
                var selectorString = selector.selector || selector;

                var $element = $(selectorString);

                if ($element && $element.length) {
                    found = true;

                    if (selector.pre) {
                        selector.pre($element);
                    }

                    var remove = true;
                    if (selector.filter) {
                        remove = selector.filter($element);
                    }

                    if (remove) {
                        $element.remove();
                    }

                    console.debug('[user script][Ads DOM Remover][hideElements] Found:', selector, 'count:', $element.length, 'in website and removed it.');
                }
            });

            return found;
        },
        actionLoop: function () {
            this.state.counter++;
            var stopInterval = this.state.intervalID && (this.state.counter > 10);
            console.debug('[user script][Ads DOM Remover][actionLoop] Running loop:', this.state.counter, 'state:', this.state, 'stop interval:', stopInterval);

            this.hideElements();

            if (stopInterval) {
                console.debug('[user script][Ads DOM Remover][actionLoop] Clearing interval ID:', this.state.intervalID);
                clearInterval(this.state.intervalID);
                this.state.intervalID = null;

                if (!this.state.secondLoop) {
                    this.startActionLoop(false, 2500);
                }
            }
        },
        startActionLoop: function (firstTime, delay) {
            var self = this;

            self.state.secondLoop = !firstTime;
            self.state.counter = 0;

            setTimeout(function() {
                self.state.intervalID = setInterval(function () {
                    self.actionLoop();
                }, 150);
                setTimeout(function () {
                    self.hideElements();
                }, 15000);
            }, delay);
        },
        run: function () {
            this.startActionLoop(true, 0);
        }
    };

    service.run();
}(window.$));

@@ -97,9 +97,9 @@
  0.4
-3
+4
 %0A// 
@@ -622,16 +622,59 @@
 co.il/*%0A
+// @match        http://www.globes.co.il/*%0A
 // @requ
@@ -989,20 +989,17 @@
         
-getS
+s
 electors
@@ -1003,28 +1003,8 @@
 ors:
- function (hostName)
  %7B%0A 
@@ -1014,22 +1014,21 @@
         
-return
+ynet:
  %5B%0A     
@@ -1899,32 +1899,32 @@
               %7D%0A
-
             %5D;%0A 
@@ -1920,16 +1920,769 @@
        %5D
+,%0A            globes: %5B%0A                '#chromeWindow',%0A                %7B%0A                    selector: 'iframe',%0A                    filter: function ($element) %7B%0A                        var id = $element.attr('id');%0A                        var src = $element.attr('src') %7C%7C '';%0A                        return (id !== 'GlobalFinanceData_home') && (src.indexOf('/news/') !== -1);%0A                    %7D%0A                %7D%0A            %5D%0A        %7D,%0A        getSelectors: function (hostName) %7B%0A            var selectors;%0A            if (hostName.indexOf('globes') !== -1) %7B%0A                selectors =  this.selectors.globes;%0A            %7D else %7B //ynet/calcalist%0A                selectors =  this.selectors.ynet;%0A            %7D%0A%0A            return selectors
 ;%0A      

add globes support