hide-older-comments

Hide old comments, highlight only parent text if it has newer children

Verzia zo dňa 25.02.2025. Pozri najnovšiu verziu.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, Greasemonkey alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, % alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, % alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey alebo Userscripts.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie správcu používateľských skriptov.

(Už mám správcu používateľských skriptov, nechajte ma ho nainštalovať!)

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

(Už mám správcu používateľských štýlov, nechajte ma ho nainštalovať!)

// ==UserScript==
// @name         hide-older-comments
// @namespace    https://orbitar.space
// @version      1.3
// @description  Hide old comments, highlight only parent text if it has newer children
// @match        https://*.orbitar.space/*
// @match        https://orbitar.space/*
// @license MIT
// ==/UserScript==

(function () {
    'use strict';

    console.log("✅ Comment filtering script is running...");

    function isPostPage() {
        return /^https:\/\/orbitar\.space\/(p\d+|s\/[^\/]+\/p\d+)([\?].*|[\#].*)?$/.test(window.location.href);
    }

    function observeForLinksSection() {
        const observer = new MutationObserver(() => {
            let linksSection = document.querySelector('[class^="PostPage_postButtons__"]');
            if (linksSection && !document.getElementById("comment-filter-container")) {
                createUI(linksSection);
            }
        });

        observer.observe(document.body, { childList: true, subtree: true });
    }

    function createUI(linksSection) {
        if (document.getElementById("comment-filter-container")) return;

        let container = document.createElement("span");
        container.id = "comment-filter-container";
        //container.style.marginLeft = "10px";

        let divider = document.createTextNode(" • ");
        container.appendChild(divider);

        let showFilterLink = document.createElement("a");
        showFilterLink.href = "#";
        showFilterLink.innerText = "фильтровать по дате";
        showFilterLink.onclick = (event) => {
            event.preventDefault();
            filterUI.style.display = "inline";
            showFilterLink.style.display = "none";
        };

        let filterUI = document.createElement("span");
        filterUI.style.display = "none";
        filterUI.style.marginLeft = "10px";

        let dateInput = document.createElement("input");
        dateInput.type = "datetime-local";
        dateInput.id = "comment-filter-date";
        dateInput.style.marginRight = "5px";

        let filterLink = document.createElement("a");
        filterLink.href = "#";
        filterLink.innerText = "фильтровать";
        filterLink.onclick = (event) => {
            event.preventDefault();
            filterComments();
        };

        let clearLink = document.createElement("a");
        clearLink.href = "#";
        clearLink.innerText = "очистить";
        clearLink.style.marginLeft = "10px";
        clearLink.onclick = (event) => {
            event.preventDefault();
            clearFilter();
        };

        let quickLinks = document.createElement("div");
        quickLinks.style.marginTop = "10px";

        ["3 часа", "сутки", "3 дня"].forEach((text, index) => {
            let timeAdjustLink = document.createElement("a");
            timeAdjustLink.href = "#";
            timeAdjustLink.innerText = text;
            timeAdjustLink.style.marginRight = "5px";
            timeAdjustLink.onclick = (event) => {
                event.preventDefault();
                adjustDate(index);
            };
            quickLinks.appendChild(timeAdjustLink);
            if (index < 2) quickLinks.appendChild(document.createTextNode(" | "));
        });
        ["gray", "pink", "blue"].forEach((color, index) => {
            quickLinks.appendChild(document.createTextNode(" | ")); // Add divider
            let colorLink = document.createElement("a");
            colorLink.href = "#";
            colorLink.innerText = color;
            colorLink.style.marginRight = "5px";
            colorLink.onclick = (event) => {
                event.preventDefault();
                changeParentCommentBgColor("light"+color);
            };
            quickLinks.appendChild(colorLink);
        });

        filterUI.appendChild(dateInput);
        filterUI.appendChild(filterLink);
        filterUI.appendChild(clearLink);
        filterUI.appendChild(quickLinks);

        container.appendChild(showFilterLink);
        container.appendChild(filterUI);
        linksSection.appendChild(container);
    }

    let parentCommentBgColor = 'lightgray'; // Default color

    function changeParentCommentBgColor(color) {
        parentCommentBgColor = color;
        let comments = document.querySelectorAll('.comment');
        comments.forEach(comment => {
            let commentTextContainer = comment.querySelector('[class^="CommentComponent_content__"]');
            if (commentTextContainer && (commentTextContainer.style.backgroundColor === 'lightgray' || commentTextContainer.style.backgroundColor === 'lightpink' || commentTextContainer.style.backgroundColor === 'lightblue')) {
                commentTextContainer.style.backgroundColor = color;
            }
        });
    }

    function adjustDate(option) {
        let dateInput = document.getElementById("comment-filter-date");
        if (!dateInput) return;

        let currentDate = new Date();
        switch (option) {
            case 0:
                currentDate.setHours(currentDate.getHours() - 3);
                break;
            case 1:
                currentDate.setDate(currentDate.getDate() - 1);
                break;
            case 2:
                currentDate.setDate(currentDate.getDate() - 3);
                break;
        }

        let localISOTime = new Date(currentDate.getTime() - currentDate.getTimezoneOffset() * 60000)
            .toISOString()
            .slice(0, 16);

        dateInput.value = localISOTime;
    }

    function filterComments() {
        let selectedDate = new Date(document.getElementById("comment-filter-date").value);
        if (isNaN(selectedDate)) {
            console.error("Invalid date selected.");
            return;
        }

        console.log("Filtering comments older than:", selectedDate.toString());

        let comments = document.querySelectorAll('.comment');
        let commentMap = new Map();

        comments.forEach(comment => {
            let dateElement = findDateElement(comment);
            if (!dateElement) return;

            let commentDateText = dateElement.innerText.trim();
            let commentDate = parseCommentDate(commentDateText);
            let commentId = comment.dataset.commentId;

            if (isNaN(commentDate)) return;

            commentMap.set(commentId, { comment, commentDate, hasNewerChild: false });
        });

        comments.forEach(comment => {
            let commentId = comment.dataset.commentId;
            let commentData = commentMap.get(commentId);
            if (!commentData) return;

            let { commentDate } = commentData;

            let childComments = [...comment.querySelectorAll('.comment')];
            let hasNewerChild = childComments.some(child => {
                let childId = child.dataset.commentId;
                return commentMap.has(childId) && commentMap.get(childId).commentDate >= selectedDate;
            });

            if (hasNewerChild) {
                commentData.hasNewerChild = true;
            }
        });

        comments.forEach(comment => {
            let commentId = comment.dataset.commentId;
            let commentData = commentMap.get(commentId);

            if (!commentData) return;

            let { commentDate, hasNewerChild } = commentData;

            let commentTextContainer = comment.querySelector('[class^="CommentComponent_content__"]');
            if (!commentTextContainer) return;

            if (commentDate >= selectedDate) {
                comment.style.display = "";
                commentTextContainer.style.backgroundColor = "";
            } else if (hasNewerChild) {
                comment.style.display = "";
                commentTextContainer.style.backgroundColor = parentCommentBgColor;
            } else {
                comment.style.display = "none";
            }
        });
    }

    function clearFilter() {
        let comments = document.querySelectorAll('.comment');
        comments.forEach(comment => {
            comment.style.display = "";
            let commentTextContainer = comment.querySelector('[class^="CommentComponent_content__"]');
            if (commentTextContainer) {
                commentTextContainer.style.backgroundColor = "";
            }
        });
        console.log("Filter cleared, all comments visible.");
    }

    function findDateElement(comment) {
        let signature = comment.querySelector('[class^="SignatureComponent_signature__"]');
        if (!signature) return null;

        let dateLinks = signature.querySelectorAll("a");
        return dateLinks.length >= 2 ? dateLinks[1] : null;
    }

    function parseCommentDate(dateText) {
    let months = {
        "января": "January", "февраля": "February", "марта": "March",
        "апреля": "April", "мая": "May", "июня": "June",
        "июля": "July", "августа": "August", "сентября": "September",
        "октября": "October", "ноября": "November", "декабря": "December"
    };

    // Handle "вчера в 12:34" format
    if (dateText.startsWith("вчера в")) {
        let match = dateText.match(/вчера в (\d{1,2}):(\d{2})/);
        if (match) {
            let yesterday = new Date();
            yesterday.setDate(yesterday.getDate() - 1);
            yesterday.setHours(parseInt(match[1], 10), parseInt(match[2], 10), 0, 0);
            return yesterday;
        }
    }

    // Handle "сегодня в 12:34" format
    if (dateText.startsWith("сегодня в")) {
        let match = dateText.match(/сегодня в (\d{1,2}):(\d{2})/);
        if (match) {
            let today = new Date();
            today.setHours(parseInt(match[1], 10), parseInt(match[2], 10), 0, 0);
            return today;
        }
    }

    // Handle Old Format: "9 февраля в 12:12"
    let oldFormatMatch = dateText.match(/(\d{1,2})\s([а-яА-Я]+)\sв\s(\d{1,2}):(\d{2})/);
    if (oldFormatMatch) {
        let [_, day, monthName, hour, minute] = oldFormatMatch;
        let month = months[monthName];
        if (!month) return NaN;

        let currentYear = new Date().getFullYear();
        return new Date(`${day} ${month} ${currentYear} ${hour}:${minute}`);
    }

    // Handle New Format: "27.08.2024 12:17"
    let newFormatMatch = dateText.match(/(\d{2})\.(\d{2})\.(\d{4})\s(\d{2}):(\d{2})/);
    if (newFormatMatch) {
        let [_, day, month, year, hour, minute] = newFormatMatch;
        return new Date(`${year}-${month}-${day}T${hour}:${minute}:00`);
    }

    return NaN; // If no format matched, return NaN
}


    if (isPostPage()) {
        observeForLinksSection();
    }
})();