Fix broken tooltips

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

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