-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinline_script_1.js
More file actions
321 lines (135 loc) · 6.12 KB
/
inline_script_1.js
File metadata and controls
321 lines (135 loc) · 6.12 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
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
// Enable input and button initially
document.getElementById('AIUserInput').disabled = false;
document.getElementById('AISendButton').disabled = false;
// Add event listeners
document.getElementById('AISendButton').addEventListener('click', sendMessage);
document.getElementById('AIUserInput').addEventListener('keypress', function (e) {
if (e.key === 'Enter') {
sendMessage();
}
});
// Function to parse rich text and convert it to HTML
function parseRichText(response) {
// Replace **bold** with <strong>bold</strong>
response = response.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');
// Replace *italic* with <em>italic</em>
response = response.replace(/\*(.*?)\*/g, '<em>$1</em>');
// Replace _underline_ with <u>underline</u>
response = response.replace(/_(.*?)_/g, '<u>$1</u>');
// Replace bullet points with <ul> and <li>
response = response.replace(/(\n\s*-\s+.*)+/g, (match) => {
const items = match.split('\n').filter(line => line.trim() !== '');
const listItems = items.map(item => `<li>${item.replace('- ', '').trim()}</li>`).join('');
return `<ul>${listItems}</ul>`;
});
// Replace numbered lists with <ol> and <li>
response = response.replace(/(\n\s*\d+\.\s+.*)+/g, (match) => {
const items = match.split('\n').filter(line => line.trim() !== '');
const listItems = items.map(item => `<li>${item.replace(/^\d+\.\s*/, '').trim()}</li>`).join('');
return `<ol>${listItems}</ol>`;
});
// Replace paragraphs with <p>
response = response.replace(/(\n\n)/g, '</p><p>');
response = `<p>${response}</p>`;
// Replace titles with <h3>
response = response.replace(/(\n#\s+.*)/g, (match) => {
return `<h3>${match.replace('#', '').trim()}</h3>`;
});
// Check if the message contains a table
if (response.includes('|')) {
const tableRegex = /(\|.*\|(\n\|.*\|)+)/g; // Match the entire table block
const tableMatches = response.match(tableRegex);
if (tableMatches) {
// Process only the first occurrence of the table
const tableBlock = tableMatches[0];
const rows = tableBlock.split('\n').filter(row => row.trim() !== '');
const tableHTML = `<table class="table table-bordered table-striped">${rows
.map((row) => {
const cells = row.split('|').filter((cell) => cell.trim() !== '');
return `<tr>${cells.map((cell) => `<td>${cell.trim()}</td>`).join('')}</tr>`;
})
.join('')}</table>`;
// Replace the entire table block with the generated HTML
response = response.replace(tableRegex, tableHTML);
}
}
return response;
}
// Function to send a message
function sendMessage() {
const userInput = document.getElementById('AIUserInput');
const chatBox = document.getElementById('AIChatBox');
const sendButton = document.getElementById('AISendButton');
const loadingIcon = document.getElementById('AILoading');
if (userInput.value.trim() === '') return;
// Disable input and button
userInput.disabled = true;
sendButton.disabled = true;
// Show loading icon
loadingIcon.style.display = 'block';
// Display user's message
const userMessage = document.createElement('div');
userMessage.classList.add('AIMessage', 'AIUserMessage');
userMessage.textContent = userInput.value;
chatBox.appendChild(userMessage);
// Clear input
const question = userInput.value;
userInput.value = '';
// Scroll to bottom
chatBox.scrollTop = chatBox.scrollHeight;
// Send question to Gemini AI
fetchGeminiResponse(question)
.then(response => {
// Parse rich text and convert to HTML
const formattedResponse = parseRichText(response);
// Display AI's response
const botMessage = document.createElement('div');
botMessage.classList.add('AIMessage', 'AIBotMessage');
botMessage.innerHTML = formattedResponse;
// Add copy icon
const copyIcon = document.createElement('i');
copyIcon.className = 'fas fa-copy AICopyIcon';
copyIcon.onclick = () => {
// Copy the entire bot message to clipboard
const range = document.createRange();
range.selectNodeContents(botMessage);
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
document.execCommand('copy');
selection.removeAllRanges();
alert('Copied to clipboard!');
};
botMessage.appendChild(copyIcon);
chatBox.appendChild(botMessage);
// Scroll to bottom
chatBox.scrollTop = chatBox.scrollHeight;
})
.catch(error => {
console.error('Error:', error);
})
.finally(() => {
// Hide loading icon and re-enable input and button
loadingIcon.style.display = 'none';
userInput.disabled = false;
sendButton.disabled = false;
});
}
// Function to fetch Gemini AI response
async function fetchGeminiResponse(question) {
const apiKey = 'AIzaSyBBNlnMeK1nX7m-WCum4PALM9bEpZXmKKQ'; // Replace with your actual API key
const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=${apiKey}`;
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
contents: [{
parts: [{ text: question }]
}]
})
});
const data = await response.json();
return data.candidates[0].content.parts[0].text;
}