-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
133 lines (124 loc) · 3.95 KB
/
background.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
// Extension event listeners are a little different from the patterns you may have seen in DOM or
// Node.js APIs. The below event listener registration can be broken in to 4 distinct parts:
//
// * chrome - the global namespace for Chrome's extension APIs
// * runtime – the namespace of the specific API we want to use
// * onInstalled - the event we want to subscribe to
// * addListener - what we want to do with this event
//
// See https://developer.chrome.com/docs/extensions/reference/events/ for additional details.
chrome.runtime.onInstalled.addListener(function () {
console.log('Price Watcher Extension installed.');
chrome.storage.sync.set({ priceCheckerEnabled: false }, function () {
console.log('Price Checker installed!');
});
chrome.storage.sync.set({ priceGraphEnabled: false }, function () {
console.log('Price Graph installed!');
});
});
chrome.declarativeContent.onPageChanged.removeRules(undefined, function () {
chrome.declarativeContent.onPageChanged.addRules([
{
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostEquals: 'https://web.whatsapp.com/' },
}),
],
actions: [new chrome.declarativeContent.ShowPageAction()],
},
]);
});
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
console.log('Received message from content script.');
if (request.contentScriptQuery === 'getCoin') {
const coinId = request.coinId;
getCoin(coinId)
.then((data) => sendResponse(data))
.catch();
return true;
}
if (request.contentScriptQuery === 'getTrends') {
getTrends()
.then((data) => sendResponse(data))
.catch();
return true;
}
if (request.contentScriptQuery === 'getChart') {
const coinId = request.coinId;
getChart(coinId)
.then((data) => sendResponse(data))
.catch();
return true;
}
});
async function getCoin(coinId) {
try {
const simplePrice = await (
await fetch(
`https://api.coingecko.com/api/v3/simple/price?ids=${coinId}&vs_currencies=usd&include_market_cap=true&include_24hr_change=true`
)
).json();
const coinMarket = await (
await fetch(
`https://api.coingecko.com/api/v3/coins/markets?vs_currency=brl&ids=${coinId}&order=market_cap_desc&per_page=100&page=1&sparkline=false`
)
).json();
const coinInfo = await (
await fetch(
`https://api.coingecko.com/api/v3/coins/${coinId}?localization=false&tickers=false&market_data=false&community_data=false&developer_data=false&sparkline=false`
)
).json();
const data = {
id: coinMarket[0].id,
name: coinMarket[0].name,
symbol: coinMarket[0].symbol,
usd: simplePrice[coinId].usd,
brl: coinMarket[0].current_price,
marketCap: coinMarket[0].market_cap,
high_24h: coinMarket[0].high_24h,
low_24h: coinMarket[0].low_24h,
price_change_percentage_24h: coinMarket[0].price_change_percentage_24h,
homepage: coinInfo.links.homepage[0],
sentiment_up: coinInfo.sentiment_votes_up_percentage,
sentiment_down: coinInfo.sentiment_votes_down_percentage,
};
return data;
} catch (e) {
console.log('Error getting coin. ', e);
}
}
async function getTrends() {
try {
const trends = await (await fetch(`https://api.coingecko.com/api/v3/search/trending`)).json();
let data = [];
trends.coins.forEach((coin, index) => {
data.push({
position: index + 1,
name: coin.item.name,
symbol: coin.item.symbol,
});
});
return data;
} catch (e) {
console.log('Error getting trends. ', e);
}
}
async function getChart(coinId) {
try {
const coinMarket = await (
await fetch(
`https://api.coingecko.com/api/v3/coins/markets?vs_currency=brl&ids=${coinId}&order=market_cap_desc&per_page=100&page=1&sparkline=false`
)
).json();
const chartData = await (
await fetch(`https://api.coingecko.com/api/v3/coins/${coinId}/market_chart?vs_currency=brl&days=15`)
).json();
const data = {
prices: chartData.prices,
name: coinMarket[0].name,
};
return data;
} catch (e) {
console.log('Error getting chart. ', e);
}
}