FFLogs Diff Column

Add a diff column to FFLogs events table

目前為 2024-06-13 提交的版本,檢視 最新版本

// ==UserScript==
// @name         FFLogs Diff Column
// @namespace    https://greasyfork.org/en/users/1317382
// @version      1.0
// @description  Add a diff column to FFLogs events table
// @author       aya
// @match        https://www.fflogs.com/reports/*
// @require      https://code.jquery.com/jquery-3.6.0.min.js
// @license    	 MIT
// ==/UserScript==

(function() {
    'use strict';

    function waitForDependencies(callback) {
        if (typeof jQuery !== 'undefined') {
            callback();
        } else {
            setTimeout(() => waitForDependencies(callback), 100);
        }
    }

    function formatDiff(diff) {
        return diff.toFixed(3).replace(/(\.0+|0+)$/, '');
    }

    function timeToMilliseconds(timeStr) {
        const [minutes, seconds] = timeStr.replace('-', '').split(':').map(parseFloat);
        return (timeStr.startsWith('-') ? -1 : 1) * (minutes * 60000 + seconds * 1000);
    }

    function addDiffColumn() {
        if (!$(".events-table thead th").length || $(".events-table thead th.diff-column").length) return;

        $(".events-table thead th").first().after("<th class='ui-state-default sorting_disabled diff-column'>Diff</th>");
        let last = null;

        $(".main-table-number").each(function() {
            if ($(this).next('td.diff-column').length) return;
            const current = timeToMilliseconds($(this).text());
            $(this).after("<td class='diff-column' style='width:2em'>" + (last === null ? '-' : formatDiff((current - last) / 1000)) + "</td>");
            last = current;
        });
    }

    function urlContainsParams() {
        const url = window.location.href;
        return url.includes("&view=events") && url.includes("&type=casts");
    }

    function checkAndRunScript() {
        if (observer) observer.disconnect();

        if (urlContainsParams()) {
            observer = new MutationObserver((mutations) => {
                if (mutations.some(mutation => mutation.addedNodes.length && document.querySelector('.events-table'))) {
                    addDiffColumn();
                }
            });

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

    let observer = null;
    checkAndRunScript();
    window.addEventListener('popstate', checkAndRunScript);

    function proxyHistoryMethod(method) {
        return new Proxy(method, {
            apply(target, thisArg, argArray) {
                target.apply(thisArg, argArray);
                window.dispatchEvent(new Event('popstate'));
            }
        });
    }

    history.pushState = proxyHistoryMethod(history.pushState);
    history.replaceState = proxyHistoryMethod(history.replaceState);

})();