-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpanel.js
438 lines (364 loc) · 15.1 KB
/
panel.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
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
console.log('Panel script loaded. Tab ID:', chrome.devtools.inspectedWindow.tabId);
let backgroundPageConnection = chrome.runtime.connect({
name: 'panel',
});
let eventGroups = {};
let lastHeartbeatTime = Date.now();
const HEARTBEAT_TIMEOUT = 60000; // 60 seconds (twice the polling interval)
function checkHeartbeat() {
const currentTime = Date.now();
if (currentTime - lastHeartbeatTime > HEARTBEAT_TIMEOUT) {
console.log('Heartbeat timeout, attempting to reconnect...');
updateConnectionStatus(false);
initializePanel();
}
}
backgroundPageConnection.onMessage.addListener(function (message) {
console.log('Panel received message:', message);
if (message.type === 'HEARTBEAT') {
lastHeartbeatTime = Date.now();
updateConnectionStatus(true);
} else if (message.type === 'HTMX_DEBUG_INFO' || message.type === 'HTMX_EVENT' || message.type === 'HTMX_EVENT_FOR_PANEL') {
console.log('Received htmx event:', message.data);
if (message.data) {
handleHtmxEvent(message.data);
} else {
console.warn('Received htmx event message without data:', message);
}
} else if (message.type === 'TEST') {
console.log('Received test message:', message.data);
addDebugMessage(`Test message received: ${message.data}`, 'text-blue-500');
updateConnectionStatus(true);
} else if (message.type === 'TEST_CONFIRMATION') {
console.log('Received test confirmation:', message.data);
addDebugMessage(`Test confirmation: ${message.data}`, 'text-green-500');
updateConnectionStatus(true);
} else if (message.type === 'ERROR') {
console.error('Received error message:', message.error);
addDebugMessage(`Error: ${message.error}`, 'text-red-500');
} else if (message.type === 'CONNECTION_TEST') {
console.log('Received CONNECTION_TEST message');
// No need to update UI for CONNECTION_TEST
} else {
console.warn('Received unknown message type:', message);
}
});
function initializePanel() {
backgroundPageConnection.postMessage({
name: 'init',
tabId: chrome.devtools.inspectedWindow.tabId,
});
console.log('Panel initialized for tab:', chrome.devtools.inspectedWindow.tabId);
lastHeartbeatTime = Date.now(); // Reset heartbeat time on initialization
}
function updateDebugInfo() {
const debugInfo = document.getElementById('debug-info');
debugInfo.innerHTML = '';
Object.entries(eventGroups).forEach(([groupName, events]) => {
const groupElement = createGroupElement(groupName, events);
debugInfo.appendChild(groupElement);
});
}
function createGroupElement(groupName, events) {
const groupElement = document.createElement('div');
groupElement.className = 'event-group mb-4 border border-gray-200 rounded-lg overflow-hidden';
const groupHeader = document.createElement('h3');
groupHeader.textContent = `Group: ${groupName} (${events.length} events)`;
groupHeader.className = 'group-header collapsed text-lg font-semibold bg-gray-100 px-4 py-2 cursor-pointer hover:bg-gray-200 transition-colors duration-200';
groupHeader.addEventListener('click', toggleGroup);
groupElement.appendChild(groupHeader);
const eventsContainer = document.createElement('div');
eventsContainer.className = 'events-container hidden';
events.forEach((event) => {
const eventElement = createEventElement(event);
eventsContainer.appendChild(eventElement);
});
groupElement.appendChild(eventsContainer);
return groupElement;
}
function createEventElement(eventData) {
const eventElement = document.createElement('div');
eventElement.className = 'htmx-event border-t border-gray-200 mb-4';
const eventHeader = document.createElement('h4');
eventHeader.textContent = `${eventData.type || 'Unknown Event'} (${eventData.timestamps.join(', ')})`;
eventHeader.className = 'event-header collapsed text-md font-medium bg-gray-50 px-4 py-2 cursor-pointer hover:bg-gray-100 transition-colors duration-200';
eventHeader.addEventListener('click', toggleEvent);
const eventContent = document.createElement('div');
eventContent.className = 'event-content hidden px-4 py-2 bg-white';
if (eventData.target) {
const targetInfo = document.createElement('div');
const targetHeader = document.createElement('h5');
targetHeader.className = 'font-bold mt-2';
targetHeader.textContent = 'Target Element:';
targetInfo.appendChild(targetHeader);
const tagP = document.createElement('p');
tagP.textContent = `Tag: ${eventData.target.tagName || 'N/A'}`;
targetInfo.appendChild(tagP);
const idP = document.createElement('p');
idP.textContent = `ID: ${eventData.target.id || 'N/A'}`;
targetInfo.appendChild(idP);
const classP = document.createElement('p');
classP.textContent = `Class: ${eventData.target.className || 'N/A'}`;
targetInfo.appendChild(classP);
// Handle HX Attributes
if (eventData.target.hxAttributes && eventData.target.hxAttributes.length > 0) {
const hxHeader = document.createElement('h5');
hxHeader.className = 'font-bold mt-2 text-blue-600';
hxHeader.textContent = 'HX Attributes:';
targetInfo.appendChild(hxHeader);
const hxList = document.createElement('ul');
hxList.className = 'list-disc pl-5';
eventData.target.hxAttributes.forEach((attr) => {
const li = document.createElement('li');
li.textContent = `${attr.name}: ${attr.value}`;
li.className = 'text-blue-600';
hxList.appendChild(li);
});
targetInfo.appendChild(hxList);
} else {
const noHx = document.createElement('p');
noHx.className = 'text-gray-500';
noHx.textContent = 'No HX attributes found.';
targetInfo.appendChild(noHx);
}
eventContent.appendChild(targetInfo);
}
// Add snapshot information
if (eventData.afterSnapshot) {
const snapshotInfo = document.createElement('div');
snapshotInfo.className = 'mt-4';
const snapshotHeader = document.createElement('h5');
snapshotHeader.className = 'font-bold';
snapshotHeader.textContent = 'Element Snapshot:';
snapshotInfo.appendChild(snapshotHeader);
if (eventData.beforeSnapshot) {
const beforeHeader = document.createElement('h6');
beforeHeader.className = 'font-semibold mt-2';
beforeHeader.textContent = 'Before:';
snapshotInfo.appendChild(beforeHeader);
const beforePre = document.createElement('pre');
beforePre.className = 'mt-1 p-2 bg-gray-100 rounded overflow-x-auto text-sm';
beforePre.textContent = JSON.stringify(eventData.beforeSnapshot, null, 2);
snapshotInfo.appendChild(beforePre);
}
const afterHeader = document.createElement('h6');
afterHeader.className = 'font-semibold mt-2';
afterHeader.textContent = 'After:';
snapshotInfo.appendChild(afterHeader);
const afterPre = document.createElement('pre');
afterPre.className = 'mt-1 p-2 bg-gray-100 rounded overflow-x-auto text-sm';
afterPre.textContent = JSON.stringify(eventData.afterSnapshot, null, 2);
snapshotInfo.appendChild(afterPre);
eventContent.appendChild(snapshotInfo);
}
const fullDataHeader = document.createElement('h5');
fullDataHeader.textContent = 'Full Event Data';
fullDataHeader.className = 'font-bold mt-4 cursor-pointer';
fullDataHeader.addEventListener('click', toggleFullData);
const fullDataContent = document.createElement('div');
fullDataContent.className = 'hidden';
const fullDataPre = document.createElement('pre');
fullDataPre.className = 'mt-2 p-2 bg-gray-100 rounded overflow-x-auto text-sm';
fullDataPre.textContent = JSON.stringify(eventData, null, 2);
fullDataContent.appendChild(fullDataPre);
eventContent.appendChild(fullDataHeader);
eventContent.appendChild(fullDataContent);
eventElement.appendChild(eventHeader);
eventElement.appendChild(eventContent);
return eventElement;
}
function toggleFullData(event) {
const header = event.target;
const content = header.nextElementSibling;
content.classList.toggle('hidden');
}
function toggleGroup(event) {
const header = event.target;
const group = header.parentElement;
const eventsContainer = group.querySelector('.events-container');
eventsContainer.classList.toggle('hidden');
// Update arrow icon based on the visibility of the events container
if (eventsContainer.classList.contains('hidden')) {
header.classList.add('collapsed');
} else {
header.classList.remove('collapsed');
}
}
function toggleEvent(event) {
const header = event.target;
const eventElement = header.parentElement;
const content = eventElement.querySelector('.event-content');
content.classList.toggle('hidden');
// Update arrow icon based on the visibility of the event content
if (content.classList.contains('hidden')) {
header.classList.add('collapsed');
} else {
header.classList.remove('collapsed');
}
}
document.getElementById('search-input').addEventListener('input', function (e) {
const searchTerm = e.target.value.toLowerCase();
document.querySelectorAll('.htmx-event').forEach((event) => {
const eventContent = event.querySelector('.event-content').textContent.toLowerCase();
const isVisible = eventContent.includes(searchTerm);
event.style.display = isVisible ? 'block' : 'none';
const group = event.closest('.event-group');
group.style.display = group.querySelector('.htmx-event[style="display: block;"]') ? 'block' : 'none';
});
});
document.getElementById('clear-button').addEventListener('click', function () {
document.getElementById('debug-info').innerHTML = '<p class="text-gray-500 text-center py-4">Debug information cleared. Waiting for new htmx events...</p>';
eventGroups = {};
});
document.querySelectorAll('.filter-button').forEach((button) => {
button.addEventListener('click', function () {
document.querySelectorAll('.filter-button').forEach((btn) => {
btn.classList.remove('active', 'bg-blue-500', 'text-white');
btn.classList.add('bg-gray-200', 'text-gray-700');
});
this.classList.remove('bg-gray-200', 'text-gray-700');
this.classList.add('active', 'bg-blue-500', 'text-white');
const filter = this.dataset.filter;
filterDebugInfo(filter);
});
});
function filterDebugInfo(filter) {
document.querySelectorAll('.htmx-event').forEach((event) => {
const eventType = event.querySelector('.event-header').textContent;
let display = 'none';
if (filter === 'ALL') {
display = 'block';
} else if (filter === 'REQUEST' && (eventType.includes('beforeRequest') || eventType.includes('beforeSend') || eventType.includes('xhr:loadstart'))) {
display = 'block';
} else if (filter === 'RESPONSE' && (eventType.includes('afterRequest') || eventType.includes('xhr:loadend') || eventType.includes('load'))) {
display = 'block';
}
event.style.display = display;
const group = event.closest('.event-group');
group.style.display = group.querySelector('.htmx-event[style="display: block;"]') ? 'block' : 'none';
});
}
function addDebugMessage(message, className = 'text-gray-500') {
const debugInfo = document.getElementById('debug-info');
const messageElement = document.createElement('p');
messageElement.className = `${className} py-1`;
messageElement.textContent = message;
debugInfo.appendChild(messageElement);
}
function sendTestMessage() {
const testMessage = {
name: 'test',
tabId: chrome.devtools.inspectedWindow.tabId,
data: 'Test message from panel',
};
backgroundPageConnection.postMessage(testMessage);
// addDebugMessage('Test message sent to background script', 'text-green-500');
console.log('Test message sent:', testMessage);
}
function updateConnectionStatus(status) {
const statusContainer = document.getElementById('connection-status-container');
if (statusContainer) {
statusContainer.innerHTML = '';
const statusElement = document.createElement('span');
statusElement.textContent = status ? 'Connected' : 'Disconnected';
statusElement.className = status ? 'text-green-500' : 'text-red-500';
statusContainer.appendChild(statusElement);
}
}
function verifyConnection() {
sendTestMessage();
checkHeartbeat();
}
// Initialize the panel
document.addEventListener('DOMContentLoaded', function () {
const debugInfo = document.getElementById('debug-info');
if (debugInfo) {
debugInfo.innerHTML = '<p class="text-gray-500 text-center py-4">htmx-debugger initialized. Waiting for events...</p>';
} else {
console.error('Debug info element not found on DOMContentLoaded');
}
initializePanel();
verifyConnection();
// Start periodic connection check and heartbeat check
setInterval(verifyConnection, 30000); // Check every 30 seconds
});
// Handle page reloads
chrome.devtools.network.onNavigated.addListener(function () {
console.log('Page reloaded, reinitializing htmx-debugger');
addDebugMessage('Page reloaded. Waiting for new htmx events...', 'text-yellow-500');
eventGroups = {};
initializePanel();
});
// Error handling for runtime errors
chrome.runtime.onMessage.addListener(function (message) {
if (message.type === 'ERROR') {
console.error('Runtime error:', message.error);
addDebugMessage(`Error: ${message.error}`, 'text-red-500');
}
});
// Add configurable option to suppress empty events
const SUPPRESS_EMPTY_EVENTS = true; // Set to false to allow all events
function handleHtmxEvent(event) {
if (SUPPRESS_EMPTY_EVENTS && !event.target && (!event.detail || Object.keys(event.detail).length === 0)) {
console.log('Suppressed empty event:', event);
return;
}
console.log('Handling htmx event:', event);
// Check if event is a valid object
if (typeof event !== 'object' || event === null) {
console.warn('Received invalid event:', event);
return;
}
// Determine the event type and timestamp
let eventType = event.type || (event.detail && event.detail.type) || 'unknown';
let eventTimestamp = event.timestamp || new Date().toISOString();
// Create a normalized event object
let normalizedEvent = {
type: eventType,
timestamps: [eventTimestamp],
target: event.target || event.detail?.target || null,
detail: event.detail || null,
beforeSnapshot: captureElementSnapshot(event.target),
// Add any other properties you want to preserve
};
let groupName = determineGroupName(normalizedEvent);
if (!eventGroups[groupName]) {
eventGroups[groupName] = [];
}
// Check if an event with the same type and target already exists
let existingEvent = eventGroups[groupName].find((e) => e.type === normalizedEvent.type && JSON.stringify(e.target) === JSON.stringify(normalizedEvent.target));
if (existingEvent) {
// If it exists, add the new timestamp to the existing event
existingEvent.timestamps.push(eventTimestamp);
// Update the afterSnapshot
existingEvent.afterSnapshot = captureElementSnapshot(event.target);
} else {
// Otherwise, add the new event to the group
eventGroups[groupName].push(normalizedEvent);
}
// Update the display
updateDebugInfo();
}
function captureElementSnapshot(element) {
if (!element) return null;
return {
outerHTML: element.outerHTML,
innerHTML: element.innerHTML,
attributes: Array.from(element.attributes).map((attr) => ({
name: attr.name,
value: attr.value,
})),
};
}
function determineGroupName(event) {
if (event.target && event.target.id) {
return event.target.id;
} else if (event.target && event.target.tagName) {
return event.target.tagName.toLowerCase();
} else if (event.type && event.type.startsWith('htmx:')) {
// Instead of returning 'htmx', let's categorize based on the specific htmx event type
return event.type.split(':')[1]; // This will return the part after 'htmx:'
} else {
return 'other';
}
}