-
Notifications
You must be signed in to change notification settings - Fork 62
/
crypto.js
64 lines (53 loc) · 1.62 KB
/
crypto.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
let symbols = [
{ symbol: 'BTC', name: '比特币' },
{ symbol: 'ETH', name: '以太坊' },
{ symbol: 'BNB', name: '币安币' },
{ symbol: 'XRP', name: '瑞波币' },
{ symbol: 'ADA', name: '艾达币' },
{ symbol: 'DOGE', name: '狗狗币' },
{ symbol: 'LTC', name: '莱特币' },
{ symbol: 'SOL', name: '索拉纳' },
{ symbol: 'TRX', name: '波场币' }
];
let notificationTitle = '加密货币汇率';
let notificationContent = [];
for (let i = 0; i < symbols.length; i++) {
let symbol = symbols[i];
let url = `https://api.binance.com/api/v3/ticker/price?symbol=${symbol.symbol}USDT`;
if (typeof $task !== 'undefined') {
// Quantumult X
$task.fetch({ url: url }).then(
(response) => {
handleResponse(response.body, symbol);
},
(reason) => {
console.log(reason.error);
}
);
} else if (typeof $httpClient !== 'undefined') {
$httpClient.get(url, function (error, response, data) {
if (error) {
console.log(error);
} else {
handleResponse(data, symbol);
}
});
}
}
function handleResponse(data, symbol) {
let jsonData = JSON.parse(data);
let price = jsonData.price;
let currencyInfo = symbol.name + symbol.symbol + '💲' + price;
notificationContent.push(currencyInfo);
if (notificationContent.length === symbols.length) {
sendNotification();
}
}
function sendNotification() {
let body = notificationContent.join('\n');
if (typeof $task !== 'undefined') {
$notify(notificationTitle, '', body);
} else if (typeof $httpClient !== 'undefined') {
$notification.post(notificationTitle, '', body);
}
}