Fix broken tooltips

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

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

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

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==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);
})();