This repository was archived by the owner on Oct 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbackground.js
More file actions
323 lines (289 loc) · 9.62 KB
/
background.js
File metadata and controls
323 lines (289 loc) · 9.62 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
"use strict";
var iteration = 0;
var intervalId = null;
var mainLoop = function(force) {
console.log("\n#" + iteration);
if (ls.showCantina === 'true')
if (force || iteration % UPDATE_CANTINAS_INTERVAL === 0)
updateCantinas();
if (force || iteration % UPDATE_NEWS_INTERVAL === 0)
updateAffiliationNews('1');
if (ls.showAffiliation2 === 'true')
if (force || iteration % UPDATE_NEWS_INTERVAL === 0)
updateAffiliationNews('2');
// Only if hardware
if (Affiliation.org[ls.affiliationKey1].hardware)
if (force || iteration % UPDATE_AFFILIATION_INTERVAL === 0)
updateAffiliation();
// No reason to count to infinity
if (10000 < iteration)
iteration = 0;
else
iteration++;
};
//
// Status (office status, meetings, and servant for affiliation)
//
var updateAffiliation = function(callback) {
console.log('updateAffiliation');
// Fetch
Affiliation.get(ls.affiliationKey1, function() {
// Run relevant background updates
if (Affiliation.org[ls.affiliationKey1].hardware) {
updateStatusAndMeetings();
updateCoffeeSubscription();
}
// Callback
if (typeof callback === 'function') callback();
});
};
var updateStatusAndMeetings = function(force, callback) {
console.log('updateStatusAndMeetings');
// Get meeting data
var meeting = ls.meetingString;
// Get status data
var strings = JSON.parse(ls.statusStrings);
var statusCode = strings.statusCode;
var statusTitle = strings.statusTitle;
var statusMessage = strings.statusMessage;
// Update the icon and icon hover text if data is new
if (force || ls.backgroundLastStatusCode !== statusCode || ls.backgroundLastStatusMessage !== statusMessage) {
// Save them
ls.backgroundLastStatusCode = statusCode;
ls.backgroundLastStatusMessage = statusMessage;
// Food status
if (Object.keys(Affiliation.foods).indexOf(statusCode) > -1) {
statusTitle = Affiliation.foods[status].title;
Browser.setIcon(Affiliation.foods[statusCode].icon);
}
// Regular status
else {
// Set icon
var errorIcon = Affiliation.org[ls.affiliationKey1].icon;
var statusIcon = Affiliation.org[ls.affiliationKey1].hardware.statusIcons[statusCode];
if (statusCode === 'error' || typeof(statusIcon) === 'undefined') {
Browser.setIcon(errorIcon);
}
else {
Browser.setIcon(statusIcon);
}
}
// Extension title (hovering mouse over icon shows the title text).
// This string is padded with one newline top and bottom, as well as two spaces on the left and right side of all strings.
var today = '\n ### Nå \n ' + statusTitle + ": " + statusMessage + " \n\n ### Resten av dagen \n " + meeting.replace(/\n/g, " \n ") + "\n";
Browser.setTitle(today);
}
if (typeof callback === 'function') callback();
};
//
// Coffee
//
var updateCoffeeSubscription = function(callback) {
console.log('updateCoffeeSubscription');
// Get
var coffeeData = JSON.parse(ls.coffeeData);
// Hope for the best
try {
var date = coffeeData.date;
var pots = coffeeData.pots;
// No coffee yields pots=0 and date=null
if (pots && date) {
// Parse that date
date = new Date(date);
var age = Coffee.minuteDiff(date);
// Check for NaN here
if (!isNaN(pots) && !isNaN(age)) {
var storedPots = Number(ls.coffeePots);
// New pot number?
if (storedPots < pots) {
// Not a meeting? Or DEBUG mode.
if (ls.backgroundLastStatusCode !== 'meeting') {
// Made less than 10 minutes ago?
if (age < 10) {
// And no meme was served within the last 10 minutes?
if ((Date.now() - Number(ls.coffeeMemeTime)) > 600000) {
// Send meme to everyone who has a coffee subscription :D
Coffee.showNotification(pots, age);
ls.coffeeMemeTime = Date.now();
}
else {console.log('Nope to coffee, last one was less than 10 minutes ago')}
}
else {console.log('Nope to coffee, not made less than 10 minutes ago')}
}
else {console.log('Nope to coffee, there is a meeting going on')}
}
// And remember to update localStorage
ls.coffeePots = pots;
}
}
if (typeof callback === 'function') callback();
}
catch (e) {
console.error(e);
}
};
//
// Cantina
//
var updateCantinas = function(callback) {
console.log('updateCantinas');
// Fetch
Cantina.get(ls.cantina1, function(result1) {
Cantina.get(ls.cantina2, function(result2) {
// Save
ls.cantina1Data = JSON.stringify(result1);
ls.cantina2Data = JSON.stringify(result2);
// Callback
if (typeof callback === 'function') callback();
});
});
};
//
// Affiliation news
//
var updateAffiliationNews = function(number, callback) {
console.log('updateAffiliationNews'+number);
// Get affiliation
var affiliationKey = ls['affiliationKey'+number];
var affiliation = Affiliation.org[affiliationKey];
// Get news for this affiliation
if (affiliation) {
News.get(affiliation, function(posts) {
// Error message, log it maybe
if (typeof posts === 'string') {
console.error(posts);
}
// Empty news posts, don't count
else if (posts.length === 0) {
updateUnreadCount(0, 0);
}
// News is here! NEWS IS HERE! FRESH FROM THE PRESS!
else {
saveAndCountNews(posts, number);
updateUnreadCount();
}
if (typeof callback === 'function') callback();
});
}
else {
console.error('Chosen affiliation "' + affiliationKey + '" is not known');
if (typeof callback === 'function') callback();
}
};
var saveAndCountNews = function(items, number) {
var feedItems = 'affiliationNews' + number;
var newsList = 'affiliationNewsList' + number;
var unreadCount = 'affiliationUnreadCount' + number;
var lastNotified = 'affiliationLastNotified' + number;
ls[feedItems] = JSON.stringify(items);
var list = JSON.parse(ls[newsList]);
ls[unreadCount] = News.countNewsAndNotify(items, list, lastNotified);
ls[newsList] = News.refreshNewsList(items);
};
var updateUnreadCount = function(count1, count2) {
// TODO: Tag all news with a "read" boolean, use this for counting and showing which news are unread
var unreadCount = 0;
if (ls.showNotifications1 === 'true') {
unreadCount += Number(ls.affiliationUnreadCount1);
}
if (ls.showNotifications2 === 'true') {
unreadCount += Number(ls.affiliationUnreadCount2);
}
Browser.setBadgeText(String(unreadCount));
};
//
// Prepare Affiliations
// (must run before other affiliation things!)
// (executes itself once)
//
(function prepareAffiliations() {
// Clears values that should start empty
Affiliation.clearAffiliationData();
// Check if both current affiliations still exist, reset if not
var keys = Object.keys(Affiliation.org);
Defaults.resetAffiliationsIfNotExist(ls.affiliationKey1, ls.affiliationKey2, keys);
}());
//
// Load Affiliation Icon
// (executes itself once)
//
var loadAffiliationIcon = (function() {
var load = function() {
var key = ls.affiliationKey1;
// Set badge icon
var icon = Affiliation.org[key].icon;
Browser.setIcon(icon);
// Set badge title
var name = Affiliation.org[key].name;
Browser.setTitle(name + ' Notifier');
}
load(); // Run once
return load; // Return for later use
}());
//
// Browser Setup
// (executes itself once)
//
(function browserSetup() {
Browser.bindCommandHotkeys();
Browser.registerNotificationListeners();
Browser.bindOmniboxToOracle();
}());
//
// Daily Statistics
// (executes itself once)
//
(function dailyStatistics() {
// Send some basic statistics once a day
setInterval( function() {
// App version is interesting
Analytics.trackEvent('appVersion', Browser.getAppVersion() + ' @ ' + Browser.name);
// Ever clicked edit? Interesting
Analytics.trackEvent('everClickedEdit', ls.everClickedEdit);
// Affiliation is also interesting, in contrast to the popup some of these are inactive users
// To find inactive user count, subtract these stats from popup stats
if (ls.showAffiliation2 !== 'true') {
Analytics.trackEvent('singleAffiliation', ls.affiliationKey1);
Analytics.trackEvent('affiliation1', ls.affiliationKey1);
}
else {
Analytics.trackEvent('doubleAffiliation', ls.affiliationKey1 + ' - ' + ls.affiliationKey2);
Analytics.trackEvent('affiliation1', ls.affiliationKey1);
Analytics.trackEvent('affiliation2', ls.affiliationKey2);
}
}, 1000 * 60 * 60 * 24);
}());
//
// Document ready function
//
$(document).ready(function() {
// Enter main loop, keeping everything up-to-date
var stayUpdated = function(now) {
console.info(ONLINE_MESSAGE);
var loopTimeout = (DEBUG ? BACKGROUND_LOOP_DEBUG : BACKGROUND_LOOP);
// Schedule for repetition
intervalId = setInterval( function() {
mainLoop();
}, loopTimeout);
// Run once right now (just wait 2 secs to avoid network-change errors)
var timeout = (now ? 0 : 2000);
setTimeout( function() {
mainLoop(true);
}, timeout);
};
// When offline, mainloop is stopped to decrease power consumption
window.addEventListener('online', stayUpdated);
window.addEventListener('offline', function() {
console.warn(OFFLINE_MESSAGE);
clearInterval(intervalId);
});
if (navigator.onLine) {
// If Online, go ahead and start the stayUpdated-function
stayUpdated(true);
}
else {
// If offline, run mainloop once, it fetches error messages
// Keep in mind: this here is at program startup, we have to get something to display
mainLoop();
}
});