-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
251 lines (209 loc) · 10.5 KB
/
Copy pathbackground.js
File metadata and controls
251 lines (209 loc) · 10.5 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
/**
* @fileoverview Background Service Worker — Focus Timer v5
*
* This is the MV3 service worker entry point. Its only responsibilities are:
* 1. Bootstrap all core modules and features in the correct order
* 2. Register Chrome event listeners (alarms, tabs, webNavigation, messages)
* 3. Delegate all real work to the appropriate module
*
* No business logic lives here. If you find yourself writing logic in this file,
* it belongs in a core module or feature module instead.
*
* Architecture: src/core/ + src/features/ → background.js (wiring only)
*/
'use strict';
// ─── Core modules ─────────────────────────────────────────────────────────────
import * as storage from './src/core/storageAdapter.js';
import * as timer from './src/core/timerEngine.js';
import * as sessionMgr from './src/core/sessionManager.js';
import { emit, HOOKS } from './src/core/eventBus.js';
// ─── Feature modules ──────────────────────────────────────────────────────────
import * as distraction from './src/features/distractionTracking/index.js';
import * as reflection from './src/features/reflectionSystem/index.js';
import * as visual from './src/features/visualEnforcement/index.js';
import * as insights from './src/features/insightsEngine/index.js';
// ─── Bootstrap ────────────────────────────────────────────────────────────────
/**
* Runs once when the service worker starts.
* Initialises features, runs storage migrations, ensures today is in weekly data.
*/
const api = typeof browser !== 'undefined' ? browser : chrome; // Firefox compatibility
async function bootstrap() {
await storage.migrate();
await sessionMgr.ensureTodayInWeekly();
distraction.init();
insights.init();
visual.init();
console.info('[FocusTimer] Service worker bootstrapped.');
}
bootstrap().catch(err => console.error('[FocusTimer] Bootstrap failed:', err));
// ─── Alarm Handler ────────────────────────────────────────────────────────────
api.alarms.onAlarm.addListener(async ({ name }) => {
if (name === timer.ALARMS.COMPLETE) {
const { state, completedSession } = await timer.complete();
if (!completedSession) return;
const sites = await storage.getDistractingSites();
await visual.showSessionOverlay(state.pendingReflection);
await visual.broadcastState(state, sites);
} else if (name === timer.ALARMS.TICK) {
const { state, expired } = await timer.tick();
if (!expired) {
const sites = await storage.getDistractingSites();
await visual.broadcastState(state, sites);
}
}
});
// ─── Tab Activation ───────────────────────────────────────────────────────────
api.tabs.onActivated.addListener(async ({ tabId }) => {
const state = await storage.getState();
if (state.status !== 'running' || !state.currentSession) return;
// Record the switch and update session in storage
const { patchState, getState: gs } = storage;
const current = await gs();
if (!current.currentSession) return;
const updated = sessionMgr.recordTabSwitch(current.currentSession);
const newState = await patchState({ currentSession: updated });
// Flush any open distraction timing and re-evaluate the newly active tab
await distraction.leaveDistraction();
let tab;
try { tab = await api.tabs.get(tabId); } catch { return; }
const url = tab.url || tab.pendingUrl || '';
const sites = await storage.getDistractingSites();
await visual.ensureContentScript(tabId, url);
await visual.broadcastState(newState, sites);
if (distraction.isDistracting(url, sites)) {
await distraction.enterDistraction(tabId, url);
// Re-broadcast with updated distraction visit count
const latestState = await storage.getState();
await visual.broadcastState(latestState, sites);
}
});
// ─── Web Navigation ───────────────────────────────────────────────────────────
api.webNavigation.onCommitted.addListener(async ({ tabId, url, frameId }) => {
if (frameId !== 0) return;
const state = await storage.getState();
if (state.status !== 'running' || !state.currentSession) return;
if (!visual.isInjectableUrl(url)) return;
const sites = await storage.getDistractingSites();
const wasDistracting = distraction.getDistractionTabId() === tabId;
const nowDistracting = distraction.isDistracting(url, sites);
if (wasDistracting && !nowDistracting) await distraction.leaveDistraction();
else if (!wasDistracting && nowDistracting) await distraction.enterDistraction(tabId, url);
});
api.webNavigation.onCompleted.addListener(async ({ tabId, url, frameId }) => {
if (frameId !== 0) return;
const state = await storage.getState();
if (state.status !== 'running') return;
if (!visual.isInjectableUrl(url)) return;
const sites = await storage.getDistractingSites();
// Short delay so the page DOM is settled before content script applies enforcement
setTimeout(async () => {
try {
await api.tabs.sendMessage(tabId, { action: 'applyFocusMode', state, sites });
} catch { /* content script not yet ready; it will self-sync via storage.onChanged */ }
}, 150);
});
// ─── Startup Recovery ─────────────────────────────────────────────────────────
api.runtime.onStartup.addListener(async () => {
await bootstrap();
const state = await timer.recover();
if (state.status === 'running') {
await visual.injectIntoAllTabs();
const sites = await storage.getDistractingSites();
await visual.broadcastState(state, sites);
}
});
// ─── Message Router ───────────────────────────────────────────────────────────
api.runtime.onMessage.addListener((msg, _sender, sendResponse) => {
_handleMessage(msg).then(sendResponse).catch(err => {
console.error('[FocusTimer] Message handler error:', err);
sendResponse({ error: err.message });
});
return true; // keep channel open for async response
});
/**
* Central async message handler. Returns an object sent back to the caller.
*
* @param {{ action: string, [key: string]: * }} msg
* @returns {Promise<Object>}
*/
async function _handleMessage(msg) {
switch (msg.action) {
// ── Timer controls ──────────────────────────────────────────────────────
case 'start': {
const state = await timer.start(msg.duration);
const stats = await sessionMgr.getDailyStats();
return { state, stats };
}
case 'pause': {
const state = await timer.pause();
const sites = await storage.getDistractingSites();
await visual.broadcastState(state, sites);
const stats = await sessionMgr.getDailyStats();
return { state, stats };
}
case 'resume': {
const state = await timer.resume();
await visual.injectIntoAllTabs();
const sites = await storage.getDistractingSites();
await visual.broadcastState(state, sites);
const stats = await sessionMgr.getDailyStats();
return { state, stats };
}
case 'reset': {
await distraction.leaveDistraction();
const state = await timer.reset();
const stats = await sessionMgr.getDailyStats();
return { state, stats };
}
// ── Content script callbacks ────────────────────────────────────────────
case 'contentOverlayReturn':
await distraction.leaveDistraction();
return { ok: true };
case 'contentOverlayContinue':
return { ok: true };
// ── Reflection ──────────────────────────────────────────────────────────
case 'saveReflection':
await reflection.save(msg.sessionId, msg.text);
return { ok: true };
case 'skipReflection':
await reflection.skip(msg.sessionId);
return { ok: true };
case 'getReflections':
return { reflections: await reflection.getAll() };
// ── Sites management ────────────────────────────────────────────────────
case 'addSite': {
const sites = await storage.getDistractingSites();
const domain = msg.domain.toLowerCase().replace(/^www\./, '').trim();
if (domain && !sites.includes(domain)) {
sites.push(domain);
await storage.setValue(storage.KEYS.SITES, sites);
}
return { sites };
}
case 'removeSite': {
const sites = (await storage.getDistractingSites()).filter(s => s !== msg.domain);
await storage.setValue(storage.KEYS.SITES, sites);
return { sites };
}
case 'getSites':
return { sites: await storage.getDistractingSites() };
// ── Data queries ────────────────────────────────────────────────────────
case 'getWeekly':
return { weekly: await storage.getWeeklyData() };
case 'getSessions':
return { sessions: await storage.getSessions() };
case 'getPatterns':
return { patterns: await storage.getPatterns() };
case 'getSuggestions':
return { suggestions: await insights.generateSuggestions() };
// ── State query (default for popup init) ────────────────────────────────
default: {
const [state, stats] = await Promise.all([
storage.getState(),
sessionMgr.getDailyStats(),
]);
return { state, stats };
}
}
}