-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbackground.js
153 lines (127 loc) · 4.12 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
var DELAY = 1;
{
chrome.runtime.onInstalled.addListener(function(tab) {
console.log("Installed");
});
chrome.action.onClicked.addListener(function(activeTab) {
var newURL = "https://app.osmosis.zone/pools";
chrome.tabs.create({
url: newURL
});
});
// initial setup:
chrome.action.setBadgeText({
text: "..."
});
chrome.action.setBadgeBackgroundColor({
color: "#8E00BA"
});
// Timer setup
// TODO: make timer syncronize with the real countdown...
chrome.alarms.create("countdown", {
delayInMinutes: DELAY,
periodInMinutes: DELAY
});
chrome.alarms.onAlarm.addListener((alarm) => {
// get epoch and defer it to promises...
chrome.storage.local.get("epoch_end_unix", gotEpochFromStorage_callback);
});
function gotEpochFromStorage_callback(result) {
let epoch_end_unix = result.epoch_end_unix;
// if it is in storage, then proceed to handle it with updateTimeDisplay...
if (epoch_end_unix) {
updateCountdownDisplay(epoch_end_unix);
} else { // if not in local storage, get it from API...
updateEpochFromAPI();
}
}
function updateCountdownDisplay(epoch_end_unix) {
var now_unix = Date.now();
var totSecs = Math.floor((epoch_end_unix - now_unix) / 1000)
var strTime = new Date(totSecs * 1000).toISOString();
var prettyPrint = "";
if (totSecs > 3600) {
strTime = strTime.substr(11, 5);
prettyPrint = strTime.substr(0, 2) + " hours, " + strTime.substr(3, 2) + " minutes"; //,"+strTime.substr(6, 2)+" seconds.";
} else {
strTime = strTime.substr(14, 2) + "m"; //strTime.substr(14, 5);
prettyPrint = strTime + " minutes.";
}
if (totSecs > 0) {
// refresh badge text
chrome.action.setBadgeText({
text: strTime
});
// refresh tooltip text
chrome.action.setTitle({
title: "Osmosis Companion\n\nNext $OSMO rewards in:\n" + prettyPrint + "\n\n"
});
} else {
chrome.action.setBadgeText({
text: "dist..."
});
// only call API again after ~20 minutes, as it seems to be delayed during rewards...
if (totSecs > -600) {
chrome.action.setTitle({
title: "Osmosis Companion\n\nClaim your rewards now!\n\n\nGetting next epoch from API in " + Math.floor((600 + totSecs) / 60) + " minutes...\n\n"
});
} else {
rewardDistribution();
}
}
}
function rewardDistribution() {
// if epoch is met, call API and save to localstorage.
console.log("rewards distributed! Getting next epoch from API endpoint...");
updateEpochFromAPI();
}
let badgeText = "load...";
chrome.action.setBadgeText({
text: badgeText
});
function updateEpochFromAPI() {
fetchEpochs()
.then((res) => res.json())
.then((data) => {
let new_epoch_end_unix = calculate_epoch_end_unix(data.epochs);
// set storage:
chrome.storage.local.set({
"epoch_end_unix": new_epoch_end_unix
});
// update the badge
updateCountdownDisplay(new_epoch_end_unix);
});
}
var fetchEpochs = function() {
return fetch("https://lcd-osmosis.keplr.app/osmosis/epochs/v1beta1/epochs", {
"headers": {
"accept": "application/json, text/plain, */*",
"accept-language": "en-US,en;q=0.9",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "cross-site",
"sec-gpc": "1"
},
"referrer": "https://app.osmosis.zone/",
"referrerPolicy": "strict-origin-when-cross-origin",
"body": null,
"method": "GET",
"mode": "cors",
"credentials": "omit"
});
};
var calculate_epoch_end_unix = function(epochs) {
// time of epoch:
var epoch_start_unix = null;
var epoch_end_unix = null;
var epoch_duration = "86400"; // 1 day's seconds
// calculate end time of epoch
epochs.forEach((epoch, i) => {
if (epoch.identifier == "day") {
epoch_start_unix = Date.parse(epoch.current_epoch_start_time);
epoch_end_unix = epoch_start_unix + parseInt(epoch_duration * 1000);
}
});
return epoch_end_unix;
}
}