forked from Goltred/tornscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtornFactionBalances.user.js
66 lines (55 loc) · 2.2 KB
/
tornFactionBalances.user.js
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
// ==UserScript==
// @name Detailed Faction Balances
// @namespace Goltred.Faction
// @version 1.0.1
// @description Add extra detail to faction money to know true faction balances and owed money
// @author Goltred
// @updateURL https://raw.githubusercontent.com/Goltred/tornscripts/master/tornFactionBalances.user.js
// @downloadURL https://raw.githubusercontent.com/Goltred/tornscripts/master/tornFactionBalances.user.js
// @match https://www.torn.com/factions.php?step=your*
// ==/UserScript==
$(document).ajaxComplete((evt, xhr, settings) => {
if (settings.type === 'POST' && settings.data === 'step=getMoneyDepositors') {
// Get depositors
const { list } = JSON.parse(xhr.responseText);
let userMoney = 0;
let owedMoney = 0;
list.forEach((user) => {
const { balance } = user;
if (balance >= 0) {
userMoney += balance;
} else {
owedMoney += balance;
}
});
addFactionMoney(userMoney, owedMoney);
}
});
function addFactionMoney(userMoney, owed) {
// Get total faction money
const moneySpan = $('#money > div.give-block > div.info.no-divider > span > span');
const total = moneySpan.attr('data-faction-money');
const factionMoney = total - userMoney;
// Check if elements are already there:
let factMoneySpan = $('#tcfb-faction');
let owedMoneySpan = $('#tcfb-owed');
if (factMoneySpan.length === 0 || owedMoneySpan.length === 0) {
// Get the parent div
const div = moneySpan.closest('div.info.no-divider');
// Create the new element
const fMoneyDiv = $(`
<div class="info no-divider" style="padding-left: 3em;">
<p><strong>Faction Money:</strong> <span id="tcfb-faction"></span></p>
</div>`);
const owedMoneyDiv = $(`
<div class="info no-divider" style="padding-left: 3em;">
<p><strong>Owed Money:</strong> <span id="tcfb-owed"></span></p>
</div>`);
div.after(fMoneyDiv);
fMoneyDiv.after(owedMoneyDiv);
factMoneySpan = $('#tcfb-faction');
owedMoneySpan = $('#tcfb-owed');
}
factMoneySpan.text(`$${factionMoney.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}`);
owedMoneySpan.text(`$${owed.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}`);
}