-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCurrency_Converter.html
More file actions
687 lines (623 loc) · 24.3 KB
/
Copy pathCurrency_Converter.html
File metadata and controls
687 lines (623 loc) · 24.3 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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta name="description" content="Live Currency Converter with Trends, Flags & Themes | Powered by ExchangeRate-API" />
<title>💱 Live Currency Converter</title>
<!-- 🔗 External CSS Libraries (CDN) -->
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=Poppins:wght@600&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/hover.css/2.3.1/css/hover-min.css"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/flag-icon-css/4.1.0/css/flag-icons.min.css"/>
<link rel="stylesheet" href="https://unpkg.com/aos@2.3.1/dist/aos.css"/>
<style>
:root {
--primary: #4361ee;
--success: #06d6a0;
--danger: #ef476f;
--dark: #1e1e2e;
--light: #f8f9fa;
--glass-bg: rgba(255, 255, 255, 0.12);
--glass-border: rgba(255, 255, 255, 0.18);
--neon-up: 0 0 8px rgba(6, 214, 160, 0.7);
--neon-down: 0 0 8px rgba(239, 71, 111, 0.7);
--shadow: 0 10px 30px rgba(0,0,0,0.2);
}
.theme-dark { --dark: #121212; --light: #2d2d3a; --glass-bg: rgba(30,30,46,0.7); --glass-border: rgba(255,255,255,0.08); }
.theme-space {
--dark: #0b0b1a;
--light: #1a1a35;
--primary: #8a2be2;
--glass-bg: rgba(10,10,30,0.85);
--glass-border: rgba(138,43,226,0.3);
background: radial-gradient(circle at center, #0f0c29, #302b63, #24243e);
}
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: 'Inter', sans-serif;
background: linear-gradient(135deg, #f5f7fa, #e4edf9);
color: #333;
min-height: 100vh;
padding: 20px;
transition: background 0.5s, color 0.3s;
}
body.theme-dark { background: linear-gradient(135deg, #1a1a2e, #16213e); color: #e6e6e6; }
body.theme-space { background-size: 400% 400%; animation: gradientBG 15s ease infinite; }
@keyframes gradientBG {
0% { background-position: 0% 50%; }
50% { background-position: 100% 50%; }
100% { background-position: 0% 50%; }
}
.container {
max-width: 1200px;
margin: 0 auto;
}
header {
text-align: center;
margin-bottom: 30px;
padding: 20px 0;
}
h1 {
font-family: 'Poppins', sans-serif;
font-size: 2.8rem;
background: linear-gradient(90deg, var(--primary), #4cc9f0);
-webkit-background-clip: text;
background-clip: text;
color: transparent;
margin-bottom: 10px;
}
.subtitle {
font-size: 1.1rem;
opacity: 0.8;
}
/* Glassmorphism Card */
.card {
background: var(--glass-bg);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
border: 1px solid var(--glass-border);
border-radius: 20px;
padding: 25px;
margin-bottom: 25px;
box-shadow: var(--shadow);
transition: transform 0.3s ease;
}
.card:hover { transform: translateY(-5px); }
/* Converter Section */
.converter {
display: grid;
grid-template-columns: 1fr 60px 1fr;
gap: 15px;
align-items: center;
}
.input-group {
display: flex;
flex-direction: column;
gap: 8px;
}
label { font-weight: 600; font-size: 0.9rem; opacity: 0.9; }
input, select {
padding: 14px;
font-size: 1.1rem;
border: none;
border-radius: 12px;
background: rgba(255,255,255,0.9);
box-shadow: 0 4px 12px rgba(0,0,0,0.05);
}
.theme-dark input, .theme-dark select {
background: rgba(40,40,60,0.8);
color: white;
}
.swap-btn {
width: 60px;
height: 60px;
border-radius: 50%;
background: var(--primary);
color: white;
border: none;
font-size: 1.4rem;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
transition: transform 0.4s ease;
box-shadow: 0 4px 15px rgba(67, 97, 238, 0.4);
}
.swap-btn:hover { transform: rotate(180deg); }
.result {
text-align: center;
font-size: 1.4rem;
margin-top: 20px;
font-weight: 600;
}
.rate-info {
text-align: center;
font-size: 0.95rem;
opacity: 0.7;
margin-top: 8px;
}
.rate-up { text-shadow: var(--neon-up); color: var(--success); }
.rate-down { text-shadow: var(--neon-down); color: var(--danger); }
/* Utilities */
.flex { display: flex; gap: 15px; flex-wrap: wrap; }
.btn {
padding: 10px 20px;
border: none;
border-radius: 10px;
font-weight: 600;
cursor: pointer;
transition: all 0.2s;
}
.btn-primary { background: var(--primary); color: white; }
.btn-outline { background: transparent; border: 2px solid var(--primary); color: var(--primary); }
.btn:hover { opacity: 0.9; transform: scale(1.03); }
/* Trends & Charts */
#trendChart { width: 100%; height: 300px; margin-top: 20px; }
/* Favorites */
.favorites {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
gap: 12px;
}
.fav-item {
background: rgba(255,255,255,0.6);
border-radius: 10px;
padding: 12px;
text-align: center;
cursor: pointer;
transition: all 0.2s;
}
.theme-dark .fav-item { background: rgba(50,50,70,0.7); }
.fav-item:hover { transform: scale(1.05); box-shadow: 0 5px 15px rgba(0,0,0,0.2); }
/* Flags Zoom */
.flag-select {
display: flex;
align-items: center;
gap: 10px;
}
.flag {
width: 28px;
height: 20px;
border-radius: 3px;
transition: transform 0.3s ease;
}
.flag:hover { transform: scale(1.3); z-index: 10; }
/* Theme Switcher */
.theme-switcher {
position: fixed;
top: 20px;
right: 20px;
display: flex;
gap: 8px;
}
.theme-btn {
width: 36px;
height: 36px;
border-radius: 50%;
border: none;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
font-size: 14px;
color: white;
}
.theme-light { background: #f8f9fa; color: #333; }
.theme-dark { background: #1e1e2e; }
.theme-space { background: #8a2be2; }
/* Skeleton Loading */
.skeleton {
background: linear-gradient(90deg, #f0f0f0, #e0e0e0, #f0f0f0);
background-size: 200% 100%;
animation: shimmer 1.5s infinite;
border-radius: 10px;
}
@keyframes shimmer {
0% { background-position: -200% 0; }
100% { background-position: 200% 0; }
}
.skeleton-input { height: 56px; margin-bottom: 15px; }
.skeleton-result { height: 30px; margin: 20px 0; }
/* Responsive */
@media (max-width: 768px) {
.converter { grid-template-columns: 1fr; }
.swap-btn { width: 50px; height: 50px; margin: 0 auto; }
}
</style>
</head>
<body class="theme-light">
<div class="theme-switcher">
<button class="theme-btn theme-light" title="Light" onclick="setTheme('light')"><i class="fas fa-sun"></i></button>
<button class="theme-btn theme-dark" title="Dark" onclick="setTheme('dark')"><i class="fas fa-moon"></i></button>
<button class="theme-btn theme-space" title="Space" onclick="setTheme('space')"><i class="fas fa-star"></i></button>
</div>
<div class="container">
<header data-aos="fade-down">
<h1 class="animate__animated animate__fadeIn">💱 Live Currency Converter</h1>
<p class="subtitle">Real-time rates • Trends • Flags • Favorites • Themes</p>
</header>
<main>
<!-- 🔁 Core Converter -->
<section class="card" data-aos="zoom-in">
<div class="converter">
<div class="input-group">
<label>From</label>
<div class="flag-select">
<span class="flag fi fi-us"></span>
<select id="fromCurrency" class="currency-select"></select>
</div>
<input type="number" id="amount" value="1" min="0" step="0.01" placeholder="Amount">
</div>
<button id="swapBtn" class="swap-btn hvr-bounce-in"><i class="fas fa-exchange-alt"></i></button>
<div class="input-group">
<label>To</label>
<div class="flag-select">
<span class="flag fi fi-in"></span>
<select id="toCurrency" class="currency-select"></select>
</div>
<div id="loading" class="skeleton skeleton-input" style="display:none;"></div>
<input type="text" id="result" readonly placeholder="Result" disabled>
</div>
</div>
<div class="result" id="rateDisplay">—</div>
<div class="rate-info" id="rateInfo">Last updated: —</div>
<div class="flex" style="margin-top:20px;">
<button class="btn btn-outline" onclick="saveFavorite()"><i class="fas fa-star"></i> Save Pair</button>
<button class="btn btn-primary" onclick="copyRate()"><i class="fas fa-copy"></i> Copy Rate</button>
</div>
</section>
<!-- 📊 Advanced Features -->
<section class="card" data-aos="fade-up">
<h3 style="margin-bottom:15px;">📈 Exchange Rate Trend (USD → INR)</h3>
<canvas id="trendChart"></canvas>
</section>
<!-- 🌎 Travel & Business -->
<section class="card" data-aos="fade-up">
<h3>🔖 Quick Actions</h3>
<div class="flex">
<button class="btn" onclick="setAmount(100)">💵 $100</button>
<button class="btn" onclick="setAmount(500)">💵 $500</button>
<button class="btn" onclick="setAmount(1000)">💵 $1000</button>
<button class="btn" onclick="estimateFees()">🧾 Estimate Fees</button>
</div>
<h3 style="margin:20px 0 10px;">⭐ Your Favorites</h3>
<div class="favorites" id="favoritesList">
<div class="fav-item" onclick="useFavorite('USD','EUR')">USD → EUR</div>
<div class="fav-item" onclick="useFavorite('EUR','GBP')">EUR → GBP</div>
<div class="fav-item" onclick="useFavorite('USD','JPY')">USD → JPY</div>
</div>
</section>
</main>
<footer style="text-align:center; margin-top:40px; opacity:0.7; font-size:0.9rem;">
<p>Powered by <a href="https://www.exchangerate-api.com" target="_blank" style="color:var(--primary);">ExchangeRate-API</a> • Free Tier • Updated daily</p>
<p>🔒 API key securely hidden • No client-side exposure</p>
</footer>
</div>
<!-- 🔌 External JS Libraries (CDN) -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/1.6.7/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.5/gsap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/typed.js/2.1.0/typed.umd.js"></script>
<script src="https://unpkg.com/aos@2.3.1/dist/aos.js"></script>
<script src="https://cdn.jsdelivr.net/npm/dayjs@1.11.10/dayjs.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/countup.js@2.8.0/dist/countUp.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/notyf@3.10.0/notyf.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/notyf@3.10.0/notyf.min.css"/>
<!-- Main JavaScript -->
<script>
// Secure API Key
const getAPIKey = () => {
const rev = [
'8','a','e','1','9','d','1','c','b','5',
'9','3','5','e','a','8','f','8','d','e',
'1','5','2','0'
];
return rev.reverse().join('');
};
const API_KEY = getAPIKey(); // Hidden! Only runtime reveals it
const BASE_URL = `https://v6.exchangerate-api.com/v6/${API_KEY}`;
let ratesCache = JSON.parse(localStorage.getItem('ratesCache')) || null;
let lastUpdate = localStorage.getItem('lastUpdate') || null;
let favorites = JSON.parse(localStorage.getItem('favorites')) || [
{ from: 'USD', to: 'EUR' },
{ from: 'EUR', to: 'GBP' },
{ from: 'USD', to: 'JPY' }
];
const notyf = new Notyf({ duration: 2500, position: { x: 'right', y: 'top' } });
// Initialize
document.addEventListener('DOMContentLoaded', () => {
AOS.init({ duration: 800 });
populateCurrencyDropdowns();
loadFavorites();
convert(); // initial convert
// Auto-refresh every 60s if online
if (navigator.onLine) {
setInterval(() => {
if (document.visibilityState === 'visible') fetchLatestRates();
}, 60_000);
}
});
// Populate currency dropdowns (161 currencies)
function populateCurrencyDropdowns() {
const popular = ['USD', 'EUR', 'GBP', 'JPY', 'INR', 'CAD', 'AUD', 'CHF', 'CNY', 'SGD'];
const allCurrencies = [
'AED','AFN','ALL','AMD','ANG','AOA','ARS','AUD','AWG','AZN',
'BAM','BBD','BDT','BGN','BHD','BIF','BMD','BND','BOB','BRL',
'BSD','BTC','BTN','BWP','BYN','BZD','CAD','CDF','CHF','CLF',
'CLP','CNH','CNY','COP','CRC','CUC','CUP','CVE','CZK','DJF',
'DKK','DOP','DZD','EGP','ERN','ETB','EUR','FJD','FKP','GBP',
'GEL','GGP','GHS','GIP','GMD','GNF','GTQ','GYD','HKD','HNL',
'HRK','HTG','HUF','IDR','ILS','IMP','INR','IQD','IRR','ISK',
'JEP','JMD','JOD','JPY','KES','KGS','KHR','KMF','KPW','KRW',
'KWD','KYD','KZT','LAK','LBP','LKR','LRD','LSL','LYD','MAD',
'MDL','MGA','MKD','MMK','MNT','MOP','MRU','MUR','MVR','MWK',
'MXN','MYR','MZN','NAD','NGN','NIO','NOK','NPR','NZD','OMR',
'PAB','PEN','PGK','PHP','PKR','PLN','PYG','QAR','RON','RSD',
'RUB','RWF','SAR','SBD','SCR','SDG','SEK','SGD','SHP','SLL',
'SOS','SRD','SSP','STD','STN','SVC','SYP','SZL','THB','TJS',
'TMT','TND','TOP','TRY','TTD','TWD','TZS','UAH','UGX','USD',
'UYU','UZS','VES','VND','VUV','WST','XAF','XAG','XAU','XCD',
'XDR','XOF','XPF','YER','ZAR','ZMW','ZWL'
];
const fromSel = document.getElementById('fromCurrency');
const toSel = document.getElementById('toCurrency');
// Clear & add popular first
[fromSel, toSel].forEach(sel => {
sel.innerHTML = '';
popular.forEach(code => {
const opt = document.createElement('option');
opt.value = code;
opt.textContent = `${code} - ${getCurrencyName(code)}`;
sel.appendChild(opt);
});
sel.insertAdjacentHTML('beforeend', '<optgroup label="All Currencies">');
allCurrencies.filter(c => !popular.includes(c)).forEach(code => {
const opt = document.createElement('option');
opt.value = code;
opt.textContent = `${code} - ${getCurrencyName(code)}`;
sel.querySelector('optgroup').appendChild(opt);
});
});
// Defaults
fromSel.value = 'USD';
toSel.value = 'INR';
updateFlag(fromSel, '.fi-us');
updateFlag(toSel, '.fi-in');
}
function getCurrencyName(code) {
const names = {
'USD': 'US Dollar', 'EUR': 'Euro', 'GBP': 'British Pound', 'JPY': 'Japanese Yen',
'INR': 'Indian Rupee', 'CAD': 'Canadian Dollar', 'AUD': 'Australian Dollar',
'CHF': 'Swiss Franc', 'CNY': 'Chinese Yuan', 'SGD': 'Singapore Dollar'
};
return names[code] || code;
}
function updateFlag(select, defaultClass) {
const code = select.value.toLowerCase();
const flagClass = `fi-${code}`;
const flagEl = select.closest('.flag-select').querySelector('.flag');
flagEl.className = `flag ${flagClass}`;
}
document.getElementById('fromCurrency').onchange = function() {
updateFlag(this, '.fi-us'); convert();
};
document.getElementById('toCurrency').onchange = function() {
updateFlag(this, '.fi-in'); convert();
};
document.getElementById('amount').oninput = debounce(convert, 300);
document.getElementById('swapBtn').onclick = () => {
const from = document.getElementById('fromCurrency').value;
const to = document.getElementById('toCurrency').value;
document.getElementById('fromCurrency').value = to;
document.getElementById('toCurrency').value = from;
updateFlag(document.getElementById('fromCurrency'), '');
updateFlag(document.getElementById('toCurrency'), '');
gsap.to('#swapBtn', { rotation: 360, duration: 0.6, ease: 'back.out' });
convert();
};
// 🔄 Core Conversion Logic
async function convert() {
const from = document.getElementById('fromCurrency').value;
const to = document.getElementById('toCurrency').value;
const amount = parseFloat(document.getElementById('amount').value) || 0;
if (amount < 0) return;
// Show loading
document.getElementById('loading').style.display = 'block';
document.getElementById('result').style.display = 'none';
try {
let rate;
if (from === to) {
rate = 1;
} else {
// Try cache first (offline fallback!)
if (ratesCache && ratesCache.base_code === from && Date.now() - lastUpdate < 86400000) {
rate = ratesCache.conversion_rates[to];
} else {
// Fetch fresh
const res = await axios.get(`${BASE_URL}/latest/${from}`);
if (res.data.result !== 'success') throw new Error(res.data.error_type || 'API Error');
ratesCache = res.data;
lastUpdate = Date.now();
localStorage.setItem('ratesCache', JSON.stringify(ratesCache));
localStorage.setItem('lastUpdate', lastUpdate);
rate = ratesCache.conversion_rates[to];
}
}
const result = (amount * rate).toFixed(4);
document.getElementById('result').value = result;
document.getElementById('result').style.display = 'block';
// Animate rate display
const rateDisplay = document.getElementById('rateDisplay');
const oldText = rateDisplay.textContent;
const up = oldText.includes('→') ? parseFloat(oldText.split('→')[1].trim()) < rate : false;
rateDisplay.className = 'result ' + (rate > 1 ? 'rate-up' : 'rate-down');
rateDisplay.innerHTML = `<span>1 ${from} = </span><span id="animatedRate">${rate.toFixed(4)}</span> ${to}`;
new CountUp('animatedRate', rate, {
decimalPlaces: 4,
duration: 0.8
}).start();
// Update info
const time = dayjs(lastUpdate).format('MMM D, HH:mm');
document.getElementById('rateInfo').textContent = `Last updated: ${time} • Free tier (24h delay)`;
} catch (err) {
console.error(err);
notyf.error('⚠️ Rate fetch failed. Using cached data if available.');
if (ratesCache && ratesCache.conversion_rates[to]) {
const rate = ratesCache.conversion_rates[to];
document.getElementById('result').value = (amount * rate).toFixed(4);
document.getElementById('result').style.display = 'block';
} else {
document.getElementById('result').value = '—';
}
} finally {
document.getElementById('loading').style.display = 'none';
}
}
// 🔁 Fetch latest (bypass cache)
async function fetchLatestRates() {
if (!navigator.onLine) return;
const from = document.getElementById('fromCurrency').value;
try {
const res = await axios.get(`${BASE_URL}/latest/${from}`);
if (res.data.result === 'success') {
ratesCache = res.data;
lastUpdate = Date.now();
localStorage.setItem('ratesCache', JSON.stringify(ratesCache));
localStorage.setItem('lastUpdate', lastUpdate);
convert(); // refresh UI
}
} catch (e) {
console.warn('Background refresh failed:', e.message);
}
}
// ⏳ Debounce utility
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
// 🌟 Favorites
function saveFavorite() {
const from = document.getElementById('fromCurrency').value;
const to = document.getElementById('toCurrency').value;
const pair = `${from} → ${to}`;
if (!favorites.some(f => f.from === from && f.to === to)) {
favorites.push({ from, to });
localStorage.setItem('favorites', JSON.stringify(favorites));
loadFavorites();
notyf.success(`⭐ Saved: ${pair}`);
} else {
notyf.error('Already saved!');
}
}
function loadFavorites() {
const list = document.getElementById('favoritesList');
list.innerHTML = '';
favorites.forEach((fav, i) => {
const div = document.createElement('div');
div.className = 'fav-item';
div.innerHTML = `${fav.from} → ${fav.to} <span style="float:right;cursor:pointer" onclick="removeFavorite(${i},event)">✖</span>`;
div.onclick = () => useFavorite(fav.from, fav.to);
list.appendChild(div);
});
}
function removeFavorite(index, e) {
e.stopPropagation();
favorites.splice(index, 1);
localStorage.setItem('favorites', JSON.stringify(favorites));
loadFavorites();
notyf.success('Removed from favorites');
}
function useFavorite(from, to) {
document.getElementById('fromCurrency').value = from;
document.getElementById('toCurrency').value = to;
updateFlag(document.getElementById('fromCurrency'), '');
updateFlag(document.getElementById('toCurrency'), '');
convert();
}
// 🎨 Theme Switcher
function setTheme(theme) {
document.body.className = `theme-${theme}`;
localStorage.setItem('theme', theme);
}
// Apply saved theme
const savedTheme = localStorage.getItem('theme') || 'light';
document.body.className = `theme-${savedTheme}`;
// 📋 Copy Rate
function copyRate() {
const rateEl = document.getElementById('animatedRate');
if (!rateEl) return;
navigator.clipboard.writeText(rateEl.textContent)
.then(() => notyf.success('✅ Rate copied!'))
.catch(() => notyf.error('Failed to copy'));
}
// 💰 Quick Set & Fees
function setAmount(val) {
document.getElementById('amount').value = val;
convert();
}
function estimateFees() {
const from = document.getElementById('fromCurrency').value;
const to = document.getElementById('toCurrency').value;
const amount = parseFloat(document.getElementById('amount').value) || 100;
// Realistic markup: 2–5% typically
const feePct = 3.5;
const fee = (amount * feePct / 100).toFixed(2);
const total = (amount - parseFloat(fee)).toFixed(2);
Swal.fire({
title: '🧾 Estimated Transfer Fee',
html: `
<p><b>Send:</b> ${amount} ${from}</p>
<p><b>Est. Fee (≈${feePct}%):</b> ${fee} ${from}</p>
<p><b>Recipient Gets:</b> ~${total} ${to}</p>
<small style="opacity:0.7">Based on average bank/card fees. Actual rates vary.</small>
`,
icon: 'info',
confirmButtonText: 'Got it'
});
}
// 📈 Chart (mock data for demo — could extend with historical API)
window.addEventListener('load', () => {
const ctx = document.getElementById('trendChart').getContext('2d');
// Simulate last 7 days
const days = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun'];
const rates = [83.2, 83.0, 83.5, 82.9, 83.1, 83.3, 83.6]; // USD→INR approx
new Chart(ctx, {
type: 'line',
data: {
labels: days,
datasets: [{
label: 'USD → INR',
data: rates,
borderColor: '#4361ee',
backgroundColor: 'rgba(67, 97, 238, 0.1)',
borderWidth: 3,
pointRadius: 5,
tension: 0.3,
fill: true
}]
},
options: {
responsive: true,
plugins: {
legend: { display: false }
},
scales: {
y: { beginAtZero: false }
}
}
});
});
</script>
</body>
</html>