-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
346 lines (319 loc) · 11.3 KB
/
popup.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
// popup.js
let currentTabId = null;
// Define grouped copy options
const groupedCopyOptions = {
copyAllErrors: ["ERROR", "EXCEPTION", "NETWORKERROR", "CORSERROR"],
copyAllWarnings: ["WARNING", "DEPRECATIONWARNING", "SECURITYWARNING"]
};
// Debounce function to limit the rate of function execution
function debounce(func, wait) {
let timeout;
return function(...args) {
clearTimeout(timeout);
timeout = setTimeout(() => func.apply(this, args), wait);
};
}
document.addEventListener('DOMContentLoaded', function() {
// Attach event listener to the toggle capture button
const toggleCaptureButton = document.getElementById('toggleCapture');
if (toggleCaptureButton) {
toggleCaptureButton.addEventListener('click', toggleCapture);
}
// Attach event listeners to all icon buttons
const iconButtonIds = [
'btnErrors',
'btnWarnings',
'btnLogs',
'btnInfo',
'btnDebug',
'btnDeprecationWarnings',
'btnNetworkErrors',
'btnCORSErrors',
'btnSecurityWarnings',
'btnExceptions',
'btnCopyAllErrors',
'btnCopyAllWarnings'
];
iconButtonIds.forEach(id => {
const button = document.getElementById(id);
if (button) {
switch (id) {
case 'btnErrors':
button.addEventListener('click', () => copyFilteredLogs(['ERROR', 'EXCEPTION', 'NETWORKERROR', 'CORSERROR'], 'Errors'));
break;
case 'btnWarnings':
button.addEventListener('click', () => copyFilteredLogs(['WARNING', 'DEPRECATIONWARNING', 'SECURITYWARNING'], 'Warnings'));
break;
case 'btnLogs':
button.addEventListener('click', () => copyFilteredLogs(['LOG'], 'Logs'));
break;
case 'btnInfo':
button.addEventListener('click', () => copyFilteredLogs(['INFO'], 'Info'));
break;
case 'btnDebug':
button.addEventListener('click', () => copyFilteredLogs(['DEBUG'], 'Debug'));
break;
case 'btnDeprecationWarnings':
button.addEventListener('click', () => copyFilteredLogs(['DEPRECATIONWARNING'], 'Deprecation Warnings'));
break;
case 'btnNetworkErrors':
button.addEventListener('click', () => copyFilteredLogs(['NETWORKERROR'], 'Network Errors'));
break;
case 'btnCORSErrors':
button.addEventListener('click', () => copyFilteredLogs(['CORSERROR'], 'CORS Errors'));
break;
case 'btnSecurityWarnings':
button.addEventListener('click', () => copyFilteredLogs(['SECURITYWARNING'], 'Security Warnings'));
break;
case 'btnExceptions':
button.addEventListener('click', () => copyFilteredLogs(['EXCEPTION'], 'Exceptions'));
break;
case 'btnCopyAllErrors':
button.addEventListener('click', () => copyGroupedLogs('copyAllErrors', 'All Errors'));
break;
case 'btnCopyAllWarnings':
button.addEventListener('click', () => copyGroupedLogs('copyAllWarnings', 'All Warnings'));
break;
default:
break;
}
}
});
// Get the current active tab
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
if (tabs.length === 0) {
console.error('No active tabs found.');
return;
}
currentTabId = tabs[0].id;
console.log(`Popup detected active tab: ${currentTabId}`);
chrome.runtime.sendMessage({action: 'getStatus', tabId: currentTabId}, function(response) {
if (chrome.runtime.lastError) {
console.error('Error querying status:', chrome.runtime.lastError.message);
showNotification('Failed to retrieve capturing status.');
return;
}
if (response.isCapturing) {
const toggleLabel = document.getElementById('toggleCaptureLabel');
const captureButton = document.getElementById('toggleCapture');
if (toggleLabel && captureButton) {
toggleLabel.textContent = 'Stop Capturing';
captureButton.classList.remove('btn-start');
captureButton.classList.add('btn-stop');
}
}
updateLogsDisplay();
});
});
// Listen for messages from background.js to display notifications
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
console.log('Popup received message:', request);
if (request.action === 'logsCleared') {
console.log('Popup clearing logs display.');
updateLogsDisplay();
}
if (request.action === 'displayNotification') {
showNotification(request.message);
}
});
});
function toggleCapture() {
console.log('Toggle Capture button clicked.');
chrome.runtime.sendMessage({action: 'toggleCapture', tabId: currentTabId}, function(response) {
console.log(`Received response from toggleCapture: ${JSON.stringify(response)}`);
const buttonLabel = document.getElementById('toggleCaptureLabel');
const captureButton = document.getElementById('toggleCapture');
if (response.isCapturing) {
if (buttonLabel && captureButton) {
buttonLabel.textContent = 'Stop Capturing';
captureButton.classList.remove('btn-start');
captureButton.classList.add('btn-stop');
}
showNotification('Capturing logs...');
} else {
if (buttonLabel && captureButton) {
buttonLabel.textContent = 'Start Capturing';
captureButton.classList.remove('btn-stop');
captureButton.classList.add('btn-start');
}
showNotification('Stopped capturing logs.');
// Reset the badge counts when stopping capture
resetBadgeCounts();
// Clear the logs display
chrome.runtime.sendMessage({action: 'clearLogs', tabId: currentTabId}, function() {
updateLogsDisplay();
});
}
updateLogsDisplay();
});
}
function resetBadgeCounts() {
const badgeIds = [
'countErrors',
'countWarnings',
'countLogs',
'countInfo',
'countDebug',
'countDeprecationWarnings',
'countNetworkErrors',
'countCORSErrors',
'countSecurityWarnings',
'countExceptions',
'countCopyAllErrors',
'countCopyAllWarnings'
];
badgeIds.forEach(id => {
const badge = document.getElementById(id);
if (badge) {
badge.textContent = 0;
toggleBadgeDisplay(id, 0);
}
});
}
const updateLogsDisplay = debounce(function() {
console.log('Updating logs display in popup.');
chrome.runtime.sendMessage({action: 'getLogs', tabId: currentTabId}, function(response) {
const logsElement = document.getElementById('logs');
if (!logsElement) {
console.error('Logs element not found in popup.');
return;
}
logsElement.textContent = '';
if (response.logs) {
const allLogs = response.logs.map(log => log.message);
logsElement.textContent = allLogs.join('\n');
logsElement.scrollTop = logsElement.scrollHeight;
updateLogCounts(response.logs);
}
});
}, 300); // Debounce with 300ms delay
function updateLogCounts(logs) {
const counts = {
'ERROR': 0,
'WARNING': 0,
'LOG': 0,
'INFO': 0,
'DEBUG': 0,
'DEPRECATIONWARNING': 0,
'NETWORKERROR': 0,
'CORSERROR': 0,
'SECURITYWARNING': 0,
'EXCEPTION': 0
};
logs.forEach(log => {
const level = log.level.toUpperCase();
if (counts.hasOwnProperty(level)) {
counts[level]++;
}
});
// Update individual counts
const countMappings = {
'countErrors': counts['ERROR'] + counts['EXCEPTION'] + counts['NETWORKERROR'] + counts['CORSERROR'],
'countWarnings': counts['WARNING'] + counts['DEPRECATIONWARNING'] + counts['SECURITYWARNING'],
'countLogs': counts['LOG'],
'countInfo': counts['INFO'],
'countDebug': counts['DEBUG'],
'countDeprecationWarnings': counts['DEPRECATIONWARNING'],
'countNetworkErrors': counts['NETWORKERROR'],
'countCORSErrors': counts['CORSERROR'],
'countSecurityWarnings': counts['SECURITYWARNING'],
'countExceptions': counts['EXCEPTION'],
'countCopyAllErrors': counts['ERROR'] + counts['EXCEPTION'] + counts['NETWORKERROR'] + counts['CORSERROR'],
'countCopyAllWarnings': counts['WARNING'] + counts['DEPRECATIONWARNING'] + counts['SECURITYWARNING']
};
for (const [id, count] of Object.entries(countMappings)) {
const badge = document.getElementById(id);
if (badge) {
badge.textContent = count;
toggleBadgeDisplay(id, count);
}
}
}
function toggleBadgeDisplay(badgeId, count) {
const badge = document.getElementById(badgeId);
if (badge) {
if (count > 0) {
badge.classList.add('show');
badge.textContent = count;
} else {
badge.classList.remove('show');
badge.textContent = 0;
}
}
}
function copyFilteredLogs(levels, categoryName) {
console.log(`Copying filtered logs for category: ${categoryName}`);
chrome.runtime.sendMessage({action: 'getLogs', tabId: currentTabId}, function(response) {
console.log('Received logs for copying:', response.logs);
if (response.logs && response.logs.length > 0) {
const filteredLogs = response.logs.filter(log => levels.includes(log.level.toUpperCase()));
if (filteredLogs.length === 0) {
showNotification(`No ${categoryName} to copy.`);
return;
}
// Structure the logs into categorized JSON
const categorizedLogs = {
[categoryName]: filteredLogs.map(log => ({
timestamp: log.timestamp,
message: log.message
}))
};
const jsonOutput = JSON.stringify(categorizedLogs, null, 2);
navigator.clipboard.writeText(jsonOutput).then(() => {
showNotification(`${categoryName} copied to clipboard!`);
}).catch((err) => {
showNotification('Failed to copy logs: ' + err);
});
} else {
showNotification('No logs to copy.');
}
});
}
function copyGroupedLogs(groupName, displayName) {
console.log(`Copying grouped logs for: ${displayName}`);
if (!groupedCopyOptions[groupName]) {
showNotification('Invalid copy option.');
return;
}
const levels = groupedCopyOptions[groupName];
chrome.runtime.sendMessage({action: 'getLogs', tabId: currentTabId}, function(response) {
console.log('Received logs for grouped copying:', response.logs);
if (response.logs && response.logs.length > 0) {
const filteredLogs = response.logs.filter(log => levels.includes(log.level.toUpperCase()));
if (filteredLogs.length === 0) {
showNotification(`No logs found for ${displayName}.`);
return;
}
// Structure the logs into categorized JSON
const categorizedLogs = {
[displayName]: filteredLogs.map(log => ({
timestamp: log.timestamp,
level: log.level,
message: log.message
}))
};
const jsonOutput = JSON.stringify(categorizedLogs, null, 2);
navigator.clipboard.writeText(jsonOutput).then(() => {
showNotification(`${displayName} copied to clipboard!`);
}).catch((err) => {
showNotification('Failed to copy logs: ' + err);
});
} else {
showNotification('No logs to copy.');
}
});
}
function showNotification(message) {
console.log(`Showing notification: ${message}`);
const notification = document.getElementById('notification');
if (!notification) {
console.error('Notification element not found in popup.');
return;
}
notification.textContent = message;
notification.classList.add('show');
setTimeout(() => {
notification.classList.remove('show');
console.log(`Notification hidden: ${message}`);
}, 2000);
}