-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoins.js
More file actions
147 lines (125 loc) · 4.12 KB
/
coins.js
File metadata and controls
147 lines (125 loc) · 4.12 KB
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
window
.fetch(
'https://data.messari.io/api/v2/assets?limit=300&fields=name,metrics/market_data/price_usd,metrics/market_data/percent_change_usd_last_24_hours'
)
.then((res) => res.json())
.then((messariRes) => messariRes.data)
.then((coins) => {
var coinTable = document.querySelector('.coin-data');
for (let [index, coin] of coins.entries()) {
// Get coin's data
var rank = index + 1;
var name = coins[index]['name'];
var price =
'$' +
parseFloat(coins[index]['metrics']['market_data']['price_usd']).toFixed(
2
);
var change =
parseFloat(
coins[index]['metrics']['market_data'][
'percent_change_usd_last_24_hours'
]
).toFixed(2) + '%';
// Add a new row with the data
var row = coinTable.insertRow();
row.classList.add('coin');
rankCell = row.insertCell();
rankCell.classList.add('center');
rankCell.classList.add('aligned');
rankCell.innerHTML = rank;
nameCell = row.insertCell();
nameCell.classList.add('left');
nameCell.classList.add('aligned');
nameCell.classList.add('theName');
nameCell.innerHTML = name;
changeCell = row.insertCell();
// Handle null change values
if (change === 'NaN%') {
changeCell.innerHTML = '0';
} else {
changeCell.innerHTML = change;
}
changeCell.classList.add('inverted');
// Number color based on positive or negative 24hr change
if (change.includes('-')) {
changeCell.classList.add('negative');
} else {
changeCell.classList.add('positive');
}
priceCell = row.insertCell();
priceCell.innerHTML = price;
starCell = row.insertCell();
starCell.classList.add('center');
starCell.classList.add('aligned');
var starButton = document.createElement('i');
starButton.classList.add('star');
starButton.classList.add('icon');
starButton.classList.add('link');
starCell.appendChild(starButton);
}
})
// chrome api get list of starred
chrome.storage.sync.get(['starred'], function (result) {
savedArray = [result.starred]
savedArrayValues = savedArray[0]
});
var favorites = []
setTimeout(function afterTwoSeconds() {
// chrome api save class to yellow
var allCoins = document.querySelectorAll('.coin')
allCoins.forEach((coin) => {
let allCoinsName = coin.children[1].innerText
// if there is something in savedArrayValues
if (typeof savedArrayValues === 'undefined') {
} else {
if (savedArrayValues.includes(allCoinsName)) {
// if true
star = coin.querySelector('.star');
star.classList.toggle('yellow');
// chrome api save class to yellow
};
}
});
var btnStar = document.querySelectorAll('.star');
btnStar.forEach((item) => {
item.addEventListener('click', (event) => {
item.classList.toggle('yellow');
// chrome storage
var coins = document.querySelectorAll('.coin');
let starred = [];
coins.forEach((coin) => {
coinName = coin.children[1].outerText
star = coin.querySelector('.star');
if (star.classList.contains('yellow')) {
starred.push(coinName)
};
});
// chrome api
chrome.storage.sync.set({ "starred": starred }, function () {
});
});
});
//changed to 500 from 2000 to make the saved stars appear fluid
}, 500);
var isStarredOnly = false;
function showStarred() {
var coins = document.querySelectorAll('.coin');
if (isStarredOnly) {
coins.forEach((coin) => {
coin.style.display = 'table-row';
});
isStarredOnly = false;
} else {
coins.forEach((coin) => {
star = coin.querySelector('.star');
if (star.classList.contains('yellow')) {
} else {
coin.style.display = 'none';
}
});
isStarredOnly = true;
}
}
favorites_on = document.querySelector('#favorites-tab');
favorites_on.addEventListener('click', showStarred, false);