VK comments blacklist FIXED

Hides comments from blacklisted users

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         VK comments blacklist FIXED
// @namespace    http://tampermonkey.net/
// @version      0.1.0
// @description  Hides comments from blacklisted users
// @author       ryba kaplya
// @match        https://vk.com/*
// @icon         https://www.google.com/s2/favicons?domain=vk.com
// @grant        none
// @require      https://code.jquery.com/jquery-3.6.0.min.js
// ==/UserScript==

(function () {
    'use strict';

    const $ = window.jQuery;
    const UFO_TEXT = 'НЛО прилетело и оставило здесь эту запись';
    const HIDDEN_CLASS = 'vkcbl-hidden';
    const BUTTON_CLASS = 'vkcbl-btn';

    let blacklist = JSON.parse(localStorage.getItem('vkBlacklistIds') || '{}');

    const saveBlacklist = () =>
        localStorage.setItem('vkBlacklistIds', JSON.stringify(blacklist));

    const createButton = (icon, title, userid, action) =>
        $('<div>', {
            class: `reply_action fl_r ${BUTTON_CLASS}`,
            text: icon,
            title,
            'data-userid': userid,
            click: e => {
                e.preventDefault();
                e.stopPropagation();
                action(userid);
            }
        });

    const hideComment = ($comment, userid) => {
        if ($comment.hasClass(HIDDEN_CLASS)) return;

        $comment.addClass(HIDDEN_CLASS).hide();

        const $ufo = $('<div>', { class: 'reply reply_dived clear vkcbl-ufo' }).append(
            $('<div>', {
                style: 'color:#aaa; margin:7px 0 7px 44px',
                text: UFO_TEXT
            }).append(createButton('👀', 'Показать сообщение', userid, forgiveUser))
        );

        $comment.after($ufo);
    };

    const showComment = userid => {
        const $comments = getUserComments(userid);
        $comments.removeClass(HIDDEN_CLASS).show();
        $comments.nextAll('.vkcbl-ufo').remove();
    };

    const forgiveUser = userid => {
        delete blacklist[userid];
        saveBlacklist();
        showComment(userid);
        updateButtons();
    };

    const blockUser = userid => {
        blacklist[userid] = true;
        saveBlacklist();
        hideUserComments(userid);
        updateButtons();
    };

    const getUserComments = userid =>
        $(`a.author[data-from-id="${userid}"]`).closest('.reply');

    const getUseridFromComment = $comment =>
        $comment.find('a.author').data('from-id');

    const hideUserComments = userid =>
        getUserComments(userid).each(function () {
            hideComment($(this), userid);
        });

    const updateButtons = () => {
        $('.reply').each(function () {
            const $comment = $(this);
            const userid = getUseridFromComment($comment);
            if (!userid) return;

            const $actions = $comment.find('.post_actions_wrap .post_actions');
            if (!$actions.length || $actions.find(`.${BUTTON_CLASS}`).length) return;

            $actions.append(createButton('🚫', 'В ЧС', userid, blockUser));
        });
    };

    const applyBlacklist = () =>
        Object.keys(blacklist).forEach(hideUserComments);

    const observeComments = () => {
        new MutationObserver(() => {
            applyBlacklist();
            updateButtons();
        }).observe(document.body, { childList: true, subtree: true });
    };

    const init = () => {
        applyBlacklist();
        updateButtons();
        observeComments();
    };

    const interval = setInterval(() => {
        if ($('.reply').length) {
            clearInterval(interval);
            init();
        }
    }, 500);
})();