forked from csrudy/btcapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
57 lines (45 loc) · 1.82 KB
/
popup.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
fetch('https://api.coindesk.com/v1/bpi/historical/close.json')
.then(response => response.json()) //turned the url data into js object
.then((jsonData) => {
const dayPrices = Object.keys(jsonData.bpi);
let firstDay = Math.round(jsonData.bpi[dayPrices[dayPrices.length-2]] * 100) / 100;
let currentDay = Math.round(jsonData.bpi[dayPrices[dayPrices.length-1]] * 100) / 100;
function pChangeFunc(current, first) {
let difference = first - current;
let change = ((difference / first) * 100).toFixed(2) + '%'
return `${change}`;
}
function vChangeFunc(current, first) {
return Math.round((current - first) * 100) / 100;
}
const vchange = vChangeFunc(currentDay, firstDay);
const pchange = pChangeFunc(firstDay, currentDay);
const valueChange = document.createElement('div');
valueChange.innerHTML = `${vchange}`;
valueChange.id = "valueChanged";
$('#value').append(valueChange);
if (Math.sign(vchange)=== 1) {
valueChange.style.color = 'green';
} else {
valueChange.style.color = 'red';
}
const percentChange = document.createElement('div')
percentChange.innerHTML = `${pchange}`
percentChange.id = "percentChanged"
$('#percent').append(percentChange)
if (Math.sign(pchange)=== 1) {
percentChange.style.color = 'green';
} else {
percentChange.style.color = 'red';
}
})
fetch('https://api.coindesk.com/v1/bpi/currentprice.json')
.then(response => response.json()) //turned the url data into js object
.then((jsonData) => {
const price = Math.round(jsonData.bpi["USD"].rate_float * 100) / 100;
console.log(price);
const currentPrice = document.createElement('div');
currentPrice.innerHTML = `${price}`;
currentPrice.id = "currentPrice";
$('#current').append(currentPrice);
})