-
Notifications
You must be signed in to change notification settings - Fork 0
/
mondo.html
130 lines (101 loc) · 3.13 KB
/
mondo.html
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Mondo Bank Extension</title>
<script src="jquery/jquery-2.1.3.min.js"></script>
<script src="jquery/jquery.tooltipster.min.js"></script>
<link rel="stylesheet" type="text/css" href="mondo.css">
<link rel="stylesheet" type="text/css" href="tooltipster.css">
</head>
<body>
<script>
//var api = "https://staging-api.gmon.io";
var api = "https://production-api.gmon.io";
safari.application.addEventListener("popover", popoverHandler, false);
safari.application.addEventListener( "message", function( e ) {
if( e.name === "getBalance" ) {
console.log("GET BALANCE CALLED");
getBalance();
}
}, false );
var balance = "";
var spend_today = "";
var currency = "";
function authenticate()
{
// Authenticate
$.post( api + "/oauth2/token",
{ grant_type: "password",
client_id: safari.extension.settings.getItem('oauth_id'),
client_secret: safari.extension.settings.getItem('client_secret'),
username: safari.extension.secureSettings.getItem('username'),
password: safari.extension.secureSettings.getItem('password')
}).done(function(data) {
safari.extension.secureSettings.access_token = data.access_token;
getAccounts();
getBalance();
return true;
}).fail(function(data) {
alert("derp");
return false;
});
return false;
}
function getAccounts()
{
var request = {};
request.url = api + "/accounts";
request.beforeSend = function (xhr) {xhr.setRequestHeader("Authorization", "Bearer " + safari.extension.secureSettings.access_token)};
request.type = "GET";
$.ajax(request).done(function(data)
{
safari.extension.secureSettings.account_id = data.accounts[0].id;
console.log("new acc : " + safari.extension.secureSettings.account_id);
}).fail(function(data)
{
alert("failed to get accounts")
});
}
function getBalance()
{
var request = {};
request.url = api + "/balance?account_id=" + safari.extension.secureSettings.account_id;
request.beforeSend = function (xhr) {xhr.setRequestHeader("Authorization", "Bearer " + safari.extension.secureSettings.access_token)};
request.type = "GET";
$.ajax(request).done(function(data)
{
balance = data.balance;
currency = data.currency;
spend_today = data.spend_today;
//e.target.page.dispatchMessage( "getBalance", fetchBalance());
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage("getBalance", fetchBalance());
}).fail(function(data)
{
alert("failed to get balance")
});
}
authenticate();
function fetchBalance()
{
var b = (balance / 100);
return "£" + b.toFixed(2);
}
function fetchSpent()
{
var s = (0 - spend_today / 100);
return "£" + s.toFixed(2);
}
function popoverHandler(event)
{
if (event.target.identifier === "info.popover")
{
getBalance();
popover = event.target;
popover.contentWindow.setBalance(fetchBalance());
popover.contentWindow.setSpent(fetchSpent());
}
}
</script>
</body>
</html>