Warcraft Logs Number Abbreviator

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

  1. // ==UserScript==
  2. // @name Warcraft Logs Number Abbreviator
  3. // @namespace athei
  4. // @author Alexander Theißen
  5. // @version 1.0.0
  6. // @description Abbreviate big numbers in the event log using K,M,B suffixes.
  7. // @match http://*.warcraftlogs.com/*
  8. // @match https://*.warcraftlogs.com/*
  9. // @grant none
  10. // @license GPL3
  11. // ==/UserScript==
  12.  
  13. // Process nodes that exist at load time of this script.
  14. if (document.readyState === 'loading') {
  15. document.addEventListener('DOMContentLoaded', processNodes);
  16. } else {
  17. processNodes();
  18. }
  19.  
  20. // Process nodes that are added after this script is run.
  21. const observer = new MutationObserver(mutations => {
  22. mutations.forEach(mutation => {
  23. mutation.addedNodes.forEach(node => {
  24. // Only process element nodes.
  25. if (node.nodeType === Node.ELEMENT_NODE) {
  26. // If the added node itself is a span, process it.
  27. if (node.tagName.toLowerCase() === 'span') {
  28. processElement(node);
  29. }
  30. // Process any span descendants of the added node.
  31. processNodes(node);
  32. }
  33. });
  34. });
  35. });
  36. observer.observe(document.body, { childList: true, subtree: true });
  37.  
  38. function processNodes(root = document) {
  39. root.querySelectorAll('.event-description-cell span').forEach(span => {
  40. processElement(span);
  41. });
  42. }
  43.  
  44. // Process a given element if its text is a pure number.
  45. function processElement(el) {
  46. // There are other characters in the cell with the raw numbers
  47. // in case of healing or dots.
  48. let newText = el.textContent.replace(/(\d+(\.\d+)?)/g, function(match) {
  49. let num = parseInt(match);
  50. if (num >= 1000) {
  51. return abbreviateNumber(num);
  52. } else {
  53. return match;
  54. }
  55. });
  56. if (newText !== el.textContent) {
  57. el.textContent = newText;
  58. }
  59. }
  60.  
  61. // Abbreviates numbers (e.g., 2358046 → 2.4M) if they're large enough.
  62. function abbreviateNumber(num) {
  63. if (num >= 1e9) return (num / 1e9).toFixed(1) + "B";
  64. if (num >= 1e6) return (num / 1e6).toFixed(1) + "M";
  65. if (num >= 1e3) return (num / 1e3).toFixed(1) + "K";
  66. return num.toString();
  67. }