VOZ - Remove tag [HN]

VOZ - Remove tag [HN] in the alert page

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         VOZ - Remove tag [HN]
// @version              0.1.8
// @namespace    VOZ
// @description  VOZ - Remove tag [HN] in the alert page
// @author       N.Duong
// @license              MIT
// @match        https://voz.vn/*
// @run-at               document-start
// @icon         https://www.google.com/s2/favicons?sz=64&domain=voz.vn
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function safeRemoveHNItems() {
        try {
            const items = document.querySelectorAll('li[data-alert-id]');
            let removedCount = 0;
            items.forEach(item => {
                try {
                    const labelSpan = item.querySelector('span.label.label--green');
                    if (labelSpan && labelSpan.textContent.trim() === 'HN') {
                        item.remove();
                        removedCount++;
                    }
                } catch (err) {
                    console.warn('Error checking/removing item:', err);
                }
            });
            if (removedCount > 0) {
                console.log(`Removed ${removedCount} HN items`);
            }
        } catch (err) {
            console.error('Error removing HN items:', err);
        }
    }

    safeRemoveHNItems();

    let observer = null;

    function setupObserver() {
        if (observer) {
            observer.disconnect();
        }

        observer = new MutationObserver((mutations) => {
            let shouldCheck = false;
            for (const mutation of mutations) {
                if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
                    shouldCheck = true;
                    break;
                }
            }

            if (shouldCheck) {
                clearTimeout(window.hnRemoveTimeout);
                window.hnRemoveTimeout = setTimeout(safeRemoveHNItems, 100);
            }
        });

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

        console.log('MutationObserver started for HN removal');
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', () => {
            safeRemoveHNItems();
            setupObserver();
            tryInjectBadge();
        });
    } else {
        safeRemoveHNItems();
        setupObserver();
        tryInjectBadge();
    }

    function tryInjectBadge() {
        let attempts = 0;
        const maxAttempts = 10;
        const intervalMs = 500;

        const retryInterval = setInterval(() => {
            attempts++;

            try {
                const alertLink = safeQuery('a[data-nav-id="ALERTS"]');
                const numberOfAlert = safeQuery('a[href="/account/alerts"]');

                if (alertLink && numberOfAlert) {
                    clearInterval(retryInterval);

                    const badgeValue = numberOfAlert.getAttribute('data-badge') || '0';
                    console.log("FOUND ALERTS link, badge =", badgeValue);

                    if (!alertLink.querySelector('.vn-alert-badge')) {
                        const badge = document.createElement('span');
                        badge.textContent = badgeValue;
                        badge.className = 'vn-alert-badge';
                        badge.style.cssText = `
                            background: red;
                            color: white;
                            border-radius: 21%;
                            padding: 2px 6px;
                            font-size: 12px;
                            margin-left: 6px;
                        `;
                        alertLink.appendChild(badge);
                    }
                }

                if (attempts >= maxAttempts) {
                    clearInterval(retryInterval);
                    console.warn("ALERTS link not found after 10 attempts.");
                }
            } catch (err) {
                console.error('Error during badge injection attempt:', err);
            }
        }, intervalMs);
    }

    function safeQuery(selector) {
        try {
            return document.querySelector(selector);
        } catch (err) {
            console.error(`Query failed for selector: ${selector}`, err);
            return null;
        }
    }

    document.addEventListener('visibilitychange', () => {
        if (!document.hidden) {
            setTimeout(safeRemoveHNItems, 500);
        }
    });

})();