-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
34 lines (28 loc) · 999 Bytes
/
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
let url = "https://gpoly.blockscan.com/gasapi.ashx?apikey=key&method=gasoracle";
// Fetch gas prices and updates badge
function fetchPrices() {
fetch(url)
.then((response) => response.json())
.then((data) => Number(data.result.ProposeGasPrice).toFixed(0))
.then((value) => chrome.browserAction.setBadgeText({ text: value }));
}
// Creates alarm that fires every minute
chrome.alarms.create("refresh", { periodInMinutes: 1 });
// // Fetches gas prices on alarm trigger
// chrome.alarms.onAlarm.addListener(fetchPrices);
// Toggle watcher
let isWatching = false;
function toggleWatcher() {
if (isWatching) {
chrome.alarms.onAlarm.removeListener(fetchPrices);
chrome.browserAction.setBadgeText({ text: "" });
isWatching = false;
} else {
chrome.alarms.onAlarm.addListener(fetchPrices);
fetchPrices();
isWatching = true;
}
}
chrome.contextMenus.create({ onclick: toggleWatcher, title: "Toggle On/Off" });
// // Calls on browser launch
// fetchPrices();