VK posts filter

Скрывает рекламные и политические посты в vk.com.

As of 11.09.2016. See апошняя версія.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name            VK posts filter
// @name:en         VK posts filter
// @namespace       FIX
// @version         0.2.1
// @description     Скрывает рекламные и политические посты в vk.com.
// @description:en  Hide ad and political posts from vk.com.
// @copyright		2016, raletag
// @author			raletag
// @supportURL      http://greasyfork.org/ru/forum/messages/add/raletag
// @include         *://vk.com/*
// @exclude         *://vk.com/*.php*
// @grant           GM_addStyle
// ==/UserScript==

(function() {
    'use strict';
    console.time('VK posts filter load');
    var
    ads = 'побед(a|у) (в сражении|над боссом)|я повысил(|a) уровень|я тебя обогнал|вступай(|те) в|расска(зать|жите) дру(зьям|гу) про запись|сдела(й|йте|ете|ть) репост|добав(ь|те|ляем|ляй) (активн(ого|ых))|(пиар(|ся|ьтесь) (тут|здесь))|подпи(шись|саться|сывайся) на|активно добавля(й|ем)|(быть|стать) (подписчиком|участником)|уровень в игре|получить бесплатно|зач(e|ё)тны(e|й) кореш(|а)|победител(ь|я|и) розыгрыш|победил(|а) босс|я одержал(|a) побед|внимание розыгрыш|добав(ь|ьте|ляйтесь) в дру|за репост|сделавшему репост|при(ми|мите|нять|нимайте) участие в|результат(|ы) конкурс|(прошел(|a)|меня) [0-9]{1,} уровень|зака(жи|жите|зывай|зывайте) по акции|я выполнил(|а) задание|успей(|те) вступить',
    urlads = 'vk\.com\/(app|top_cards|fotomimi|megatest|beauty_cards|fotomagic_su|we_love_cards|denegnoe_derevo|skanograf|friends\?act=invite&group_id=)|clickerwars\.com\/vk|denezhnojederevo\.ru',
    politiс = 'выборы в|правительств|националист|оппозици|петици|митинг|парламент|фальсификац|госдум|арест',
    regexp = new RegExp('(' + ads + '|' + urlads + '|' + politiс +')', 'mi'),
    showheader = true; // false - полностью скрывать пост

    if (showheader === true) new GM_addStyle('.post_header_fix {padding: 15px 20px 15px!important;}');

    function filter (e) {
        var text = e.querySelector('.wall_post_text');
        e.setAttribute('vpf', '1');
        if ((e.querySelector('a.lnk[href*="vk.com/app"]') || (text && regexp.test(text.textContent) === true))) {
            if (showheader === true) {
                e.querySelector('.post_header').classList.add('post_header_fix');
                e.querySelector('.post_content').innerHTML = '';
            } else {
                e.innerHTML = '';
            }
        }
    }

    var links = document.body.querySelectorAll('div[data-post-id]:not([vpf])');
    for (var i = links.length - 1; i >= 0; --i) {
        filter (links[i]);
    }

    var o = new MutationObserver(function (ms) {
        ms.forEach(function (m) {
            m.addedNodes.forEach(function (n) {
                if (n.nodeType !== Node.ELEMENT_NODE) {
                    return;
                } else if (n.getAttribute('data-post-id') && !n.getAttribute('vpf')) {
                    filter (n);
                } else {
                    var links = n.querySelectorAll('div[data-post-id]:not([vpf])');
                    for (var i = links.length - 1; i >= 0; --i) {
                        filter (links[i]);
                    }
                }
            });
        });
    });
    o.observe(document.body, {childList: true, subtree: true});
    console.timeEnd('VK posts filter load');
})();