-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
chathistory.js
263 lines (261 loc) · 10.4 KB
/
chathistory.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
// chathistory.js
const DB_NAME = 'chatMessagesDB_v3';
const STORE_NAME = 'messages';
const PAGE_SIZE = 100;
const MAX_PAGES = 5;
let db;
let messages = [];
let currentPage = 0;
let totalPages = 0;
let isLoading = false;
const searchInput = document.getElementById('search-input');
const messagesContainer = document.getElementById('messages-container');
const exportButton = document.getElementById('export-button');
const exportFormat = document.getElementById('export-format');
function initDatabase() {
return new Promise((resolve, reject) => {
const request = indexedDB.open(DB_NAME, 3); // Add version number to match db.js
request.onerror = event => reject(event.target.error);
request.onsuccess = event => {
db = event.target.result;
resolve(db);
};
request.onupgradeneeded = event => {
const db = event.target.result;
if (!db.objectStoreNames.contains(STORE_NAME)) {
const store = db.createObjectStore(STORE_NAME, { keyPath: 'id', autoIncrement: true });
store.createIndex('timestamp', 'timestamp');
store.createIndex('user_timestamp', ['chatname', 'timestamp']);
store.createIndex('user_type_timestamp', ['chatname', 'type', 'timestamp']);
}
};
});
}
function loadMessages(page = 0, direction = 'down') {
return new Promise((resolve, reject) => {
const transaction = db.transaction([STORE_NAME], "readonly");
const store = transaction.objectStore(STORE_NAME);
const index = store.index("timestamp");
const loadedMessages = [];
const skipCount = page * PAGE_SIZE;
const cursorDirection = direction === 'down' ? 'prev' : 'next';
const cursorRequest = index.openCursor(null, cursorDirection);
let count = 0;
cursorRequest.onsuccess = (event) => {
const cursor = event.target.result;
if (cursor) {
if (count < skipCount) {
count++;
cursor.continue();
} else if (loadedMessages.length < PAGE_SIZE) {
loadedMessages.push(cursor.value);
cursor.continue();
} else {
resolve(loadedMessages);
}
} else {
resolve(loadedMessages);
}
};
cursorRequest.onerror = (event) => reject(event.target.error);
});
}
function loadMoreMessages(direction) {
if (isLoading) {
return;
}
isLoading = true;
const pageToLoad = direction === 'up' ? currentPage - 1 : currentPage + 1;
loadMessages(pageToLoad, direction)
.then(newMessages => {
if (newMessages.length > 0) {
if (direction === 'up') {
messages = [...newMessages, ...messages];
currentPage--;
} else {
messages = [...messages, ...newMessages];
currentPage++;
}
totalPages++;
removeOldMessages();
renderMessages();
if (direction === 'up') {
const heightDiff = messagesContainer.scrollHeight - messagesContainer.scrollTop;
messagesContainer.scrollTop = messagesContainer.scrollHeight - heightDiff;
}
} else {
}
isLoading = false;
})
.catch(error => {
console.error("Error loading more messages:", error);
isLoading = false;
});
}
function removeOldMessages() {
if (totalPages > MAX_PAGES) {
const messagesToRemove = PAGE_SIZE;
messages = messages.slice(messagesToRemove);
totalPages--;
}
}
function formatTimestamp(timestamp) {
const now = new Date();
const messageDate = new Date(timestamp);
const diffInSeconds = Math.floor((now - messageDate) / 1000);
if (diffInSeconds < 60) {
return 'Just now';
} else if (diffInSeconds < 3600) {
const minutes = Math.floor(diffInSeconds / 60);
return `${minutes}m ago`;
} else if (diffInSeconds < 86400) {
const hours = Math.floor(diffInSeconds / 3600);
return `${hours}h ago`;
} else if (diffInSeconds < 604800) {
const days = Math.floor(diffInSeconds / 86400);
return `${days}d ago`;
} else {
return messageDate.toLocaleDateString();
}
}
function renderMessages() {
const searchTerm = searchInput.value.toLowerCase();
const filteredMessages = messages.filter(message =>
message.chatname.toLowerCase().includes(searchTerm) ||
message.type.toLowerCase().includes(searchTerm) ||
message.chatmessage.toLowerCase().includes(searchTerm)
);
messagesContainer.innerHTML = filteredMessages.map(message => `
<div class="message-wrapper" id="message-${message.id}">
<div class="message">
<img src="${message.chatimg}" alt="Avatar" class="avatar" data-error-hide="message">
<div class="message-content">
<div class="message-header">
<span class="user-name">${message.chatname}</span>
<img src="./sources/images/${message.type}.png" alt="${message.type}" class="type-image" data-error-hide="self">
<span class="timestamp">${formatTimestamp(message.timestamp)}</span>
</div>
<p class="message-text">${message.chatmessage}</p>
${message.contentimg ? `<img src="${message.contentimg}" alt="Content" class="content-image" data-error-hide="self">` : ''}
${message.donation ? `<p class="donation">Donation: ${message.donation}</p>` : ''}
</div>
</div>
</div>
`).join('');
messagesContainer.querySelectorAll('img').forEach(img => {
img.addEventListener('error', handleImageError);
});
checkAndLoadMore();
}
function handleImageError(event) {
const img = event.target;
const errorBehavior = img.getAttribute('data-error-hide');
if (errorBehavior === 'message') {
const messageWrapper = img.closest('.message-wrapper');
if (messageWrapper) {
messageWrapper.style.display = 'none';
}
} else if (errorBehavior === 'self') {
img.style.display = 'none';
}
}
searchInput.addEventListener('input', renderMessages);
messagesContainer.addEventListener('scroll', () => {
const scrollPosition = messagesContainer.scrollTop + messagesContainer.clientHeight;
const totalHeight = messagesContainer.scrollHeight;
const scrollPercentage = (scrollPosition / totalHeight) * 100;
if (scrollPercentage > 80 && !isLoading) {
loadMoreMessages('down');
}
if (messagesContainer.scrollTop === 0 && !isLoading) {
loadMoreMessages('up');
}
});
exportButton.addEventListener('click', () => {
const format = exportFormat.value;
exportMessages(format);
});
function checkAndLoadMore() {
const containerHeight = messagesContainer.clientHeight;
const contentHeight = messagesContainer.scrollHeight;
if (contentHeight <= containerHeight && !isLoading) {
loadMoreMessages('down');
} else {
}
}
function exportMessages(format) {
const searchTerm = searchInput.value.toLowerCase();
const filteredMessages = messages.filter(message =>
message.chatname.toLowerCase().includes(searchTerm) ||
message.type.toLowerCase().includes(searchTerm) ||
message.chatmessage.toLowerCase().includes(searchTerm)
);
let content = '';
let filename = `chat_export_${new Date().toISOString()}.${format}`;
switch (format) {
case 'json':
content = JSON.stringify(filteredMessages, null, 2);
break;
case 'tsv':
content = 'ID\tTimestamp\tUsername\tType\tMessage\tDonation\n' +
filteredMessages.map(m =>
`${m.id}\t${m.timestamp}\t${m.chatname}\t${m.type}\t${m.chatmessage}\t${m.donation || ''}`
).join('\n');
break;
case 'html':
content = `
<html>
<head>
<style>
body { font-family: Arial, sans-serif; }
.message { border-bottom: 1px solid #ccc; padding: 10px; }
.username { font-weight: bold; }
.timestamp { color: #888; font-size: 0.8em; }
</style>
</head>
<body>
${filteredMessages.map(m => `
<div class="message">
<span class="username">${m.chatname}</span>
<span class="timestamp">${new Date(m.timestamp).toLocaleString()}</span>
<p>${m.chatmessage}</p>
${m.donation ? `<p>Donation: ${m.donation}</p>` : ''}
</div>
`).join('')}
</body>
</html>
`;
break;
}
const blob = new Blob([content], { type: 'text/' + format });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = filename;
link.click();
URL.revokeObjectURL(link.href);
}
document.getElementById('export-button').addEventListener('click', () => {
const format = document.getElementById('export-format').value;
exportMessages(format);
});
function loadAllMessages() {
return new Promise((resolve, reject) => {
const transaction = db.transaction([STORE_NAME], "readonly");
const store = transaction.objectStore(STORE_NAME);
const request = store.getAll();
request.onerror = event => reject(event.target.error);
request.onsuccess = event => resolve(event.target.result);
});
}
initDatabase()
.then(result => {
db = result;
return loadMessages();
})
.then(loadedMessages => {
messages = loadedMessages;
totalPages = 1;
renderMessages();
checkAndLoadMore();
})
.catch(error => console.error("Error initializing app:", error));