lollms-webui/static/js/db_export.js

55 lines
2.0 KiB
JavaScript
Raw Normal View History

2023-04-13 20:44:26 +00:00
function db_export(){
const exportButton = document.getElementById('export-button');
2023-04-15 13:57:23 +00:00
exportButton.title = "Export database";
2023-04-13 20:44:26 +00:00
exportButton.addEventListener('click', () => {
const messages = Array.from(chatWindow.querySelectorAll('.message')).map(messageElement => {
const senderElement = messageElement.querySelector('.sender');
const messageTextElement= messageElement.querySelector('.message-text');
const sender = senderElement.textContent;
const messageText = messageTextElement.textContent;
return { sender, messageText };
});
const exportFormat = 'json'; // replace with desired export format
if (exportFormat === 'text') {
const exportText = messages.map(({ sender, messageText }) => `${sender}: ${messageText}`).join('\n');
downloadTextFile(exportText);
} else if (exportFormat === 'json') {
fetch('/export')
.then(response => response.json())
.then(data => {
db_data = JSON.stringify(data)
// Do something with the data, such as displaying it on the page
console.log(db_data);
downloadJsonFile(db_data);
})
.catch(error => {
// Handle any errors that occur
console.error(error);
});
} else {
console.error(`Unsupported export format: ${exportFormat}`);
}
});
function downloadTextFile(text) {
const blob = new Blob([text], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
downloadUrl(url);
}
2023-04-13 20:44:26 +00:00
function downloadJsonFile(json) {
const blob = new Blob([json], { type: 'application/json' });
const url = URL.createObjectURL(blob);
downloadUrl(url);
}
function downloadUrl(url) {
const link = document.createElement('a');
link.href = url;
link.download = 'chat.txt';
link.click();
}
}