// ==UserScript==
// @name         Remove VK ads
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Remove VK advertisement
// @author       maxxx1313
// @match        https://vk.com/*
// @grant        none
// @runat        document-end
// ==/UserScript==

(function() {
    'use strict';

    // remove left
    var adsleft = document.getElementById('ads_left');
    if(adsleft){
        adsleft.remove();
    }
    watchElementByID('feed_rows');
    watchElementByID('page_wall_posts');

    function watchElementByID(id){
        var el = document.getElementById(id);
        if(!el) return;
        // remove romoted posts
        var m = new MutationObserver(function(mutations) {
            mutations.forEach(function(mutation) {
                if(mutation.type=='childList' /*&& mutation.target.className == '_feed_rows'*/){
                    // console.log(mutation);
                    removePromotedPosts(mutation.target);
                }
            });
        });
        m.observe(el, {childList:true, subtree:true});
        removePromotedPosts(el);
    }

    function removePromotedPosts(node){
        if(!node) return;
        node.querySelectorAll('.feed_block_articles').forEach(function(e){
            if (e){
                e.closest('.feed_row').remove();
            }
        });
        node.querySelectorAll('.post._ads_promoted_post_data_w').forEach(function(e){
            if (e){
                // e.closest('.post').remove();
                e.remove();
            }
        });
        node.querySelectorAll('.wall_marked_as_ads').forEach(function(e){
            if (e){
                e.closest('.post').remove();
            }
        });
    }

})();