-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
78 lines (64 loc) · 2.37 KB
/
script.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
document.getElementById('mobile-menu').addEventListener('click', function() {
document.querySelector('.navbar-menu').classList.toggle('active');
});
function stripMarkdown(text) {
// Remove Markdown characters
return text
.replace(/[*_~`]/g, '') // Remove *, _, ~, `
.replace(/\*\*/g, '') // Remove **
.replace(/##/g, '') // Remove ##
.replace(/!\[.*?\]\(.*?\)/g, '') // Remove images
.replace(/\[.*?\]\(.*?\)/g, '') // Remove links
.replace(/^>/gm, '') // Remove blockquotes
.replace(/^\s*[-+*]\s+/gm, '') // Remove list items
.replace(/^\d+\.\s+/gm, ''); // Remove ordered list items
}
async function sendMessage() {
const userInput = document.getElementById("user-input").value;
if (!userInput) return;
const chatBox = document.getElementById("chat-box");
const userMessage = document.createElement("div");
userMessage.style.background="#f0e6db";
userMessage.style.marginBottom="1rem"
userMessage.style.marginTop="1rem"
userMessage.style.padding="1rem"
userMessage.style.borderRadius="10px"
userMessage.innerHTML = `<b>You:</b> ${userInput}`;
chatBox.appendChild(userMessage);
try {
const response = await fetch('/api/send-message', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ message: userInput }),
});
const result = await response.json();
const modelMessage = document.createElement("div");
modelMessage.style.background="#E7D6C5"
modelMessage.style.padding="1rem"
modelMessage.style.marginBottom="1rem"
modelMessage.style.borderRadius="10px"
modelMessage.innerHTML = `<b>MARINO⚓:</b> ${stripMarkdown(escapeHTML(result.response))}`;
chatBox.appendChild(modelMessage);
} catch (error) {
console.error('Error:', error);
const errorMessage = document.createElement("div");
errorMessage.style.color="yellow"
errorMessage.innerHTML = `<b>Error:</b> Unable to get response from the server.`;
chatBox.appendChild(errorMessage);
} finally {
document.getElementById("user-input").value = "";
}
}
// Helper function to escape HTML
function escapeHTML(str) {
const div = document.createElement('div');
div.innerText = str;
return div.innerHTML;
}
document.getElementById("user-input").addEventListener("keypress", function(event) {
if (event.key === "Enter") {
sendMessage();
}
});