-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
521 lines (454 loc) · 20.2 KB
/
Copy pathapp.js
File metadata and controls
521 lines (454 loc) · 20.2 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
// Slanglate - Main Application Logic
document.addEventListener('DOMContentLoaded', () => {
// DOM Elements
const inputText = document.getElementById('inputText');
const outputText = document.getElementById('outputText');
const translateBtn = document.getElementById('translateBtn');
const swapBtn = document.getElementById('swapBtn');
const clearBtn = document.getElementById('clearBtn');
const copyBtn = document.getElementById('copyBtn');
const charCount = document.getElementById('charCount');
const inputLabel = document.getElementById('inputLabel');
const outputLabel = document.getElementById('outputLabel');
const slangToEnglish = document.getElementById('slangToEnglish');
const englishToSlang = document.getElementById('englishToSlang');
const trendingChips = document.getElementById('trendingChips');
const dictionaryGrid = document.getElementById('dictionaryGrid');
const searchInput = document.getElementById('searchInput');
const speakBtn = document.getElementById('speakBtn');
const voiceBtn = document.getElementById('voiceBtn');
const floatingEmojis = document.getElementById('floatingEmojis');
const categoryFilters = document.getElementById('categoryFilters');
const loadMoreBtn = document.getElementById('loadMoreBtn');
const historySection = document.getElementById('historySection');
const historyList = document.getElementById('historyList');
const clearHistoryBtn = document.getElementById('clearHistoryBtn');
const termCount = document.getElementById('termCount');
let isSlangToEnglish = true;
let currentCategory = 'all';
let displayedItems = 12;
let translationHistory = JSON.parse(localStorage.getItem('slangHistory') || '[]');
// Initialize floating emojis background
function initFloatingEmojis() {
const emojis = ['💯', '🔥', '💀', '😭', '🗣️', '✨', '🧢', '💅', '🥀', '😏', '💚', '🎀'];
for (let i = 0; i < 15; i++) {
const emoji = document.createElement('div');
emoji.className = 'floating-emoji';
emoji.textContent = emojis[Math.floor(Math.random() * emojis.length)];
emoji.style.left = Math.random() * 100 + '%';
emoji.style.top = Math.random() * 100 + '%';
emoji.style.animationDelay = Math.random() * 20 + 's';
emoji.style.animationDuration = (15 + Math.random() * 10) + 's';
floatingEmojis.appendChild(emoji);
}
}
// Initialize trending chips
function initTrendingChips() {
trendingChips.innerHTML = '';
trendingSlang.forEach(item => {
const chip = document.createElement('div');
chip.className = 'trending-chip';
chip.innerHTML = `<span class="chip-emoji">${item.emoji}</span><span>${item.term}</span>`;
chip.addEventListener('click', () => {
inputText.value = item.term;
updateCharCount();
performTranslation();
});
trendingChips.appendChild(chip);
});
}
// Get filtered dictionary entries
function getFilteredEntries(filter = '', category = 'all') {
const lowerFilter = filter.toLowerCase();
return Object.entries(slangDictionary)
.filter(([term, data]) => {
// Category filter
if (category !== 'all') {
const hasCategory = data.tags.some(tag =>
tag.toLowerCase() === category.toLowerCase() ||
(category === 'trending' && (tag.includes('2024') || tag.includes('2023') || tag === 'trending'))
);
if (!hasCategory) return false;
}
// Search filter
if (!filter) return true;
return term.toLowerCase().includes(lowerFilter) ||
data.definition.toLowerCase().includes(lowerFilter) ||
data.tags.some(tag => tag.toLowerCase().includes(lowerFilter));
});
}
// Initialize dictionary cards
function initDictionary(filter = '', category = 'all', limit = 12) {
dictionaryGrid.innerHTML = '';
const entries = getFilteredEntries(filter, category);
// Update term count
termCount.textContent = `(${entries.length} terms)`;
entries.slice(0, limit).forEach(([term, data]) => {
const card = document.createElement('div');
card.className = 'dictionary-card fade-in';
card.innerHTML = `
<div class="card-header">
<span class="slang-term">${term}</span>
<span class="card-emoji">${data.emoji}</span>
</div>
<p class="slang-definition">${data.definition}</p>
<p class="slang-example">${data.example}</p>
<div class="card-tags">
${data.tags.map(tag => `<span class="card-tag" data-tag="${tag}">${tag}</span>`).join('')}
</div>
`;
card.addEventListener('click', () => {
inputText.value = term;
updateCharCount();
performTranslation();
window.scrollTo({ top: 0, behavior: 'smooth' });
});
dictionaryGrid.appendChild(card);
});
// Show/hide load more button
loadMoreBtn.style.display = entries.length > limit ? 'block' : 'none';
}
// Update character count
function updateCharCount() {
const count = inputText.value.length;
charCount.textContent = count;
if (count > 450) {
charCount.style.color = '#f59e0b';
} else if (count >= 500) {
charCount.style.color = '#ef4444';
} else {
charCount.style.color = '';
}
}
// Translate slang to English
// Random slang generator
function randomSlang() {
const terms = Object.keys(slangDictionary);
let randomTerm = terms[Math.floor(Math.random() * terms.length)];
let phrase = "";
// Sometimes make a longer sentence
if (Math.random() > 0.5) {
const openers = ["yo", "bro", "honestly", "ngl", "lowkey"];
const opener = openers[Math.floor(Math.random() * openers.length)];
phrase = `${opener} ${randomTerm} fr fr`;
} else {
phrase = `${randomTerm} is crazy`;
}
// Or just use the example from dictionary
if (Math.random() > 0.3) {
phrase = slangDictionary[randomTerm].example;
}
inputText.value = phrase;
updateCharCount();
performTranslation();
}
// Initialize Random Button
const randomBtn = document.getElementById('randomBtn');
if(randomBtn) randomBtn.addEventListener('click', randomSlang);
// Translate slang to English
function translateSlangToEnglish(text) {
if (!text.trim()) return '';
let result = text;
const foundTerms = [];
const replacements = [];
// Sort by length (longest first) to match longer phrases before shorter ones
const sortedTerms = Object.keys(slangDictionary).sort((a, b) => b.length - a.length);
// First pass: find all matches and store them
sortedTerms.forEach(term => {
const regex = new RegExp(`\\b${escapeRegex(term)}\\b`, 'gi');
let match;
while ((match = regex.exec(text)) !== null) {
// Check if this position overlaps with existing replacements
const overlaps = replacements.some(r =>
(match.index >= r.start && match.index < r.end) ||
(match.index + match[0].length > r.start && match.index + match[0].length <= r.end)
);
if (!overlaps) {
const data = slangDictionary[term];
replacements.push({
start: match.index,
end: match.index + match[0].length,
original: match[0],
replacement: data.reverse[0],
definition: data.definition.replace(/"/g, "'")
});
foundTerms.push({ term, definition: data.definition });
}
}
});
// Sort replacements by position (reverse order for safe replacement)
replacements.sort((a, b) => b.start - a.start);
// Second pass: apply replacements from end to start
result = text;
replacements.forEach(r => {
const highlighted = `<span class="highlight-word" title="${r.definition}">${r.replacement}</span>`;
result = result.substring(0, r.start) + highlighted + result.substring(r.end);
});
if (foundTerms.length === 0) {
return `<span class="translated-text">${text}</span><br><br><em style="color: var(--text-muted);">No slang detected. Try words like: rizz, lowkey, no cap, bussin, etc.</em>`;
}
return `<span class="translated-text">${result}</span>`;
}
// Translate English to slang
function translateEnglishToSlang(text) {
if (!text.trim()) return '';
let result = text;
const foundTerms = [];
// Check each slang term's reverse translations
Object.entries(slangDictionary).forEach(([slangTerm, data]) => {
data.reverse.forEach(englishPhrase => {
const regex = new RegExp(`\\b${escapeRegex(englishPhrase)}\\b`, 'gi');
if (regex.test(result)) {
result = result.replace(regex, `<span class="highlight-word" title="${data.definition}">${slangTerm}</span>`);
foundTerms.push({ term: slangTerm, definition: data.definition });
}
});
});
if (foundTerms.length === 0) {
return `<span class="translated-text">${text}</span><br><br><em style="color: var(--text-muted);">Try phrases like: "no lie", "really good", "suspicious", "for real", etc.</em>`;
}
return `<span class="translated-text">${result}</span>`;
}
// Escape special regex characters
function escapeRegex(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
// Add to history
function addToHistory(original, translated, direction) {
const historyItem = {
original: original.substring(0, 100),
translated: translated.replace(/<[^>]*>/g, '').substring(0, 100),
direction: direction,
time: new Date().toISOString()
};
translationHistory.unshift(historyItem);
if (translationHistory.length > 10) {
translationHistory = translationHistory.slice(0, 10);
}
localStorage.setItem('slangHistory', JSON.stringify(translationHistory));
renderHistory();
}
// Render history
function renderHistory() {
if (translationHistory.length === 0) {
historySection.style.display = 'none';
return;
}
historySection.style.display = 'block';
historyList.innerHTML = '';
translationHistory.forEach((item, index) => {
const historyEl = document.createElement('div');
historyEl.className = 'history-item';
const timeAgo = getTimeAgo(new Date(item.time));
historyEl.innerHTML = `
<span class="history-direction">${item.direction === 'slang-to-english' ? '🧢→📖' : '📖→🧢'}</span>
<div class="history-content">
<div class="history-original">${item.original}</div>
<div class="history-translated">${item.translated}</div>
</div>
<span class="history-time">${timeAgo}</span>
`;
historyEl.addEventListener('click', () => {
inputText.value = item.original;
updateCharCount();
performTranslation();
});
historyList.appendChild(historyEl);
});
}
// Get relative time
function getTimeAgo(date) {
const seconds = Math.floor((new Date() - date) / 1000);
if (seconds < 60) return 'just now';
if (seconds < 3600) return Math.floor(seconds / 60) + 'm ago';
if (seconds < 86400) return Math.floor(seconds / 3600) + 'h ago';
return Math.floor(seconds / 86400) + 'd ago';
}
// Perform translation
function performTranslation() {
const text = inputText.value.trim();
if (!text) {
outputText.innerHTML = '<span class="placeholder-text">Translation will appear here...</span>';
return;
}
// Add loading state
translateBtn.disabled = true;
translateBtn.innerHTML = '<span class="btn-text">Translating...</span><span class="btn-icon">⏳</span>';
// Simulate slight delay for UX
setTimeout(() => {
let translatedText;
if (isSlangToEnglish) {
translatedText = translateSlangToEnglish(text);
addToHistory(text, translatedText, 'slang-to-english');
} else {
translatedText = translateEnglishToSlang(text);
addToHistory(text, translatedText, 'english-to-slang');
}
outputText.innerHTML = translatedText;
outputText.classList.add('fade-in');
translateBtn.disabled = false;
translateBtn.innerHTML = '<span class="btn-text">Translate</span><span class="btn-icon">✨</span>';
}, 300);
}
// Swap languages
function swapLanguages() {
isSlangToEnglish = !isSlangToEnglish;
if (isSlangToEnglish) {
slangToEnglish.classList.add('active');
englishToSlang.classList.remove('active');
inputLabel.textContent = 'Enter Slang';
outputLabel.textContent = 'Translation';
inputText.placeholder = "Type slang here... (e.g., 'no cap that's lowkey fire fr fr')";
} else {
slangToEnglish.classList.remove('active');
englishToSlang.classList.add('active');
inputLabel.textContent = 'Enter English';
outputLabel.textContent = 'Slang Translation';
inputText.placeholder = "Type normal English... (e.g., 'that's really good for real')";
}
// Swap the text if there's content
const currentOutput = outputText.textContent;
if (currentOutput && !currentOutput.includes('Translation will appear') && !currentOutput.includes('No slang detected') && !currentOutput.includes('Try phrases')) {
inputText.value = currentOutput;
updateCharCount();
}
outputText.innerHTML = '<span class="placeholder-text">Translation will appear here...</span>';
}
// Copy to clipboard
function copyToClipboard() {
const text = outputText.textContent;
if (text && !text.includes('Translation will appear')) {
navigator.clipboard.writeText(text).then(() => {
showToast('✅', 'Copied to clipboard!');
copyBtn.innerHTML = `
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<polyline points="20 6 9 17 4 12"/>
</svg>
<span class="copy-text">Copied!</span>
`;
setTimeout(() => {
copyBtn.innerHTML = `
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<rect x="9" y="9" width="13" height="13" rx="2" ry="2"/>
<path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/>
</svg>
<span class="copy-text">Copy</span>
`;
}, 2000);
});
}
}
// Clear input
function clearInput() {
inputText.value = '';
outputText.innerHTML = '<span class="placeholder-text">Translation will appear here...</span>';
updateCharCount();
inputText.focus();
}
// Show toast notification
function showToast(icon, message) {
let toast = document.querySelector('.toast');
if (!toast) {
toast = document.createElement('div');
toast.className = 'toast';
document.body.appendChild(toast);
}
toast.innerHTML = `<span class="toast-icon">${icon}</span>${message}`;
toast.classList.add('show');
setTimeout(() => toast.classList.remove('show'), 2500);
}
// Text-to-speech
function speakText() {
const text = outputText.textContent;
if (text && !text.includes('Translation will appear') && 'speechSynthesis' in window) {
const utterance = new SpeechSynthesisUtterance(text);
utterance.rate = 0.9;
speechSynthesis.speak(utterance);
}
}
// Voice input
function startVoiceInput() {
if ('webkitSpeechRecognition' in window || 'SpeechRecognition' in window) {
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
const recognition = new SpeechRecognition();
recognition.lang = 'en-US';
recognition.interimResults = false;
recognition.onstart = () => {
voiceBtn.style.color = '#ef4444';
showToast('🎤', 'Listening...');
};
recognition.onresult = (event) => {
const transcript = event.results[0][0].transcript;
inputText.value = transcript;
updateCharCount();
performTranslation();
};
recognition.onerror = () => {
showToast('❌', 'Voice input failed');
};
recognition.onend = () => {
voiceBtn.style.color = '';
};
recognition.start();
} else {
showToast('❌', 'Voice input not supported');
}
}
// Event Listeners
translateBtn.addEventListener('click', performTranslation);
swapBtn.addEventListener('click', swapLanguages);
clearBtn.addEventListener('click', clearInput);
copyBtn.addEventListener('click', copyToClipboard);
speakBtn.addEventListener('click', speakText);
voiceBtn.addEventListener('click', startVoiceInput);
inputText.addEventListener('input', updateCharCount);
inputText.addEventListener('keydown', (e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
performTranslation();
}
});
slangToEnglish.addEventListener('click', () => {
if (!isSlangToEnglish) swapLanguages();
});
englishToSlang.addEventListener('click', () => {
if (isSlangToEnglish) swapLanguages();
});
searchInput.addEventListener('input', (e) => {
displayedItems = 12;
initDictionary(e.target.value, currentCategory, displayedItems);
});
// Category filter listeners
categoryFilters.addEventListener('click', (e) => {
if (e.target.classList.contains('filter-btn')) {
document.querySelectorAll('.filter-btn').forEach(btn => btn.classList.remove('active'));
e.target.classList.add('active');
currentCategory = e.target.dataset.category;
displayedItems = 12;
initDictionary(searchInput.value, currentCategory, displayedItems);
}
});
// Load more button
loadMoreBtn.addEventListener('click', () => {
displayedItems += 12;
initDictionary(searchInput.value, currentCategory, displayedItems);
});
// Clear history
clearHistoryBtn.addEventListener('click', () => {
translationHistory = [];
localStorage.removeItem('slangHistory');
renderHistory();
showToast('🗑️', 'History cleared');
});
// Limit input length
inputText.addEventListener('input', () => {
if (inputText.value.length > 500) {
inputText.value = inputText.value.slice(0, 500);
}
});
// Initialize
initFloatingEmojis();
initTrendingChips();
initDictionary();
renderHistory();
});