flyink13 / VK Negative filter

// ==UserScript==
// @name         VK Negative filter
// @namespace    http://vk.com/
// @version      0.2
// @description  Скрипт скрывает все посты, в которых есть негативная эмоция
// @author       FlyInk
// @match        https://*.vk.com/*
// @icon         https://www.google.com/s2/favicons?domain=vk.com
// @grant        none
// @copyright 2021, flyink13 (https://openuserjs.org/users/flyink13)
// @downloadURL https://openuserjs.org/install/flyink13/VK_Negative_filter.user.js
// @updateURL https://openuserjs.org/meta/flyink13/VK_Negative_filter.meta.js
// @license MIT
// ==/UserScript==
/* global gpeByClass, ce */


function Main() {
    // Функция проверки новых элементов на негатив и печаль
    function checkElements(el) {
        // Тут указаны селекторы через aria-label, поэтому может не работать на различных языках
        document.querySelectorAll('.ReactionsPreview [aria-label="&$#%!"], .ReactionsPreview [aria-label="Печаль"]').forEach((el) => {
            const post = gpeByClass('page_block', el) || gpeByClass('wl_post', el); // Ищем пост в родителе найденного элемента
            if (!post || post.negativeChecked) return; // Если нет или уже проверяли - пропускаем
            const negativeClass = 'negativePost';
            post.classList.add(negativeClass); // добавляем класс посту, что он негативный
            post.addEventListener('click', () => post.classList.remove(negativeClass)); // обработчик нажатия, для снятия метки
            post.negativeChecked = 1; // Кещ
        });
    }

    // Создаем обработчик мутаций элемента
    var observer = new MutationObserver(function (mutations) {
        mutations.forEach(function (mutation) { // Перебираем обновленя в элементах
            if (mutation.target.nodeType !== 1) return; // Если элемент не блок, то выходим
            checkElements(mutation.target); // Отдаем элемент на проверку ссылок
        });
    });

    observer.observe(document.body, { // Запускаем обработчик мутаций
        childList: true, // Проведять детей элемента
        subtree: true // по всему дереву
    });

    // Стили для негативных постов
    document.head.appendChild(document.createElement("style")).innerHTML = `
.negativePost:before {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64' width='64' height='64'%3E%3Cg transform='matrix(0.125,0,0,0.125,0,0)'%3E%3Cg transform='matrix(6.119885444641113,0,0,5.880114555358887,256,259.8363342285156)' %3E%3Cg transform='matrix(1,0,0,1,0,0)'%3E%3Cpath fill='%23ddd' d=' M32,0 C32,17.67300033569336 17.67300033569336,32 0,32 C-17.67300033569336,32 -32,17.67300033569336 -32,0 C-32,-17.67300033569336 -17.67300033569336,-32 0,-32 C17.67300033569336,-32 32,-17.67300033569336 32,0z'%3E%3C/path%3E%3C/g%3E%3C/g%3E%3Cg transform='matrix(6.119391441345215,0,0,5.879639625549316,255.99746704101562,315.02301025390625)' %3E%3Cg transform='matrix(1,0,0,1,0,0)'%3E%3Cpath fill='%23aaa' d=' M-10.630000114440918,11.897000312805176 C-10.229000091552734,10.359999656677246 -7,4.136000156402588 -0.03500000014901161,4.136000156402588 C6.084000110626221,4.136000156402588 9.458999633789062,9.317999839782715 10.272000312805176,12.001999855041504 C10.472999572753906,12.663999557495117 10.0649995803833,13.406000137329102 9.404000282287598,13.609999656677246 C8.770999908447266,13.819000244140625 8.227999687194824,13.786999702453613 7.728000164031982,13.1850004196167 C7.328999996185303,12.680999755859375 4.0920000076293945,9.135000228881836 -0.08500000089406967,9.04699993133545 C-4.0329999923706055,8.96399974822998 -7.210000038146973,12.319000244140625 -7.869999885559082,13.144000053405762 C-8.359999656677246,13.755999565124512 -9.305999755859375,13.859000205993652 -9.875,13.463000297546387 C-10.307999610900879,13.16100025177002 -10.755000114440918,12.378000259399414 -10.630000114440918,11.897000312805176z'%3E%3C/path%3E%3C/g%3E%3C/g%3E%3Cg transform='matrix(-6.119391441345215,0,0,5.879639625549316,256,329.71917724609375)' %3E%3Cg transform='matrix(1.2170000076293945,0,0,1.1430000066757202,-2.1700000762939453,1.564133644104004)'%3E%3Cg transform='matrix(1,0,0,1,0,0)'%3E%3Cpath fill='%23aaa' d=' M5.955054759979248,-10.89663028717041 C6.039545059204102,-7.154792785644531 7.75983190536499,-5.2176690101623535 10.003358840942383,-5.214479923248291 C12.224392890930176,-5.211843490600586 13.973041534423828,-7.1687397956848145 14.040480613708496,-10.897906303405762 C14.106346130371094,-14.626434326171875 12.322404861450195,-16.86007308959961 9.998554229736328,-16.86389923095703 C7.681167125701904,-16.86764144897461 5.897225856781006,-14.6257963180542 5.955054759979248,-10.89663028717041z'%3E%3C/path%3E%3C/g%3E%3C/g%3E%3Cg transform='matrix(1,0,0,1,0,0.05200004577636719)'%3E%3Cg transform='matrix(1,0,0,1,1,0)'%3E%3Cpath fill='%23ccc' d=' M8.008121490478516,-12.999373435974121 C8.008121490478516,-11.94063663482666 8.899592399597168,-11.094663619995117 10.00016975402832,-11.094663619995117 C11.100748062133789,-11.094663619995117 11.992218971252441,-11.941912651062012 11.992218971252441,-13.000649452209473 C11.992218971252441,-14.059385299682617 11.100748062133789,-14.92717170715332 10.00016975402832,-14.92717170715332 C8.899592399597168,-14.92717170715332 8.008121490478516,-14.058110237121582 8.008121490478516,-12.999373435974121z'%3E%3C/path%3E%3C/g%3E%3C/g%3E%3C/g%3E%3Cg transform='matrix(6.119391441345215,0,0,5.879639625549316,256.50994873046875,329.71917724609375)' %3E%3Cg transform='matrix(1.2170000076293945,0,0,1.1430000066757202,-2.1700000762939453,1.564133644104004)'%3E%3Cg transform='matrix(1,0,0,1,0,0)'%3E%3Cpath fill='%23aaa' d=' M5.955054759979248,-10.89663028717041 C6.039545059204102,-7.154792785644531 7.75983190536499,-5.2176690101623535 10.003358840942383,-5.214479923248291 C12.224392890930176,-5.211843490600586 13.973041534423828,-7.1687397956848145 14.040480613708496,-10.897906303405762 C14.106346130371094,-14.626434326171875 12.322404861450195,-16.86007308959961 9.998554229736328,-16.86389923095703 C7.681167125701904,-16.86764144897461 5.897225856781006,-14.6257963180542 5.955054759979248,-10.89663028717041z'%3E%3C/path%3E%3C/g%3E%3C/g%3E%3Cg transform='matrix(1,0,0,1,-2,0.05200004577636719)'%3E%3Cg transform='matrix(1,0,0,1,1,0)'%3E%3Cpath fill='%23ccc' d=' M8.008121490478516,-12.999373435974121 C8.008121490478516,-11.94063663482666 8.899592399597168,-11.094663619995117 10.00016975402832,-11.094663619995117 C11.100748062133789,-11.094663619995117 11.992218971252441,-11.941912651062012 11.992218971252441,-13.000649452209473 C11.992218971252441,-14.059385299682617 11.100748062133789,-14.92717170715332 10.00016975402832,-14.92717170715332 C8.899592399597168,-14.92717170715332 8.008121490478516,-14.058110237121582 8.008121490478516,-12.999373435974121z'%3E%3C/path%3E%3C/g%3E%3C/g%3E%3C/g%3E%3Cg transform='matrix(6.4110822677612305,0,0,6.159903526306152,253.82464599609375,318.3469543457031)' %3E%3Cg transform='matrix(1,0,0,1,0,0)'%3E%3Cpath fill='%23aaa' d=' M-7.001999855041504,-19.333999633789062 C-7.611999988555908,-18.54800033569336 -8.142000198364258,-17.937999725341797 -8.831999778747559,-17.399999618530273 C-9.508999824523926,-16.878999710083008 -10.243000030517578,-16.43600082397461 -11.020999908447266,-16.089000701904297 C-11.795999526977539,-15.739999771118164 -12.607000350952148,-15.468000411987305 -13.439000129699707,-15.321000099182129 C-14.265999794006348,-15.145000457763672 -15.170000076293945,-15.199000358581543 -16,-15.208000183105469 C-16.437000274658203,-15.213000297546387 -16.78700065612793,-15.569999694824219 -16.781999588012695,-16.006999969482422 C-16.777999877929688,-16.417999267578125 -16.454999923706055,-16.83099937438965 -16.054000854492188,-16.865999221801758 C-16.054000854492188,-16.865999221801758 -15.994000434875488,-16.871000289916992 -15.994000434875488,-16.871000289916992 C-15.281000137329102,-16.93000030517578 -14.699000358581543,-17.09000015258789 -14.026000022888184,-17.299999237060547 C-13.347000122070312,-17.481000900268555 -12.70199966430664,-17.766000747680664 -12.095999717712402,-18.106000900268555 C-11.48799991607666,-18.444000244140625 -10.92300033569336,-18.85700035095215 -10.413999557495117,-19.32699966430664 C-9.916999816894531,-19.775999069213867 -9.413999557495117,-20.357999801635742 -9.118000030517578,-20.825000762939453 C-9.118000030517578,-20.825000762939453 -9.086000442504883,-20.874000549316406 -9.086000442504883,-20.874000549316406 C-8.729999542236328,-21.434999465942383 -7.86899995803833,-21.44300079345703 -7.308000087738037,-21.086999893188477 C-6.747000217437744,-20.731000900268555 -6.579999923706055,-19.988000869750977 -6.935999870300293,-19.427000045776367 C-6.955999851226807,-19.395000457763672 -6.979000091552734,-19.363000869750977 -7.001999855041504,-19.333999633789062z'%3E%3C/path%3E%3C/g%3E%3C/g%3E%3Cg transform='matrix(-6.4110822677612305,0,0,6.159903526306152,251.86424255371094,318.3469543457031)' %3E%3Cg transform='matrix(1,0,0,1,0,0)'%3E%3Cpath fill='%23aaa' d=' M-7.001999855041504,-19.333999633789062 C-7.611999988555908,-18.54800033569336 -8.142000198364258,-17.937999725341797 -8.831999778747559,-17.399999618530273 C-9.508999824523926,-16.878999710083008 -10.243000030517578,-16.43600082397461 -11.020999908447266,-16.089000701904297 C-11.795999526977539,-15.739999771118164 -12.607000350952148,-15.468000411987305 -13.439000129699707,-15.321000099182129 C-14.265999794006348,-15.145000457763672 -15.170000076293945,-15.199000358581543 -16,-15.208000183105469 C-16.437000274658203,-15.213000297546387 -16.78700065612793,-15.569999694824219 -16.781999588012695,-16.006999969482422 C-16.777999877929688,-16.417999267578125 -16.454999923706055,-16.83099937438965 -16.054000854492188,-16.865999221801758 C-16.054000854492188,-16.865999221801758 -15.994000434875488,-16.871000289916992 -15.994000434875488,-16.871000289916992 C-15.281000137329102,-16.93000030517578 -14.699000358581543,-17.09000015258789 -14.026000022888184,-17.299999237060547 C-13.347000122070312,-17.481000900268555 -12.70199966430664,-17.766000747680664 -12.095999717712402,-18.106000900268555 C-11.48799991607666,-18.444000244140625 -10.92300033569336,-18.85700035095215 -10.413999557495117,-19.32699966430664 C-9.916999816894531,-19.775999069213867 -9.413999557495117,-20.357999801635742 -9.118000030517578,-20.825000762939453 C-9.118000030517578,-20.825000762939453 -9.086000442504883,-20.874000549316406 -9.086000442504883,-20.874000549316406 C-8.729999542236328,-21.434999465942383 -7.86899995803833,-21.44300079345703 -7.308000087738037,-21.086999893188477 C-6.747000217437744,-20.731000900268555 -6.579999923706055,-19.988000869750977 -6.935999870300293,-19.427000045776367 C-6.955999851226807,-19.395000457763672 -6.979000091552734,-19.363000869750977 -7.001999855041504,-19.333999633789062z'%3E%3C/path%3E%3C/g%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
    content: "Этот пост может испортить вам настроение, хотите его посмотреть?";
    position: absolute;
    text-align: center;
    width: 70%;
    z-index: 2;
    box-sizing: border-box;
    color: #333;
    overflow: hidden;
    background-size: 72px;
    background-position: center 26px;
    background-repeat: no-repeat;
    padding: 32px;
    padding-top: 112px;
    margin-top: 104px;
    font-weight: 500;
    margin-left: 15%;
    background-color: #fff;
    border-radius: 5px;
    border: 1px solid #ccc;
}

.negativePost .post_info,
.negativePost .wl_post_body_wrap {
    filter: blur(20px);
    opacity: 0.2;
    pointer-events: none;
    height: 260px;
    overflow: hidden;
}
`;
}

// Вставляем Main в страницу
(function injectScript() {
    const script = document.createElement('script');
    const code = '(' + Main + ')();';
    script.appendChild(document.createTextNode(code));
    (document.body || document.head || document.documentElement).appendChild(script);
})();