Warcraft Logs Number Abbreviator

Abbreviate big numbers in the event log using K,M,B suffixes.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Warcraft Logs Number Abbreviator
// @namespace    athei
// @author       Alexander Theißen
// @version      1.0.0
// @description  Abbreviate big numbers in the event log using K,M,B suffixes.
// @match        http://*.warcraftlogs.com/*
// @match        https://*.warcraftlogs.com/*
// @grant        none
// @license      GPL3
// ==/UserScript==

// Process nodes that exist at load time of this script.
if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', processNodes);
} else {
    processNodes();
}

// Process nodes that are added after this script is run.
const observer = new MutationObserver(mutations => {
    mutations.forEach(mutation => {
        mutation.addedNodes.forEach(node => {
            // Only process element nodes.
            if (node.nodeType === Node.ELEMENT_NODE) {
                // If the added node itself is a span, process it.
                if (node.tagName.toLowerCase() === 'span') {
                    processElement(node);
                }
                // Process any span descendants of the added node.
                processNodes(node);
            }
        });
    });
});
observer.observe(document.body, { childList: true, subtree: true });

function processNodes(root = document) {
    root.querySelectorAll('.event-description-cell span').forEach(span => {
        processElement(span);
    });
}

// Process a given element if its text is a pure number.
function processElement(el) {
    // There are other characters in the cell with the raw numbers
    // in case of healing or dots.
    let newText = el.textContent.replace(/(\d+(\.\d+)?)/g, function(match) {
        let num = parseInt(match);
        if (num >= 1000) {
            return abbreviateNumber(num);
        } else {
            return match;
        }
    });
    if (newText !== el.textContent) {
        el.textContent = newText;
    }
}

// Abbreviates numbers (e.g., 2358046 → 2.4M) if they're large enough.
function abbreviateNumber(num) {
    if (num >= 1e9) return (num / 1e9).toFixed(1) + "B";
    if (num >= 1e6) return (num / 1e6).toFixed(1) + "M";
    if (num >= 1e3) return (num / 1e3).toFixed(1) + "K";
    return num.toString();
}