Fix broken tooltips

Fixes tooltips not showing for icons near edges or right-most in lists

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         Fix broken tooltips
// @namespace    torn.com.tooltip.fix
// @version      1.8
// @description  Fixes tooltips not showing for icons near edges or right-most in lists
// @author       Xiphias[187717]
// @match        https://www.torn.com/*
// @run-at       document-end
// @license      MIT
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    const TOOLTIP_SELECTOR = '[data-floating-ui-portal] [role="tooltip"]';

    function getNewestTooltip() {
        const tooltips = [...document.querySelectorAll(TOOLTIP_SELECTOR)]
        .filter(t => t instanceof HTMLElement);

        return tooltips.at(-1) || null;
    }

    function hideOtherTooltips(activeTooltip) {
        document.querySelectorAll(TOOLTIP_SELECTOR).forEach(tooltip => {
            if (tooltip instanceof HTMLElement && tooltip !== activeTooltip) {
                tooltip.style.opacity = '0';
            }
        });
    }

    function fixAllTooltips() {
        const activeTooltip = getNewestTooltip();

        if (!activeTooltip) {
            return;
        }

        hideOtherTooltips(activeTooltip);

        if (activeTooltip.style.opacity === '0') {
            activeTooltip.style.opacity = '1';
        }
    }

    const observer = new MutationObserver((mutations) => {
        for (const mutation of mutations) {
            for (const node of mutation.addedNodes) {
                if (
                    node instanceof HTMLElement &&
                    (
                        node.matches?.(TOOLTIP_SELECTOR) ||
                        node.querySelector?.(TOOLTIP_SELECTOR)
                    )
                ) {
                    requestAnimationFrame(fixAllTooltips);
                    return;
                }
            }
        }
    });

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

    window.addEventListener('resize', fixAllTooltips, true);
    window.addEventListener('scroll', fixAllTooltips, true);
})();