-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
581 lines (542 loc) · 18.6 KB
/
Copy pathbackground.js
File metadata and controls
581 lines (542 loc) · 18.6 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
import { UserException } from './utils.js';
import icons from './icons.js';
const DEBUG = true;
function debug(msg) {
if (DEBUG) {
console.log(`[${new Date().toTimeString()}] ${msg}`);
}
}
let stateCache = null;
const defaultState = {
"inFocus": false,
"focusStartTimestamp": 0,
"focusStopTimestamp": 0,
"idleStartTimestamp": 0,
"activeStartTimestamp": 0,
"nextAlarmTimestamp": 0,
"nextHistoryId": 0,
};
async function getState() {
if (stateCache === null) {
const { state: loadedState } = await chrome.storage.local.get(['state']);
stateCache = { ...defaultState, ...loadedState };
}
return stateCache;
}
function writeState(state = stateCache) {
const result = chrome.storage.local.set({ state });
notifyStateChanged(state); // fire and forget
return result;
}
const stateChangeSubscribers = [];
function notifyStateChanged(state = stateCache) {
stateChangeSubscribers.forEach((subscriber) => subscriber(state));
chrome.runtime.sendMessage({
event: 'state_changed',
state: state,
});
}
let historyCache = null;
async function getHistory() {
if (historyCache === null) {
const storageKeys = await chrome.storage.local.getKeys();
if (!storageKeys.includes('history')) {
historyCache = [];
return historyCache;
}
const { history } = await chrome.storage.local.get(['history']);
historyCache = history;
}
return historyCache;
}
async function writeHistory() {
if (historyCache === null) {
throw new Error('historyCache is not loaded.');
}
const result = await chrome.storage.local.set({ history: historyCache });
notifyHistoryChanged(); // fire and forget
return result;
}
function notifyHistoryChanged() {
return chrome.runtime.sendMessage({
event: 'history_changed',
});
}
let optionsCache = null;
const defaultOptions = {
"focusGoalMinutes": 25,
"snoozeMinutes": 10,
"idleDetectionSeconds": 120,
"idleDismissalSeconds": 30,
"spilloverHours": 4,
"showBadgeText": true,
"showNotifications": true,
};
async function getOptions() {
if (optionsCache === null) {
const { options: loadedOptions } = await chrome.storage.local.get(['options']);
optionsCache = { ...defaultOptions, ...loadedOptions };
}
return optionsCache;
}
function vetOptions(options) {
if (!Number.isInteger(options.focusGoalMinutes)) {
throw new UserException(`Focus goal minutes should be an integer, is ${options.focusGoalMinutes}`);
}
if (!(options.focusGoalMinutes > 0 && options.focusGoalMinutes <= 240)) {
throw new UserException(`Focus goal minutes should be between 0 and 240, is ${options.focusGoalMinutes}`);
}
if (!Number.isInteger(options.snoozeMinutes)) {
throw new UserException(`Snooze minutes should be an integer, is ${options.focusGoalMinutes}`);
}
if (!(options.snoozeMinutes > 0 && options.snoozeMinutes <= 60)) {
throw new UserException(`Snooze minutes should be between 0 and 60, is ${options.snoozeMinutes}`);
}
if (!Number.isInteger(options.idleDetectionSeconds)) {
throw new UserException(`Idle detection seconds should be an integer, is ${options.idleDetectionSeconds}`);
}
if (!(options.idleDetectionSeconds >= 15 && options.idleDetectionSeconds <= 1800)) {
throw new UserException(`Idle detection seconds should be between 15 and 1800, is ${options.idleDetectionSeconds}`);
}
if (!Number.isInteger(options.idleDismissalSeconds)) {
throw new UserException(`Idle notification dismissal seconds should be an integer, is ${options.idleDismissalSeconds}`);
}
if (!(options.idleDismissalSeconds >= 30 && options.idleDismissalSeconds <= 1800)) {
throw new UserException(`Idle notification dismissal seconds should be between 30 and 1800, is ${options.idleDismissalSeconds}`);
}
if (!Number.isInteger(options.spilloverHours)) {
throw new UserException(`Spillover hours should be an integer, is ${options.spilloverHours}`);
}
if (!(options.spilloverHours >= 0 && options.spilloverHours <= 23)) {
throw new UserException(`Spillover hours should be between 0 and 23, is ${options.spilloverHours}`);
}
};
async function writeOptions(options) {
return chrome.storage.local.set({ options });
}
const optionsChangeSubscribers = [];
function notifyOptionsChanged(newOptions) {
optionsChangeSubscribers.forEach((subscriber) => subscriber(newOptions));
}
async function setOptions(newOptions) {
vetOptions(newOptions);
const options = await getOptions();
options.focusGoalMinutes = newOptions.focusGoalMinutes;
options.snoozeMinutes = newOptions.snoozeMinutes;
options.idleDetectionSeconds = newOptions.idleDetectionSeconds;
options.idleDismissalSeconds = newOptions.idleDismissalSeconds;
options.spilloverHours = newOptions.spilloverHours;
options.showBadgeText = newOptions.showBadgeText;
options.showNotifications = newOptions.showNotifications;
notifyOptionsChanged(options);
await writeOptions(options);
return options;
}
async function resetStorage() {
stateCache = null;
historyCache = null;
optionsCache = null;
await chrome.storage.local.clear();
notifyStateChanged(await getState());
notifyHistoryChanged();
}
const updateAlarmName = 'phocus-update-alarm';
const updateAlarmConfig = {
periodInMinutes: 0.5,
};
const clearIdleAlarmName = 'phocus-clear-idle-alarm';
const focusGoalNotificationName = 'phocus-goal-notification';
const lockedNotificationName = 'phocus-locked-notification';
const idleNotificationName = 'phocus-idle-notification';
async function setNotes(notes) {
const state = await getState();
state.notes = notes || undefined;
await writeState();
}
async function enterFocus(args) {
const state = await getState();
if (state.inFocus) {
return state;
}
const options = await getOptions();
let startTimestamp;
if (args?.startTimestamp) {
startTimestamp = args.startTimestamp;
} else if (args?.startEvent) {
if (args.startEvent === 'since_active') {
if (!state.activeStartTimestamp) {
throw new UserException('No start timestamp recorded since when the computer has been active. Cannot record focus time since then.');
}
startTimestamp = state.activeStartTimestamp;
} else {
throw new Error(`Unknown startEvent: ${args.startEvent}`);
}
} else {
startTimestamp = Date.now();
}
const history = await getHistory();
if (history.length > 0) {
history.sort((a, b) => a.stopTimestamp - b.stopTimestamp);
const latestEntry = history[history.length - 1];
if (startTimestamp < latestEntry.stopTimestamp) {
throw new UserException(`Latest history entry ending at ${new Date(latestEntry.stopTimestamp)} precedes start timestamp at ${new Date(startTimestamp)}.`);
}
}
state.focusStartTimestamp = startTimestamp;
state.inFocus = true;
state.nextAlarmTimestamp = state.focusStartTimestamp + (options.focusGoalMinutes * 60000);
state.idleStartTimestamp = 0;
state.focusStopTimestamp = 0;
chrome.alarms.create(updateAlarmName, updateAlarmConfig);
await writeState();
return state;
}
async function leaveFocus(stopTimestamp = Date.now()) {
const state = await getState();
if (!state.inFocus) {
return state;
}
const startTimestamp = state.focusStartTimestamp;
state.inFocus = false;
state.focusStartTimestamp = 0;
state.nextAlarmTimestamp = 0;
state.idleStartTimestamp = 0;
state.focusStopTimestamp = stopTimestamp;
await addHistoryEntry({
startTimestamp: startTimestamp,
stopTimestamp: stopTimestamp,
notes: state.notes || '',
});
return state;
}
async function resumeFocus() {
const state = await getState();
if (state.inFocus) {
throw new UserException('Already in focus. Cannot resume the previous focus block.');
}
const history = await getHistory();
if (!history) {
throw new UserException('No history, cannot resume previous focus block.');
}
history.sort((a, b) => a.stopTimestamp - b.stopTimestamp);
const latestEntry = history[history.length - 1];
await deleteFromHistory(latestEntry);
await enterFocus({ startTimestamp: latestEntry.startTimestamp });
}
async function listHistory(filter) {
const history = await getHistory();
return history.filter((entry) => {
// The negation allows for undefined filters properties to have no effect.
return !(filter.fromTimestamp > entry.stopTimestamp) && !(filter.untilTimestamp < entry.startTimestamp);
}).toSorted((a, b) => {
return a.startTimestamp - b.startTimestamp
});
}
async function addHistoryEntry(entry) {
if (!Number.isInteger(entry.startTimestamp)) {
throw new UserException(`Bad start timestamp: ${entry.startTimestamp}`);
}
if (!Number.isInteger(entry.stopTimestamp)) {
throw new UserException(`Bad stop timestamp: ${entry.stopTimestamp}`);
}
if (entry.startTimestamp >= entry.stopTimestamp) {
throw new UserException(`Start time before stop time: ${new Date(entry.startTimestamp)}, ${new Date(entry.stopTimestamp)}`);
}
// TODO - This code has quite some overlap with leaveFocus().
const state = await getState();
const history = await getHistory();
const newEntry = {
id: state.nextHistoryId++,
version: 0,
startTimestamp: entry.startTimestamp,
stopTimestamp: entry.stopTimestamp,
notes: entry.notes,
};
history.push(newEntry);
await writeState();
await writeHistory();
}
async function updateHistoryEntry(entry) {
const history = await getHistory();
const state = await getState();
const index = history.findIndex((e) => e.id === entry.id);
if (index === -1) {
throw new UserException(`No recorded focus block with that ID: ${JSON.stringify(entry)}`);
}
const e = history[index];
if (e.version !== entry.version) {
throw new UserException(`Versions don't match: Recorded focus block is ${JSON.stringify(e)}, provided block is ${JSON.stringify(entry)}`);
}
if (entry.startTimestamp === undefined) {
entry.startTimestamp = e.startTimestamp;
} else if (!Number.isInteger(entry.startTimestamp)) {
throw new UserException(`Bad start timestamp: ${entry.startTimestamp}`);
}
if (entry.stopTimestamp === undefined) {
entry.stopTimestamp = e.stopTimestamp;
} else if (!Number.isInteger(entry.stopTimestamp)) {
throw new UserException(`Bad stop timestamp: ${entry.stopTimestamp}`);
}
if (entry.startTimestamp >= entry.stopTimestamp) {
throw new UserException(`Start time before stop time: ${new Date(entry.startTimestamp)}, ${new Date(entry.stopTimestamp)}`);
}
e.startTimestamp = entry.startTimestamp;
e.stopTimestamp = entry.stopTimestamp;
e.notes = entry.notes;
e.version++;
await writeHistory();
}
async function deleteFromHistory(entry) {
const history = await getHistory();
const state = await getState();
let index = history.findIndex((e) => e.id === entry.id);
if (index === -1) {
throw new UserException(`No recorded focus block with that ID: ${JSON.stringify(entry)}`);
}
const e = history[index];
if (e.version !== entry.version) {
throw new UserException(`Versions don't match: Recorded focus block is ${JSON.stringify(e)}, provided block is ${JSON.stringify(entry)}`);
}
history.splice(index, 1);
await writeHistory();
}
async function updateBadge() {
const state = await getState();
const options = await getOptions();
if (state.inFocus) {
const focusMinutes = Math.trunc((Date.now() - state.focusStartTimestamp) / 60000);
const isGoalAttained = focusMinutes >= options.focusGoalMinutes;
chrome.action.setIcon({ path: isGoalAttained ? icons['orange-blue'] : icons['orange'] });
if (options.showBadgeText) {
chrome.action.setBadgeTextColor({ color: isGoalAttained ? '#FFF' : '#000' });
chrome.action.setBadgeBackgroundColor({ color: isGoalAttained ? '#0076BA' : '#F2B13E' });
chrome.action.setBadgeText({ text: `${focusMinutes}'` });
} else {
chrome.action.setBadgeText({ text: '' });
}
} else {
chrome.action.setIcon({ path: icons['blue'] });
chrome.action.setBadgeText({ text: '' });
}
}
stateChangeSubscribers.push((_) => updateBadge());
optionsChangeSubscribers.push((_) => updateBadge());
async function notifyOfGoal() {
const options = await getOptions();
if (!options.showNotifications) {
return;
}
const state = await getState();
const elapsedMins = Math.round((Date.now() - state.focusStartTimestamp) / 60000);
await chrome.notifications.create(focusGoalNotificationName, {
type: 'basic',
iconUrl: icons['orange-blue']['128'],
title: 'Phocus',
message: `You have focused for ${elapsedMins} minutes now. Time for a break?`,
requireInteraction: true,
buttons: [{ title: 'Remind me later' }, { title: 'Take a break' }],
});
}
stateChangeSubscribers.push((newState) => {
if (!newState.inFocus) {
chrome.notifications.clear(focusGoalNotificationName);
}
});
async function notifyOfAutocancel() {
const options = await getOptions();
if (!options.showNotifications) {
return;
}
await chrome.notifications.create(lockedNotificationName, {
type: 'basic',
iconUrl: icons['blue']['128'],
title: 'Phocus',
message: 'Your focus time was cancelled when you locked your computer.',
requireInteraction: false,
});
}
stateChangeSubscribers.push((newState) => {
if (newState.inFocus) {
chrome.notifications.clear(lockedNotificationName);
}
});
async function notifyOfIdleDetection() {
const state = await getState();
const date = new Date(state.idleStartTimestamp);
await chrome.notifications.create(idleNotificationName, {
type: 'basic',
iconUrl: icons['orange']['128'],
title: 'Phocus',
message: `Your computer has been detected as idle since ${date.toLocaleTimeString()}. Have you left your focus then?`,
requireInteraction: true,
buttons: [{ title: 'Yes, I left my focus time.' }, { title: 'No, I was keeping my focus.' }],
});
}
stateChangeSubscribers.push((newState) => {
if (!newState.inFocus) {
chrome.notifications.clear(idleNotificationName);
}
});
async function initialize() {
const state = await getState();
if (!state.inFocus) {
return;
}
const alarm = await chrome.alarms.get(updateAlarmName);
if (!alarm) {
await chrome.alarms.create(updateAlarmName, updateAlarmConfig);
};
updateBadge();
}
initialize();
const commands = {
"get_state": async function(args) {
return getState();
},
"set_notes": async function(args) {
return setNotes(args);
},
"enter_focus": async function(args) {
return enterFocus(args);
},
"leave_focus": async function(args) {
return leaveFocus();
},
"resume_focus": async function(args) {
return resumeFocus();
},
"list_history": async function(args) {
return listHistory(args);
},
"add_history_entry": async function(args) {
return addHistoryEntry(args);
},
"update_history_entry": async function(args) {
return updateHistoryEntry(args);
},
"delete_from_history": async function(args) {
return deleteFromHistory(args);
},
"get_options": async function(args) {
return getOptions();
},
"set_options": async function(args) {
return setOptions(args.options);
},
"reset_storage": async function(args) {
return resetStorage();
},
};
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
const impl = commands[request.command];
if (!impl) {
throw new Error(`Unknown command in ${JSON.stringify(request)}.`);
}
impl(request.args)
.catch(err => {
sendResponse({
success: false,
error: {
name: err.name,
message: err.message,
},
})
})
.then(result => {
sendResponse({
success: true,
result: result,
})
});
return true;
});
chrome.alarms.onAlarm.addListener(async (alarm) => {
debug(`Received alarm: ${JSON.stringify(alarm)}`);
const state = await getState();
if (alarm.name === updateAlarmName) {
updateBadge();
if (state.nextAlarmTimestamp && Date.now() >= state.nextAlarmTimestamp) {
notifyOfGoal();
state.nextAlarmTimestamp = 0;
writeState(state);
}
} else if (alarm.name === clearIdleAlarmName) {
if (!state.idleStartTimestamp) {
return;
}
state.idleStartTimestamp = 0;
await writeState(state);
chrome.notifications.clear(idleNotificationName);
} else {
throw new Error(`Unexpected alarm: ${alarm}`);
}
});
chrome.notifications.onButtonClicked.addListener(async (notificationId, btnIndex) => {
const options = await getOptions();
if (notificationId === focusGoalNotificationName) {
chrome.notifications.clear(focusGoalNotificationName);
if (btnIndex === 0) {
// Snooze.
const state = await getState();
state.nextAlarmTimestamp = Date.now() + (options.snoozeMinutes * 60000);
await writeState(state);
return;
}
if (btnIndex === 1) {
// Take a break.
await leaveFocus();
return;
}
} else if (notificationId === idleNotificationName) {
chrome.notifications.clear(idleNotificationName);
const state = await getState();
if (btnIndex === 0) {
// User left focus.
await leaveFocus(state.idleStartTimestamp);
return;
} else if (btnIndex === 1) {
// User did not leave their focus.
state.idleStartTimestamp = 0;
await writeState(state);
return;
}
}
throw new Error(`Unknown notification: ${notificationId}`);
});
getOptions().then(options => chrome.idle.setDetectionInterval(options.idleDetectionSeconds));
optionsChangeSubscribers.push((newOptions) => chrome.idle.setDetectionInterval(newOptions.idleDetectionSeconds));
chrome.idle.onStateChanged.addListener(async (newState) => {
debug(`Registered state change to ${newState}`);
const state = await getState();
const options = await getOptions();
if (newState === 'active') {
state.activeStartTimestamp = Date.now();
await writeState(state);
if (state.idleStartTimestamp) {
chrome.alarms.create(clearIdleAlarmName, { delayInMinutes: options.idleDismissalSeconds / 60 });
}
return;
} else if (newState === 'idle') {
// Ask the user if they are still here and give them the option to retroactively leave their focus time.
if (state.inFocus && !state.idleStartTimestamp) {
chrome.alarms.clear(clearIdleAlarmName); // in case there's a pending alarm that might dismiss the notification too early
state.idleStartTimestamp = Date.now() - (options.idleDetectionSeconds * 1000);
await writeState(state);
notifyOfIdleDetection();
}
return;
} else if (newState === 'locked') {
// Cancel if machine is locked while in focus.
if (!state.inFocus) {
return;
}
if (state.idleStartTimestamp) {
leaveFocus(state.idleStartTimestamp);
} else {
leaveFocus();
}
notifyOfAutocancel();
return;
}
throw new Error(`Unknown idle state: ${newState}`);
});