-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackground.js
210 lines (183 loc) · 6.31 KB
/
background.js
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
/* eslint-env worker */
let connections = {};
let messageQueue = [];
let isProcessingQueue = false;
let messageCounter = 0;
const MAX_QUEUE_SIZE = 1000;
const RATE_LIMIT_INTERVAL = 1000; // 1 second
const MAX_MESSAGES_PER_INTERVAL = 100;
let heartbeatIntervals = {};
function logWithTimestamp(message, data) {
console.log(`[${new Date().toISOString()}] ${message}`, data);
}
function processMessageQueue() {
if (isProcessingQueue || messageQueue.length === 0) return;
isProcessingQueue = true;
const startTime = Date.now();
let processedCount = 0;
while (messageQueue.length > 0 && processedCount < MAX_MESSAGES_PER_INTERVAL) {
const { message, sender, sendResponse } = messageQueue.shift();
try {
handleMessage(message, sender, sendResponse);
messageCounter++;
} catch (error) {
console.error('Error processing message:', error);
reportError(error);
}
processedCount++;
if (Date.now() - startTime >= RATE_LIMIT_INTERVAL) {
break;
}
}
isProcessingQueue = false;
if (messageQueue.length > 0) {
setTimeout(processMessageQueue, RATE_LIMIT_INTERVAL);
}
logWithTimestamp(`Processed ${processedCount} messages. Total messages: ${messageCounter}`);
}
function queueMessage(message, sender, sendResponse) {
if (messageQueue.length >= MAX_QUEUE_SIZE) {
logWithTimestamp('Message queue full. Dropping oldest message.');
messageQueue.shift();
}
messageQueue.push({ message, sender, sendResponse });
processMessageQueue();
}
function handleMessage(request, sender, sendResponse) {
logWithTimestamp('Processing message:', request);
if (sender.tab) {
const tabId = sender.tab.id;
if (tabId in connections) {
if (request.type === 'HTMX_EVENT' || request.type.startsWith('htmx:')) {
logWithTimestamp(`Forwarding htmx event to panel for tab ${tabId}:`, request);
connections[tabId].postMessage({
type: 'HTMX_EVENT_FOR_PANEL',
data: request.data, // Ensure we're sending the full data
});
logWithTimestamp(`htmx event sent to panel for tab ${tabId}`);
} else {
// Only forward non-CONNECTION_TEST messages to the panel
if (request.type !== 'CONNECTION_TEST') {
logWithTimestamp(`Forwarding message to panel for tab ${tabId}:`, request);
connections[tabId].postMessage(request);
logWithTimestamp(`Message sent to panel for tab ${tabId}`);
} else {
logWithTimestamp(`Received CONNECTION_TEST from tab ${tabId}`);
}
}
} else {
logWithTimestamp('Tab not found in connection list:', tabId);
}
} else if (request.type === 'TEST') {
logWithTimestamp('Received test message:', request);
Object.values(connections).forEach((port) => {
port.postMessage(request);
logWithTimestamp('Test message sent to panel');
});
} else {
logWithTimestamp('sender.tab not defined and not a test message.');
}
// Always send a response to avoid timeouts
if (sendResponse) {
sendResponse({ status: 'Message processed' });
}
}
function startHeartbeat(tabId) {
if (heartbeatIntervals[tabId]) {
clearInterval(heartbeatIntervals[tabId]);
}
heartbeatIntervals[tabId] = setInterval(() => {
// Changed to setInterval (self is already recognized)
if (connections[tabId]) {
connections[tabId].postMessage({ type: 'HEARTBEAT' });
} else {
clearInterval(heartbeatIntervals[tabId]);
delete heartbeatIntervals[tabId];
}
}, 5000); // Send heartbeat every 5 seconds
}
// Listen for connections from the devtools panel
chrome.runtime.onConnect.addListener(function (port) {
if (port.name !== 'panel') return;
const extensionListener = function (message) {
if (message.name === 'init') {
connections[message.tabId] = port;
logWithTimestamp(`Panel connected for tab ${message.tabId}`);
startHeartbeat(message.tabId);
}
};
port.onMessage.addListener(extensionListener);
port.onDisconnect.addListener(function (disconnectedPort) {
port.onMessage.removeListener(extensionListener);
const tabs = Object.keys(connections);
for (let i = 0, len = tabs.length; i < len; i++) {
if (connections[tabs[i]] === disconnectedPort) {
logWithTimestamp(`Panel disconnected for tab ${tabs[i]}`);
delete connections[tabs[i]];
clearInterval(heartbeatIntervals[tabs[i]]);
delete heartbeatIntervals[tabs[i]];
break;
}
}
});
});
// Listen for messages from content scripts
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
// Use queueMessage instead of directly calling handleMessage
queueMessage(request, sender, sendResponse);
return true; // Indicate that the response is sent asynchronously
});
logWithTimestamp('Background service worker loaded');
// Keep the service worker alive and manage periodic tasks
chrome.runtime.onInstalled.addListener(() => {
logWithTimestamp('Extension installed or updated');
chrome.alarms.create('keep-alive', { periodInMinutes: 1 });
chrome.alarms.create('reset-counter', { periodInMinutes: 60 }); // Reset counter every hour
chrome.alarms.create('log-stats', { periodInMinutes: 5 }); // Log stats every 5 minutes
});
chrome.runtime.onUpdateAvailable.addListener(() => {
logWithTimestamp('Extension update available. Reloading...');
chrome.runtime.reload();
});
chrome.alarms.onAlarm.addListener((alarm) => {
switch (alarm.name) {
case 'keep-alive':
logWithTimestamp('Keep-alive ping');
break;
case 'reset-counter':
logWithTimestamp(`Resetting message counter. Previous count: ${messageCounter}`);
messageCounter = 0;
break;
case 'log-stats':
logWithTimestamp(`Current message count: ${messageCounter}`);
logWithTimestamp(`Current queue size: ${messageQueue.length}`);
break;
}
});
// global error handling
function reportError(error) {
console.error('Error in background script:', error);
Object.values(connections).forEach((port) => {
port.postMessage({ type: 'ERROR', error: error.message, stack: error.stack });
});
}
globalThis.addEventListener('error', (event) => {
reportError(event.error);
});
globalThis.addEventListener('unhandledrejection', (event) => {
reportError(event.reason);
});
chrome.runtime.onInstalled.addListener(() => {
logWithTimestamp('Extension installed');
});
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'heartbeat') {
sendResponse({ status: 'alive' });
}
if (message.type === 'HTMX_EVENT') {
chrome.runtime.sendMessage({
type: 'HTMX_EVENT_FOR_PANEL',
data: message.data,
});
}
});