-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
373 lines (330 loc) · 11.8 KB
/
script.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// Content Sections
let cc = {
SIGNIN_CONTENT: document.getElementById("signinContent"),
EXTENSION_CONTENT: document.getElementById("extensionContent"),
FOOTER: document.getElementsByClassName("footer")[0],
// Info for using app
CLIENT_ID: "ce5a268500cd300dd697e9419ba4800d1236477c1b77f1b4c043dee5266dfa29",
CLIENT_SECRET: "55404ab2ea23a6737b68100211c774e86087ee6fc79ab285533a62731be77eb8",
REDIRECT_URI: "https://murphyme.co.uk/success",
ACCESS_TOKEN: "ACCESS_TOKEN",
accessToken: "",
REFRESH_TOKEN: "REFRESH_TOKEN",
refreshToken: "",
authorization_code: "authorization_code",
tempCode: "",
SELECTED: ["BCH", "BTC", "ETC", "ETH", "LTC", "ZRX", ],
isLoggedin: false,
t: "",
init: () => {
cc.toggleContent();
cc.toggleAbout();
cc.toggleCurrency();
},
toggleContent: () => {
console.log("Not Logged in");
cc.SIGNIN_CONTENT.classList.add("visible");
cc.EXTENSION_CONTENT.classList.remove("visible");
cc.FOOTER.classList.add("footer--blue");
let footerLink = document.getElementsByClassName("menu__link");
footerLink[0].classList.add("footer--blue");
cc.getAccounts();
// Add href to button
const signinButton = document.getElementById("signinButton");
signinButton.setAttribute(
"href",
"https://www.coinbase.com/oauth/authorize?client_id=" + cc.CLIENT_ID +
"&redirect_uri=" + encodeURIComponent(cc.REDIRECT_URI) +
"&response_type=code&scope=wallet%3Aaccounts%3Aread&account=all"
);
chrome.tabs.query({}, function(tabs) {
for (let i = 0; i < tabs.length; i++) {
if (tabs[i].url.includes(cc.REDIRECT_URI)) {
cc.getParams();
}
}
});
},
toggleAbout: () => {
const aboutButton = document.getElementById("aboutButton");
const aboutModal = document.getElementsByClassName("about__modal")[0];
// toggle about modal screen
aboutButton.addEventListener("click", function(e) {
aboutModal.classList.toggle("modal__toggle");
});
},
login: () => {
if (cc.isLoggedIn) {
console.log("Not Logged in");
cc.SIGNIN_CONTENT.classList.add("visible");
cc.EXTENSION_CONTENT.classList.remove("visible");
cc.FOOTER.classList.add("footer--blue");
let footerLink = document.getElementsByClassName("menu__link");
footerLink[0].classList.remove("footer--blue");
cc.getAccounts();
// cc.getPrices();
} else {
console.log("Logged in");
// exchange temp code for auth/refresh codes
cc.tempCode = localStorage.getItem(cc.authorization_code);
fetch("https://api.coinbase.com/oauth/token" +
"?grant_type=" + cc.authorization_code +
"&code=" + cc.tempCode +
"&client_id=" + cc.CLIENT_ID +
"&client_secret=" + cc.CLIENT_SECRET +
"&redirect_uri=" + cc.REDIRECT_URI, {
method: "POST",
headers: {
"CB-Version": "2018-09-26"
}
})
.then(response => response.json())
.then(obj => {
cc.t = obj.expires_in;
// Store ACCESS_TOKEN and REFRESH_TOKEN
localStorage.setItem(cc.REFRESH_TOKEN, obj.refresh_token);
localStorage.setItem(cc.ACCESS_TOKEN, obj.access_token);
cc.refreshToken = localStorage.getItem(cc.REFRESH_TOKEN);
cc.accessToken = localStorage.getItem(cc.ACCESS_TOKEN);
cc.SIGNIN_CONTENT.classList.remove("visible");
cc.EXTENSION_CONTENT.classList.add("visible");
cc.FOOTER.classList.remove("footer--blue");
let footerLink = document.getElementsByClassName("menu__link");
footerLink[0].classList.remove("footer--blue");
cc.getAccounts();
// cc.getPrices();
// Set timeout of time until tokens expire
function refresh() {
setInterval(() => {
fetch("https://api.coinbase.com/oauth/token" +
"?grant_type=refresh_token" +
"&refresh_token=" + cc.refreshToken +
"&client_id=" + cc.CLIENT_ID +
"&client_secret=" + cc.CLIENT_SECRET, {
method: "POST",
})
.then(response => response.json())
.then(obj => {
// Store ACCESS_TOKEN and REFRESH_TOKEN
localStorage.setItem(cc.REFRESH_TOKEN, obj.refresh_token);
localStorage.setItem(cc.ACCESS_TOKEN, obj.access_token);
cc.refreshToken = localStorage.getItem(cc.REFRESH_TOKEN);
cc.accessToken = localStorage.getItem(cc.ACCESS_TOKEN);
// **** Add classes
cc.SIGNIN_CONTENT.style.display = "none";
cc.EXTENSION_CONTENT.style.display = "block";
// cc.getAccounts();
console.log("Let's refresh");
}); // END then exchange tokens
}, cc.t); // END timeout
isLoggedIn = true;
}
refresh();
}); // END then handle obj
} // END If not logged in
},
getParams: () => {
//Check urls
chrome.tabs.query({}, function(tabs) {
for (let i = 0; i < tabs.length; i++) {
if (tabs[i].url.includes(cc.REDIRECT_URI)) {
// Split url
let splitUrl = tabs[i].url.split("?");
let params = splitUrl[1];
// Split params
let splitParams = params.split("=");
let value = params.split("=")[1];
// Store authorization_code
localStorage.setItem(cc.authorization_code, value);
// tempCode = localStorage.getItem(cc.authorization_code);
// stay logged in fn
cc.login();
}
}
});
}, // END getParams fn
// get abreviated name of asset
getAbrName: (abr) => {
switch (abr) {
case "BCH":
return abr;
case "ZRX":
return abr;
case "ETC":
return abr;
case "LTC":
return abr;
break;
case "ETH":
return abr;
break;
case "BTC":
return abr;
break;
default:
return "TBC";
break;
}
},
getAccounts: () => {
console.log("getAccounts");
// Other header info needed?? *******************
fetch('https://api.coinbase.com/v2/accounts', {
headers: {
"Authorization": "Bearer " + cc.accessToken,
},
})
.then(response => response.json())
.then(function(response) {
// response.data returns array of numbers, each is an account
let data = response.data;
console.log(response.data);
let dataSelected = [];
// create object (objects are not called crypto in dataSelected)
for (let j = 0; j < data.length; j++) {
let crypto = {
image: `./node_modules/cryptocurrency-icons/32/color/${getIcon(data[j].currency.code)}`,
name: data[j].name,
amount: `${data[j].balance.amount} ${cc.getAbrName(data[j].currency.code)}`,
code: cc.getAbrName(data[j].currency.code),
}
// push objects to list
dataSelected.push(crypto);
}
for (let i = 0; i < cc.SELECTED.length; i++) {
for (let j = 0; j < dataSelected.length; j++) {
if (dataSelected[j].code.indexOf(cc.SELECTED[i]) > -1) {
// NEEDS Argument *********
cc.buildAccountsTable(dataSelected[j]);
}
}
}
}); // END thenable content
// get icon relevant to asset
function getIcon(abr) {
switch (abr) {
case "BCH":
return "bch.png";
case "ZRX":
return "zrx.png";
case "LTC":
return "ltc.png";
case "ETH":
return "eth.png";
case "ETC":
return "etc.png";
case "BTC":
return "btc.png";
case "USDC":
return "usdc.png";
default:
return "bat.png";
break;
}
}
},
getPrices: () => {
console.log("getPrices");
let outputs = document.getElementsByClassName("table__cell--output");
let names = document.getElementsByClassName("table__cell--assetname");
fetch(`https://api.coinbase.com/v2/exchange-rates?currency=${cc.getSymbol(cc.currencySymbol)}`)
.then(response => response.json())
.then(response => {
// Show rates of selected currency
let rates = response.data.rates;
// Keys from rates
const keys = Object.keys(rates);
// Values from rates
const vals = Object.values(rates);
// Array of cryptos displayed in rates table
const assets = ["BCH", "BTC", "ETC", "ETH", "LTC"];
// New array
let list = [];
// compare items in both arrays and build a new array
keys.forEach((key) => assets.forEach((asset, index) => {
if (key === asset) {
let crypto = {
name: key,
amount: vals[index],
}
list.push(crypto);
names[index].innerHTML = crypto.name;
outputs[index].innerHTML = crypto.amount;
} // END compare arrays IF
})); // END compare arrays forEach
}); // END then clause
cc.calcPricesProduct();
},
//
buildAccountsTable: (account) => {
// append to accounts table, display 3 accounts and more on btn toggle
let accountsTable = document.getElementsByClassName("accounts")[0];
// create tr, append to table
let row = document.createElement("tr");
if (accountsTable.children || accountsTable.children === 0) {
row.innerHTML =
`<td class="table__cell--imagehldr">
<img class="cell--image" src="${account.image}" /></td>
<td class="table__cell--name">${account.name}</td>
<td class="table__cell--amount">${account.name}<br><span class="table__cell--value">${account.amount}</span></td>`;
accountsTable.appendChild(row);
} else {
row.innerHTML += "";
}
},
// Remake requests with GBP,USD, EUR in params,
// append to prices and account vals
toggleCurrency: () => {
console.log("toggleCurrency");
// Get currencySymbol from radio buttons
let currency = document.getElementsByClassName("currency")[0];
// Traversing may be better?? ***********************************
let currencySymbolToggle = document.querySelector(".currency__toggle input:checked~.currency__toggle--symbol");
let currencySymbol = currencySymbolToggle.innerHTML;
// Add symbol to amount ***********************************
for (let i = 0; i < outputs.length; i++) {
outputs[i].innerHTML = currencySymbol.concat(outputs[i].innerHTML);
}
// Change symbols on page when radio button selected changes
currency.addEventListener("change", function(e) {
// input symbol
currencySymbol = e.path[1].children[1].innerHTML;
console.log(currencySymbol);
for (i = 0; i < outputs.length; i++) {
// remove existing symbol from val first, then add new one
outputs[i].innerHTML = currencySymbol.concat(outputs[i].innerHTML);
}
});
},
// get selected symbol from radio buttons and return code
getSymbol: (sym) => {
switch (sym) {
case "$":
return "USD";
break;
case "£":
return "GBP";
break;
case "€":
return "EUR";
break;
default:
return "USD";
break;
}
},
// x * y = output
calc: (crypto, outputClass) => {
let output = document.getElementsByClassName(output);
// If value in output is not a number, display currency value of 1 coin
if (Number.isNaN(parseFloat(target.value))) {
product.innerHTML = crypto.amount;
} else {
// Convert value to string
let outputVal = parseFloat(target.value * crypto.amount).toString();
// return only the first 5 characters
product.innerHTML = outputVal.substring(0, 5);
}
},
};
cc.init();