-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.js
More file actions
72 lines (63 loc) · 2.09 KB
/
Copy pathlogger.js
File metadata and controls
72 lines (63 loc) · 2.09 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
// Create a logging utility
const logger = {
logs: [],
lastWrite: Date.now(),
writeInterval: 5000, // Write every 5 seconds
log(...args) {
const timestamp = new Date().toISOString();
const logEntry = `${timestamp}: ${args.join(' ')}`;
this.logs.push(logEntry);
this._scheduleWrite();
},
error(...args) {
const timestamp = new Date().toISOString();
const logEntry = `${timestamp} ERROR: ${args.join(' ')}`;
this.logs.push(logEntry);
this._scheduleWrite();
},
_scheduleWrite() {
const now = Date.now();
if (now - this.lastWrite >= this.writeInterval) {
this._writeLogs();
}
},
async _writeLogs() {
if (this.logs.length === 0) return;
const logText = this.logs.join('\n') + '\n';
const timestamp = new Date().toISOString().slice(0,19).replace(/:/g,'-');
const key = `log_${timestamp}`;
const logData = {
timestamp: timestamp,
content: logText
};
try {
console.log('Attempting to store logs:', {
key,
timestamp,
logLength: logText.length,
entries: this.logs.length
});
// Send logs to background script for storage
const response = await chrome.runtime.sendMessage({
action: "storeLogs",
key: key,
timestamp: timestamp,
logData: logData
});
if (!response || !response.success) {
console.error('Failed to store logs:', response?.error || 'No response');
throw new Error(response?.error || 'Failed to store logs');
}
console.log('Logs stored successfully:', {
key,
response,
timestamp
});
} catch (error) {
console.error('Error writing logs:', error);
} finally {
this.logs = [];
this.lastWrite = Date.now();
}
}
};