-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.ts
More file actions
67 lines (61 loc) · 2.5 KB
/
main.ts
File metadata and controls
67 lines (61 loc) · 2.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
/// <reference types="chrome"/>
import { SessionManager } from './sessionManager.ts';
import { EventProcessor } from './eventProcessor.ts';
import { StorageService } from './storage.ts';
// Listen for messages from Content Script or Popup
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'TELEMETRY_EVENT') {
// Only accept telemetry from the tab that owns the session
StorageService.getCurrentTabId().then((ownedTabId) => {
if (sender.tab?.id === ownedTabId) {
EventProcessor.processEvent(message.payload);
}
});
} else if (message.action === 'GET_SESSION_STATUS') {
// Only return active=true to the tab that owns the session
Promise.all([
SessionManager.getCurrentSession(),
StorageService.getCurrentTabId()
]).then(([session, ownedTabId]) => {
const isOwnerTab = sender.tab?.id === ownedTabId;
sendResponse({
active: !!session && isOwnerTab,
config: (!!session && isOwnerTab) ? session.config : undefined
});
});
return true; // async
} else if (message.action === 'START_SESSION') {
// tabId is sent by the popup, which queries the active tab before sending
const tabId: number | undefined = message.payload?.tabId;
SessionManager.startSession(message.payload).then(async (session) => {
if (tabId) {
await StorageService.setCurrentTabId(tabId);
// Only notify the single target tab
chrome.tabs.sendMessage(tabId, {
action: 'SESSION_STARTED',
config: session.config
}).catch(() => { });
}
sendResponse({ session });
});
return true;
} else if (message.action === 'STOP_SESSION') {
StorageService.getCurrentTabId().then((tabId) => {
SessionManager.endSession().then(async (session) => {
await StorageService.setCurrentTabId(null);
// Only notify the tab that owned the session
if (tabId) {
chrome.tabs.sendMessage(tabId, {
action: 'SESSION_STOPPED'
}).catch(() => { });
}
sendResponse({ session });
});
});
return true;
}
});
// Initialize
chrome.runtime.onInstalled.addListener(() => {
// Extension installed
});