-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontent.js
106 lines (92 loc) · 3.45 KB
/
content.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
/*jshint esversion: 6 */
// Asynchronous HTTP Requestor
var HttpClient = function() {
this.get = function(aUrl, aCallback) {
var anHttpRequest = new XMLHttpRequest();
anHttpRequest.onreadystatechange = function() {
if (anHttpRequest.readyState == 4 && anHttpRequest.status == 200)
aCallback(anHttpRequest.responseText);
};
anHttpRequest.open( 'GET', aUrl, true );
anHttpRequest.send( null );
};
};
// Key of currency symbols
function getCurrency(currency) {
const symbols = {
USD: '$',
EUR: '€',
GBP: '£',
INR: '₹',
AUD: 'A$',
CAD: 'C$',
JPY: 'J¥',
CNY: 'C¥'
};
return symbols[currency];
}
// Collection of website information by class
var names = document.getElementsByClassName('coin ng-binding');
var amounts = document.getElementsByClassName('total f-right ng-binding');
var btcs = document.getElementsByClassName('equalValue f-right ng-binding');
// Global variables
var namesIndex = [];
var index;
var currency = 'USD';
// Links formatting constants
var prefix0 = `<br><button class="btn btn-success" onclick=" window.open('https://www.cryptocompare.com/coins/`;
var suffix0 = `/overview/`;
var suffix1 = `','_blank')">`;
var spacing = ` (`;
var suffix2 = `)</button>`;
// Test if prices have already been added
if (amounts[1].innerHTML.includes('<button class="btn btn-success"')) {
for (var i = 1; i < amounts.length; i++) {
namesIndex.push(names[i].innerHTML);
if (parseFloat(btcs[indexFinder(names[i].innerHTML) + 1].innerHTML) > 0) {
// Removal of previous prices and formatting
amounts[i].innerHTML = amounts[i].innerHTML.substring(0, amounts[i].innerHTML.indexOf('-'));
amounts[i].innerHTML = amounts[i].innerHTML.replace('<br>', '');
}
}
}
chrome.storage.sync.get('currency', (data) => {
// Asynchronous HTTP request
var client = new HttpClient();
client.get('https://min-api.cryptocompare.com/data/price?fsym=BTC&tsyms=' + data.currency, function(response) {
currency = data.currency;
// Synchronous spawner of asynchronous requests based off found wallets
for (var i = 1, l = amounts.length; i < l; i++) {
var coinValue = '';
namesIndex.push(names[i].innerHTML);
if (parseFloat(btcs[indexFinder(names[i].innerHTML) + 1].innerHTML) > 0) {
parseP(names[i].innerHTML, response);
}
}
});
});
// Parsing of HTTP request
function parseP(coinName, coinValue) {
coinValue = coinValue.substr(7);
coinValue = coinValue.substr(0, coinValue.length - 1);
index = indexFinder(coinName);
var output = parseFloat(coinValue) * parseFloat(btcs[index + 1].innerHTML);
var outputEach = output.toFixed(2) / amounts[index + 1].innerHTML.replace(/<(?:.|\n)*?>/gm, '');
output = getCurrency(currency) + output.toFixed(2);
// Gets rid of decimal places if a single coin is worth over 100
if (outputEach > 100) {
outputEach = getCurrency(currency) + outputEach.toFixed(0);
} else {
outputEach = getCurrency(currency) + outputEach.toFixed(2);
}
// Dropdown for links to different resources when hovering over value
// Final posting of data
amounts[index + 1].innerHTML += prefix0 + coinName.replace(/<(?:.|\n)*?>/gm, '').toLowerCase() + suffix0 + currency + suffix1 + output + spacing + outputEach + suffix2;
}
// Indexing agent for matching asynchronous replies to page
function indexFinder(coinName) {
function finding(element) {
return element === coinName;
}
return namesIndex.findIndex(finding);
}