-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
172 lines (142 loc) · 5.9 KB
/
Copy pathscript.js
File metadata and controls
172 lines (142 loc) · 5.9 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
162
163
164
165
166
167
168
169
170
171
172
const toggleButton = document.getElementById('theme-toggle');
const currentTheme = localStorage.getItem('theme');
if (currentTheme) {
document.documentElement.setAttribute('data-theme', currentTheme);
if (currentTheme === 'dark') {
toggleButton.textContent = '☀️';
}
}
toggleButton.addEventListener('click', () => {
let theme = document.documentElement.getAttribute('data-theme');
if (theme === 'dark') {
document.documentElement.setAttribute('data-theme', 'light');
localStorage.setItem('theme', 'light');
toggleButton.textContent = '🌙';
} else {
document.documentElement.setAttribute('data-theme', 'dark');
localStorage.setItem('theme', 'dark');
toggleButton.textContent = '☀️';
}
});
// Chatbot Communication Logic
const sendBtn = document.getElementById('send-chat-btn');
const chatInput = document.getElementById('chat-input');
const chatLogs = document.getElementById('chat-logs');
const chatFab = document.getElementById('chat-fab');
const portfolioChat = document.getElementById('portfolio-chat');
const closeChatBtn = document.getElementById('close-chat-btn');
chatFab.addEventListener('click', () => {
portfolioChat.style.display = 'block';
chatFab.style.display = 'none';
});
closeChatBtn.addEventListener('click', () => {
portfolioChat.style.display = 'none';
chatFab.style.display = 'flex';
});
function escapeHTML(str) {
return str.replace(/[&<>'"]/g, tag => ({
'&': '&',
'<': '<',
'>': '>',
"'": ''',
'"': '"'
}[tag]));
}
let isCooldown = false;
async function handleChat() {
if (isCooldown) return;
const rawQuery = chatInput.value.trim();
if (!rawQuery) return;
const query = escapeHTML(rawQuery);
// UI Rate Limiting: Disable inputs for 3 seconds
isCooldown = true;
chatInput.disabled = true;
sendBtn.disabled = true;
sendBtn.style.opacity = '0.7';
sendBtn.style.cursor = 'not-allowed';
setTimeout(() => {
isCooldown = false;
chatInput.disabled = false;
sendBtn.disabled = false;
sendBtn.style.opacity = '1';
sendBtn.style.cursor = 'pointer';
}, 3000);
// Display user message
chatLogs.innerHTML += `<div style="margin-bottom: 8px;"><b>You:</b> ${query}</div>`;
chatInput.value = '';
chatLogs.scrollTop = chatLogs.scrollHeight;
// Add loading indicator
const loadingId = 'loading-' + Date.now();
chatLogs.innerHTML += `<div id="${loadingId}" style="margin-bottom: 8px; color: var(--text-secondary);"><i>Bot is typing...</i></div>`;
chatLogs.scrollTop = chatLogs.scrollHeight;
try {
// Set this to your live deployed worker URL
const res = await fetch('https://portfoliobot.rohitchawda4241.workers.dev/', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message: query })
});
const data = await res.json();
let botReply = data.candidates[0].content.parts[0].text;
botReply = escapeHTML(botReply);
// Simple markdown bold replacement for better UI
botReply = botReply.replace(/\*\*(.*?)\*\*/g, '<b>$1</b>');
// Remove loading indicator
const loader = document.getElementById(loadingId);
if (loader) loader.remove();
// Display Bot response
chatLogs.innerHTML += `<div style="margin-bottom: 8px; color: var(--accent-color);"><b>Bot:</b> ${botReply}</div>`;
} catch (err) {
// Remove loading indicator
const loader = document.getElementById(loadingId);
if (loader) loader.remove();
chatLogs.innerHTML += `<div style="margin-bottom: 8px; color: red;"><b>Error:</b> Could not reach assistant.</div>`;
}
chatLogs.scrollTop = chatLogs.scrollHeight;
}
sendBtn.addEventListener('click', handleChat);
chatInput.addEventListener('keypress', (e) => { if (e.key === 'Enter') handleChat(); });
// Contact Form Logic
const contactForm = document.getElementById('contact-form');
const contactSubmitBtn = document.getElementById('contact-submit-btn');
const contactStatus = document.getElementById('contact-status');
contactForm.addEventListener('submit', async (e) => {
e.preventDefault();
const name = document.getElementById('contact-name').value.trim();
const email = document.getElementById('contact-email').value.trim();
const message = document.getElementById('contact-message').value.trim();
if (!name || !email || !message) return;
// Disable button to prevent double submission
contactSubmitBtn.disabled = true;
contactSubmitBtn.textContent = 'Sending...';
contactSubmitBtn.style.opacity = '0.7';
contactSubmitBtn.style.cursor = 'not-allowed';
contactStatus.style.display = 'none';
try {
const res = await fetch('https://portfolioemail.rohitchawda4241.workers.dev/', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name, email, message })
});
const data = await res.json();
if (res.ok && data.success) {
contactStatus.textContent = '✅ Message sent successfully!';
contactStatus.style.color = 'green';
contactForm.reset();
} else {
contactStatus.textContent = `❌ ${data.error || 'Failed to send message.'}`;
contactStatus.style.color = 'red';
}
} catch (err) {
contactStatus.textContent = '❌ Could not reach the server. Please try again later.';
contactStatus.style.color = 'red';
}
contactStatus.style.display = 'block';
// Re-enable button after 5 seconds
setTimeout(() => {
contactSubmitBtn.disabled = false;
contactSubmitBtn.textContent = 'Send Message';
contactSubmitBtn.style.opacity = '1';
contactSubmitBtn.style.cursor = 'pointer';
}, 5000);
});