-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessageRouter.js
More file actions
128 lines (111 loc) · 3.57 KB
/
messageRouter.js
File metadata and controls
128 lines (111 loc) · 3.57 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
// Routes runtime messages to background features (DB, IA cache, status).
const LAST_COORD_SIG_BY_SENDER = new Map(); // key -> { sig, at }
function senderKey(sender) {
try {
const tabId = sender && sender.tab && typeof sender.tab.id === "number" ? sender.tab.id : "no_tab";
const frameId = sender && typeof sender.frameId === "number" ? sender.frameId : 0;
return `${tabId}:${frameId}`;
} catch (_) {
return "unknown";
}
}
export function installMessageRouter({
bgConfig,
getDb,
saveLogToDatabase,
viewLogs,
clearDatabaseAll,
iasCache,
}) {
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
try {
if (!message || !message.type) return;
if (message.type === "settings_updated") {
bgConfig.reload();
return;
}
if (message.type === "log_coordinates") {
// Dedupe identical signatures from the same tab+frame within a short window
try {
const sig = message && message.data && typeof message.data.signature === "string" ? message.data.signature : null;
if (sig) {
const key = senderKey(sender);
const prev = LAST_COORD_SIG_BY_SENDER.get(key);
const now = Date.now();
if (prev && prev.sig === sig && now - prev.at < 500) {
return;
}
LAST_COORD_SIG_BY_SENDER.set(key, { sig, at: now });
}
} catch (_) {}
const logEntry = {
timestamp: new Date().toISOString(),
coordinates: message.data,
};
saveLogToDatabase(logEntry);
return;
}
if (message.type === "log_keypress") {
const kp = message.data || {};
const entry = {
timestamp: kp.timestamp || new Date().toISOString(),
coordinates: {
message: kp.message || "First 's' key press logged",
timestamp: kp.timestamp || new Date().toISOString(),
},
};
saveLogToDatabase(entry);
return;
}
if (message.type === "log_acceptance") {
const data = message.data || {};
const entry = {
timestamp: new Date().toISOString(),
event: { type: "suggestion_accepted", ...data },
};
saveLogToDatabase(entry);
iasCache.markAcceptanceAgainstPrimaryRoot();
return;
}
if (message.type === "log_acceptance_check") {
iasCache.appendAcceptanceCheck(message.data || {});
return;
}
if (message.type === "view_logs") {
viewLogs((logs) => {
sendResponse({ logs });
});
return true;
}
if (message.type === "export_logs") {
viewLogs((logs) => {
sendResponse({ logs });
});
return true;
}
if (message.type === "clear_database") {
clearDatabaseAll(() => {
sendResponse({ status: "cleared" });
});
return true;
}
if (message.type === "force_update_ias" || message.type === "get_ia_summary") {
iasCache.updateIasCache(() => {
if (message.type === "force_update_ias") {
sendResponse({ ok: true });
} else {
iasCache.getIasSummary((summary) => sendResponse(summary));
}
});
return true;
}
if (message.type === "get_status") {
iasCache.getStatus((status) => sendResponse({ ok: true, ...status }));
return true;
}
// Other message types intentionally ignored.
} catch (err) {
console.error("background message handler error:", err);
}
});
}