* Saldo

Ergänzt eine Zeile mit den Spaltensummen zur Creditsübersicht

  1. // ==UserScript==
  2. // @name * Saldo
  3. // @namespace bos-ernie.leitstellenspiel.de
  4. // @version 1.2.0
  5. // @license BSD-3-Clause
  6. // @author BOS-Ernie
  7. // @description Ergänzt eine Zeile mit den Spaltensummen zur Creditsübersicht
  8. // @match https://www.leitstellenspiel.de/credits/overview
  9. // @icon https://www.google.com/s2/favicons?sz=64&domain=leitstellenspiel.de
  10. // @run-at document-idle
  11. // @grant none
  12. // @resource https://forum.leitstellenspiel.de/index.php?thread/25282-script-saldo-by-bos-ernie/
  13. // ==/UserScript==
  14.  
  15. (function () {
  16. const table = document.querySelector("table");
  17.  
  18. function reorderColumns() {
  19. const rows = table.rows;
  20.  
  21. for (let i = 0; i < rows.length; i++) {
  22. const row = rows[i];
  23. const cells = row.cells;
  24. const balanceCell = cells[0];
  25. const revenueCell = cells[1];
  26. const expenditureCell = cells[2];
  27. const dateCell = cells[3];
  28.  
  29. row.removeChild(balanceCell);
  30. row.removeChild(revenueCell);
  31. row.removeChild(expenditureCell);
  32. row.removeChild(dateCell);
  33.  
  34. expenditureCell.classList.add("text-right");
  35. revenueCell.classList.add("text-right");
  36. balanceCell.classList.add("text-right");
  37.  
  38. row.appendChild(dateCell);
  39. row.appendChild(expenditureCell);
  40. row.appendChild(revenueCell);
  41. row.appendChild(balanceCell);
  42. }
  43. }
  44.  
  45. function sum(columnIndex) {
  46. const cells = table.querySelectorAll(`tbody tr td:nth-child(${columnIndex})`);
  47. let sum = 0;
  48. cells.forEach(cell => {
  49. sum += parseInt(cell.textContent.replace(/\./g, ""));
  50. });
  51. return sum;
  52. }
  53.  
  54. function addFooter() {
  55. const sumOfRevenue = sum(2);
  56. const sumOfExpenditure = sum(3);
  57. const sumOfBalance = sum(4);
  58.  
  59. const dateCell = document.createElement("td");
  60. dateCell.textContent = "Summe";
  61.  
  62. const revenueCell = document.createElement("td");
  63. revenueCell.textContent = sumOfRevenue.toLocaleString();
  64. revenueCell.classList.add("text-right");
  65.  
  66. const expenditureCell = document.createElement("td");
  67. expenditureCell.textContent = sumOfExpenditure.toLocaleString();
  68. expenditureCell.classList.add("text-right");
  69.  
  70. const balanceCell = document.createElement("td");
  71. balanceCell.classList.add(sumOfBalance < 0 ? "text-danger" : "text-success");
  72. balanceCell.textContent = sumOfBalance.toLocaleString();
  73. balanceCell.classList.add("text-right");
  74.  
  75. const footerRow = document.createElement("tr");
  76. footerRow.style.fontWeight = "bold";
  77. footerRow.appendChild(dateCell);
  78. footerRow.appendChild(revenueCell);
  79. footerRow.appendChild(expenditureCell);
  80. footerRow.appendChild(balanceCell);
  81.  
  82. const footer = document.createElement("tfoot");
  83. footer.appendChild(footerRow);
  84.  
  85. table.appendChild(footer);
  86. }
  87.  
  88. function main() {
  89. table.querySelector("thead tr th").textContent = "Saldo";
  90. table.classList.add("table-hover");
  91.  
  92. reorderColumns();
  93. addFooter();
  94. }
  95.  
  96. main();
  97. })();