Fix broken tooltips

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

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

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.

(I already have a user style manager, let me install it!)

// ==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);
})();