This repository has been archived by the owner on Nov 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mondo.js
109 lines (81 loc) · 2.4 KB
/
mondo.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
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
98
99
100
101
102
103
104
105
106
107
108
109
var balance = "";
var spend_today = "";
var currency = "";
var baseUrl;
var account;
var token;
function authenticate(u, p) {
baseUrl = config.url;
// authenticate
$.post(baseUrl + "/oauth2/token",
{ grant_type: "password",
client_id: config.client_id,
client_secret: config.client_secret,
username: u,
password: p
}).done(function(data) {
token = data.access_token;
document.getElementById('status').textContent = '';
getAccounts();
}).fail(function(data) {
document.getElementById('status').textContent = 'Authentication failed.';
setTimeout(function() {
status.textContent = '';
}, 3000);
});
}
function getAccounts() {
var request = {};
request.url = baseUrl + "/accounts";
request.beforeSend = function (xhr) {xhr.setRequestHeader("Authorization", "Bearer " + token)};
request.type = "GET";
$.ajax(request).done(function(data)
{
account = data.accounts[0].id;
document.getElementById('status').textContent = '';
getBalance();
}).fail(function(data) {
document.getElementById('status').textContent = 'Failed to get account';
});
}
function getBalance() {
var request = {};
request.url = baseUrl + "/balance?account_id=" + account;
request.beforeSend = function (xhr) {xhr.setRequestHeader("Authorization", "Bearer " + token)};
request.type = "GET";
$.ajax(request).done(function(data)
{
balance = data.balance;
currency = data.currency;
spend_today = data.spend_today;
document.getElementById('status').textContent = '';
updateBalance();
updateSpent();
}).fail(function(data) {
document.getElementById('status').textContent = 'Failed to get balance';
});
}
function fetchBalance() {
var b = (balance / 100);
return "£" + b.toFixed(2);
}
function fetchSpent() {
var s = (0 - spend_today / 100);
return "£" + s.toFixed(2);
}
function updateBalance() {
document.getElementById("balance").innerHTML = fetchBalance();
}
function updateSpent() {
document.getElementById("today").innerHTML = fetchSpent();
}
function dec_password(input, key) {var output = CryptoJS.AES.decrypt(input, key);output = output.toString(CryptoJS.enc.Utf8);return output;}
function load_options_and_process() {
chrome.storage.sync.get({
u: 'u',
p: 'p'
}, function(items) {
authenticate(items.u, dec_password(items.p, items.u));
});
}
load_options_and_process();