sidola / Wowhead Comment Number Fix

// ==UserScript==
// @name         Wowhead Comment Number Fix
// @version      1.0.2
// @description  Adds commas to Wowhead spell tooltips
// @match        http://www.wowhead.com/*
// @match        https://www.warcraftlogs.com/*
// @copyright 2017, sidola (https://openuserjs.org/users/sidola)
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    const MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;

    const numberWithCommas = (x) => {
        return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    };

    const nodeObserver = new MutationObserver(mutations => {

        // NOTE: I can't get this fucking thing to stop spamming
        // even though I've tried filtering down attributes.
        mutations.forEach(mutation => {
            if (mutation.attributeName != 'data-visible') return;

            const spans = mutation.target.querySelectorAll('span.q');

            // Doesn't contain an attack tooltip
            if (spans.length === 0) return;

            spans[0].innerHTML = numberWithCommas(spans[0].innerHTML);
        });

    });

    var tooltipObserver = new MutationObserver(mutations => {
        mutations.forEach(mutation => {
            if (!mutation.addedNodes) return;

            for(var node of mutation.addedNodes) {
                if (node.classList[0] == "wowhead-tooltip") {

                    nodeObserver.observe(node, {
                        attributes: true,
                        attributeOldValue: true,
                        attributeFilter: ['data-visible']
                    });

                    break;
                }
            }
        });
    });

    tooltipObserver.observe(document.body, {
        childList: true
    });
})();