-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared.js
147 lines (135 loc) · 3.59 KB
/
shared.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
"use strict";
chrome.runtime.onInstalled.addListener(onInit);
chrome.runtime.onStartup.addListener(onStartup);
var AJAX = {
get: function(url) {
return new Promise((resolve, reject) => {
const req = new XMLHttpRequest();
req.onreadystatechange = (e) => {
if (req.readyState === 4) {
req.status === 200 ? resolve(req.response) : reject(req.status);
}
};
req.ontimeout = () => reject('timeout');
req.open('GET', url, true);
req.send();
});
}
}
var Storage = {
get: function(storedItems) {
return new Promise((resolve, reject) => {
chrome.storage.sync.get(storedItems, function(items) {
resolve(items);
});
});
},
set: function(itemsToStore) {
chrome.storage.sync.set(itemsToStore);
},
clear: function() {
chrome.storage.sync.clear();
}
}
function setTitle() {
chrome.browserAction.setTitle({
"title": ["1 ", CryptoCurrency.selected.symbol, "= ", CryptoCurrency.price.toFixed(6), " ", CryptoCurrency.conversion].join("")
});
}
function setBadge() {
chrome.browserAction.setBadgeText({
text: CryptoCurrency.price.toFixed(2)
});
chrome.browserAction.setIcon({
path: '32/icon/' + CryptoCurrency.selected.symbol.toLowerCase() + ".png"
})
}
var CryptoCurrency = {
conversion: "CAD",
selected: {
id: 1958
},
refreshRate: "5",
price: 0,
setSettings: async function() {
const items = await Storage.get(['conversion', 'selected', 'refreshRate']);
if (Object.keys(items).length > 0) {
this.conversion = items.conversion;
this.selected = items.selected;
this.refreshRate = items.refreshRate;
}
},
setPrice: async function() {
var parsePrice = function(jsonData) {
return parseFloat(jsonData.quotes[this.conversion.toUpperCase()].price);
}
try {
const response = await AJAX.get(["https://api.coinmarketcap.com/v2/ticker/", this.selected.id, "/?convert=", this.conversion].join(""));
const data = JSON.parse(response).data;
this.selected.symbol = data.symbol;
this.selected.name = data.name;
if (parsePrice.call(this, data) >= this.price) {
chrome.browserAction.setBadgeBackgroundColor({
color: '#2eb82e'
});
} else {
chrome.browserAction.setBadgeBackgroundColor({
color: '#ff0000'
});
}
this.price = parsePrice.call(this, data); // Ensure the Cryptocurrency context is used
setTimeout(() => chrome.browserAction.setBadgeBackgroundColor({ color: '#808080' }), 3000);
} catch (e) {
console.error(e);
}
}
}
function prepareBadge() {
chrome.browserAction.setBadgeBackgroundColor({
color: '#808080'
});
chrome.browserAction.setBadgeText({
text: '...'
})
}
function onInit() {
prepareBadge();
}
function onStartup() {
CryptoCurrency.setSettings();
}
async function loadTicker() {
await CryptoCurrency.setSettings();
await CryptoCurrency.setPrice();
setTitle();
setBadge();
}
var setMessageListener = function() {
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log('Message Sent');
if (request && request.hasOwnProperty('type')) {
switch (request.type) {
case "currency":
CryptoCurrency.selected = request.data;
break;
case "fiat":
CryptoCurrency.conversion = request.data;
break;
case "refresh":
CryptoCurrency.refreshRate = request.data;
chrome.alarms.create("tickerAlarm", {
when: Date.now(),
periodInMinutes: parseInt(CryptoCurrency.refreshRate)
});
break;
}
Storage.set({
'conversion': CryptoCurrency.conversion,
'selected': CryptoCurrency.selected,
'refreshRate': CryptoCurrency.refreshRate
});
loadTicker();
sendResponse("refreshed");
}
});
}