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