-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
144 lines (127 loc) · 4.09 KB
/
background.js
File metadata and controls
144 lines (127 loc) · 4.09 KB
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
const oldUrl = "https://ural-srv.herokuapp.com/info";
const oldUrl2 = "https://next-ural-crawler.vercel.app/api/info";
const url = "https://next-ural-crawler.vercel.app/api/scrape";
const logs = true; // Logging disabled by default
const default_options = {
req_every: 5, // Minutes
send_notif: true,
is_updating: true,
};
let timeout;
let isRecentlySent = false;
logs && console.log("Initialising at ", new Date().toTimeString());
fetchData();
initScript();
function initScript() {
logs && console.log("initScript at", new Date().toTimeString());
timeout !== undefined ? clearTimeout(timeout) : null;
chrome.storage.sync.get("ext_options", function (data) {
!data.ext_options ? chrome.storage.sync.set({ ext_options: default_options }) : null;
logs && console.log("Options: ", data.ext_options ? data.ext_options : default_options);
if (data.ext_options && !data.ext_options.is_updating) return;
timeout = setTimeout(
() => {
logs && console.log("Fetching...");
fetchData();
initScript();
},
!data.ext_options ? default_options.req_every * 60000 : data.ext_options.req_every * 60000
);
});
}
function fetchData(cb) {
fetch(url)
.then((response) => {
if (response.ok) {
return response.json();
} else {
console.log(response.status);
console.log(response.statusText);
//throw Error(response.statusText);
}
})
.then((data) => {
logs && console.log(data);
saveData(data);
if (cb) chrome.runtime.sendMessage({ message: "callBackInitLoading" });
})
.catch((err) => console.log(err));
}
function saveData(data) {
logs && console.log("Saving received data");
// Checks if map was changed and sends a notification
checkIsMapChanged(data.isMapChanged, data.map);
// Saves all the data in the storage
chrome.storage.sync.set({ player_num: data.player_num });
chrome.storage.sync.set({ player_total: data.player_total });
chrome.storage.sync.set({ map: data.map });
chrome.storage.sync.set({ date: data.date });
chrome.storage.sync.set({ players: data.players });
chrome.storage.sync.set({ map_date: data.map_date });
chrome.storage.sync.set({ isMapChanged: data.isMapChanged });
chrome.storage.sync.set({ settings: data.settings });
}
function checkIsMapChanged(isChanged, mapName) {
chrome.storage.sync.get("ext_options", function (data) {
if (isChanged && data.ext_options.send_notif && !isRecentlySent) {
chrome.notifications.create(
{
type: "basic",
iconUrl: "./images/favicon-128.png",
title: "UralServer66 - New Map!",
message: `The map has changed to: ${mapName}`,
// contextMessage: 'contextMessage'
},
() => logs && console.log("Notification was sent")
);
// Set the badge after Sending Notification
// The badge will be reset after clicking on an extension Icon
setBadge();
// If notification was sent then set the flag and reset it after 5 minutes
isRecentlySent = true;
setTimeout(() => {
isRecentlySent = false;
}, 5 * 60000);
}
});
}
function setBadge() {
chrome.action.setBadgeBackgroundColor(
{
color: "#F00",
},
() => logs && console.log("set Badge color")
);
chrome.action.setBadgeText(
{
text: "1",
},
() => logs && console.log("Badge was set")
);
}
function removeBadge() {
chrome.action.setBadgeText(
{
text: "",
},
() => logs && console.log("Badge was disabled")
);
}
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
switch (request.message) {
case "removeBadge":
removeBadge();
sendResponse({ message: "Background.js removed Badge" });
break;
case "fetchData":
fetchData(() => {});
sendResponse({ message: "Background.js calling fetchData with initLoading callback" });
break;
case "initScript":
initScript();
sendResponse({ message: "Background.js calling initScript" });
break;
case "getLogsVariable":
sendResponse({ logs: logs });
}
});