-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathchat.js
More file actions
270 lines (230 loc) · 8.19 KB
/
chat.js
File metadata and controls
270 lines (230 loc) · 8.19 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
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
264
265
266
267
268
269
270
// ─────────────────────────────
// LOADER & THEME
// ─────────────────────────────
window.addEventListener('load', () => {
setTimeout(() => {
const loader = document.getElementById('loader');
const main = document.getElementById('mainPage');
if (loader && main) {
loader.classList.add('fade-out');
setTimeout(() => {
loader.style.display = 'none';
main.classList.remove('hidden');
}, 500);
}
}, 3200);
});
(function initTheme() {
const btn = document.getElementById('themeToggle');
const saved = localStorage.getItem('theme') || 'dark';
applyTheme(saved);
if (btn) {
btn.addEventListener('click', () => {
const isLight = document.documentElement.classList.contains('light-mode');
applyTheme(isLight ? 'dark' : 'light');
});
}
function applyTheme(theme) {
if (theme === 'light') {
document.documentElement.classList.add('light-mode');
if (btn) btn.textContent = '☀️';
} else {
document.documentElement.classList.remove('light-mode');
if (btn) btn.textContent = '🌙';
}
localStorage.setItem('theme', theme);
}
})();
// ─────────────────────────────
// SCAM DETECTOR LOGIC
// ─────────────────────────────
let conversationHistory = [];
const TEMPLATES = {
job: "Congratulations! Your resume has been selected for the position of Virtual Assistant. Weekly pay is $1,200. No experience needed. To start immediately, you must deposit $150 via Bitcoin for your software training kit. Reply YES if interested.",
phishing: "FedEx ALERT: Your parcel has a pending delivery fee of $2.99. If not paid within 12 hours, the package will be returned to the sender. Please resolve at http://dhl-pending-delivery-fees-support.xyz/status",
otp: "ALERT: Suspected unauthorized transaction of $500 on your account. If this was not you, call our security helpline immediately at 1-800-FAKE-BANK. Our agent will verify your identity. Please prepare to share the 6-digit OTP sent to your mobile phone to lock your card.",
crypto: "Welcome to the official CryptoDouble community! Guaranteed 200% return on deposit in 24 hours. Tested by thousands of members. Send any amount from 0.05 BTC to 2 BTC to our promotional address: 1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa and receive double back instantly. Time is running out!"
};
function fillTemplate(type) {
const input = document.getElementById('chatInput');
if (input && TEMPLATES[type]) {
input.value = TEMPLATES[type];
adjustTextareaHeight(input);
input.focus();
}
}
function adjustTextareaHeight(el) {
el.style.height = 'auto';
el.style.height = (el.scrollHeight) + 'px';
}
function clearChat() {
conversationHistory = [];
const historyDiv = document.getElementById('chatHistory');
if (historyDiv) {
historyDiv.innerHTML = `
<div class="chat-msg robot">
<div class="chat-avatar">🛡️</div>
<div class="chat-bubble">
<strong>Chat Cleared!</strong>
<p class="chat-welcome-text">
Paste any suspicious message above and click Verify to analyze it.
</p>
</div>
</div>
`;
}
}
async function sendMessage() {
const input = document.getElementById('chatInput');
const sendBtn = document.getElementById('sendBtn');
const historyDiv = document.getElementById('chatHistory');
const typing = document.getElementById('typingIndicator');
const text = input.value.trim();
if (!text) return;
// Disable input/button
input.value = '';
input.style.height = 'auto';
input.disabled = true;
sendBtn.disabled = true;
// Append user bubble
appendMessage('user', text);
scrollToBottom();
// Show typing dot indicator
if (typing) {
typing.classList.remove('hidden');
scrollToBottom();
}
try {
const apiHost = (window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1')
? 'http://localhost:3000'
: 'https://cybershield-sxz0.onrender.com';
const response = await fetch(`${apiHost}/api/scam-detect`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
message: text,
history: conversationHistory
})
});
if (!response.ok) {
throw new Error(`Server returned status: ${response.status}`);
}
const report = await response.json();
// Hide typing indicator
if (typing) {
typing.classList.add('hidden');
}
// Append AI bubble with structured report
appendAnalysisReport(report);
scrollToBottom();
// Save history (we serialize our memory)
conversationHistory.push({ role: 'user', text: text });
// Save model's explanation response as the context text
conversationHistory.push({ role: 'model', text: report.explanation });
} catch (err) {
if (typing) {
typing.classList.add('hidden');
}
appendMessage('robot', `⚠️ <strong>Error analyzing message:</strong> ${err.message}. Make sure the backend server is running.`);
scrollToBottom();
} finally {
input.disabled = false;
sendBtn.disabled = false;
input.focus();
}
}
function appendMessage(role, messageText) {
const historyDiv = document.getElementById('chatHistory');
if (!historyDiv) return;
const msgDiv = document.createElement('div');
msgDiv.className = `chat-msg ${role}`;
if (role === 'user') {
msgDiv.innerHTML = `
<div class="chat-bubble user-bubble">
${escapeHTML(messageText)}
</div>
`;
} else {
msgDiv.innerHTML = `
<div class="chat-avatar">🛡️</div>
<div class="chat-bubble">
${messageText}
</div>
`;
}
historyDiv.appendChild(msgDiv);
}
function appendAnalysisReport(report) {
const historyDiv = document.getElementById('chatHistory');
if (!historyDiv) return;
const classification = report.classification || 'safe';
const categories = {
phishing: '🎣 Phishing / Credential Theft',
fake_job: '💼 Fake Job Offer',
otp_fraud: '🛡️ OTP / PIN Fraud',
investment_scam: '📉 Investment Scam',
impersonation: '👤 Impersonation Fraud',
other: '⚠️ Suspicious Request',
none: '✅ Safe Communication'
};
const scamLabel = categories[report.scamType] || 'General Text';
const reportHtml = `
<div class="report-header-row">
<span class="report-badge classification-${classification}">
${classification}
</span>
<span class="report-scam-type">
${scamLabel}
</span>
</div>
<!-- Explanation -->
<p class="report-explanation">
${escapeHTML(report.explanation).replace(/\n/g, '<br>')}
</p>
<!-- Score & Progress -->
<div class="report-score-section">
<div class="report-score-labels">
<span>Threat Confidence</span>
<span>${report.confidence}%</span>
</div>
<div class="report-progress-track">
<div class="report-progress-bar classification-${classification}" style="width: ${report.confidence}%;"></div>
</div>
</div>
<!-- Actionable Advice -->
<div class="report-advice-box classification-${classification}">
<strong class="report-advice-title">Advice:</strong> ${escapeHTML(report.actionableAdvice)}
</div>
`;
const msgDiv = document.createElement('div');
msgDiv.className = 'chat-msg robot';
msgDiv.innerHTML = `
<div class="chat-avatar">🤖</div>
<div class="chat-bubble report-bubble classification-border-${classification}">
${reportHtml}
</div>
`;
historyDiv.appendChild(msgDiv);
}
function scrollToBottom() {
const historyDiv = document.getElementById('chatHistory');
if (historyDiv) {
historyDiv.scrollTop = historyDiv.scrollHeight;
}
}
function escapeHTML(str) {
return str
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
document.getElementById('chatInput').addEventListener('keydown', e => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
sendMessage();
}
});