-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
303 lines (256 loc) · 8.98 KB
/
app.js
File metadata and controls
303 lines (256 loc) · 8.98 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
let tg = window.Telegram?.WebApp;
if (tg) {
tg.expand();
tg.ready();
}
/* Disable copy/paste and context menu */
document.addEventListener('copy', (e) => e.preventDefault());
document.addEventListener('cut', (e) => e.preventDefault());
document.addEventListener('paste', (e) => e.preventDefault());
document.addEventListener('contextmenu', (e) => e.preventDefault());
document.addEventListener('selectstart', (e) => e.preventDefault());
document.addEventListener('dragstart', (e) => e.preventDefault());
document.addEventListener('keydown', (e) => {
const key = (e.key || '').toLowerCase();
if ((e.ctrlKey || e.metaKey) && ['c', 'x', 'a', 's', 'p'].includes(key)) {
e.preventDefault();
}
});
const amountInput = document.getElementById('amount');
const starsAmount = document.getElementById('stars-amount');
const tonAmount = document.getElementById('ton-amount');
const usdtAmount = document.getElementById('usdt-amount');
const resultCards = document.querySelectorAll('.result-card');
const MAX_INPUT_VALUE = 1000000000;
let currentCurrency = 'stars';
let rates = null;
const STARS_TO_USDT = 0.015;
// Fetch TON rate from external API
async function fetchTonRate() {
try {
const response = await fetch('https://api.split.tg/buy/ton_rate');
if (response.ok) {
const data = await response.json();
if (data.ok) {
return data.message;
} else {
console.error('API error:', data.error_message);
return 1.35; // fallback rate
}
} else {
console.error('Failed to fetch TON rate');
return 1.35; // fallback rate
}
} catch (error) {
console.error('Error fetching TON rate:', error);
return 1.35; // fallback rate
}
}
// Calculate all exchange rates
async function fetchRates() {
try {
const tonRate = await fetchTonRate();
rates = {
stars_to_usdt: STARS_TO_USDT,
stars_to_ton: STARS_TO_USDT / tonRate,
ton_to_usdt: tonRate,
ton_to_stars: tonRate / STARS_TO_USDT,
usdt_to_stars: 1 / STARS_TO_USDT,
usdt_to_ton: 1 / tonRate
};
console.log('Rates updated:', rates);
} catch (error) {
console.error('Error calculating rates:', error);
}
}
function getInputValue() {
const text = amountInput.value.trim();
return text === '' ? '' : text;
}
function setInputValue(value) {
amountInput.value = value;
}
function validateInput(value) {
const numValue = parseFloat(value);
if (numValue > MAX_INPUT_VALUE) {
amountInput.classList.add('error');
// Vibration if supported
if (navigator.vibrate) {
navigator.vibrate([100, 50, 100]);
}
// Remove error class after animation (1 second)
setTimeout(() => {
amountInput.classList.remove('error');
}, 1000);
// Set input to max value
setInputValue(MAX_INPUT_VALUE.toString());
return MAX_INPUT_VALUE;
}
return numValue;
}
function checkInputLimit(value) {
const numValue = parseFloat(value);
if (numValue > MAX_INPUT_VALUE) {
amountInput.classList.add('error');
// Vibration if supported
if (navigator.vibrate) {
navigator.vibrate([100, 50, 100]);
}
// Remove error class after animation
setTimeout(() => {
amountInput.classList.remove('error');
}, 500);
return true;
}
return false;
}
function updateActiveCard(activeCurrency) {
resultCards.forEach(card => {
if (card.dataset.currency === activeCurrency) {
card.classList.add('active');
} else {
card.classList.remove('active');
}
});
}
function formatNumber(num, decimals) {
if (num === 0) return '0';
const fixed = num.toFixed(decimals);
// Remove trailing zeros after decimal point, but keep integer part
return fixed.replace(/(\.\d*?)0+$/, '$1').replace(/\.$/, '');
}
function convert(amount) {
// Always update active card, even without amount
updateActiveCard(currentCurrency);
if (!rates || amount === '' || isNaN(parseFloat(amount))) {
starsAmount.textContent = '0';
tonAmount.textContent = '0';
usdtAmount.textContent = '0';
return;
}
const parsedAmount = validateInput(amount);
let starsValue, tonValue, usdtValue;
switch (currentCurrency) {
case 'stars':
starsValue = parsedAmount;
tonValue = parsedAmount * rates.stars_to_ton;
usdtValue = parsedAmount * rates.stars_to_usdt;
break;
case 'ton':
tonValue = parsedAmount;
starsValue = parsedAmount * rates.ton_to_stars;
usdtValue = parsedAmount * rates.ton_to_usdt;
break;
case 'usdt':
usdtValue = parsedAmount;
tonValue = parsedAmount * rates.usdt_to_ton;
starsValue = parsedAmount * rates.usdt_to_stars;
break;
}
starsAmount.textContent = formatNumber(starsValue, 0);
tonAmount.textContent = formatNumber(tonValue, 4);
usdtAmount.textContent = formatNumber(usdtValue, 4);
}
function filterNumericInput(e) {
// Allow: backspace, delete, tab, escape, enter, arrow keys
if ([8, 9, 27, 13, 46, 37, 38, 39, 40].indexOf(e.keyCode) !== -1 ||
// Allow: Ctrl+A, Ctrl+C, Ctrl+V, Ctrl+X
(e.keyCode === 65 && (e.ctrlKey || e.metaKey)) ||
(e.keyCode === 67 && (e.ctrlKey || e.metaKey)) ||
(e.keyCode === 86 && (e.ctrlKey || e.metaKey)) ||
(e.keyCode === 88 && (e.ctrlKey || e.metaKey))) {
return;
}
// Get current text and selection
const currentText = amountInput.value || '';
const key = e.key;
// Allow numbers and one decimal point
if (!/[0-9.]/.test(key)) {
e.preventDefault();
return;
}
// Prevent multiple decimal points
if (key === '.' && currentText.includes('.')) {
e.preventDefault();
return;
}
// Check if adding this character would exceed the limit
if (/[0-9]/.test(key)) {
const testValue = parseFloat(currentText + key);
if (testValue > MAX_INPUT_VALUE) {
checkInputLimit(testValue.toString());
e.preventDefault();
return;
}
}
}
async function init() {
await fetchRates();
// Handle input events for input field
amountInput.addEventListener('input', () => {
// Clean up non-numeric characters in real-time
let currentText = amountInput.value || '';
let cleanText = currentText.replace(/[^0-9.]/g, '');
// Ensure only one decimal point
const parts = cleanText.split('.');
if (parts.length > 2) {
cleanText = parts[0] + '.' + parts.slice(1).join('');
}
// Check if input exceeds limit
const numValue = parseFloat(cleanText);
if (numValue > MAX_INPUT_VALUE) {
// Show error and vibration
checkInputLimit(cleanText);
// Set to max value
cleanText = MAX_INPUT_VALUE.toString();
}
// Update content if it was cleaned
if (currentText !== cleanText) {
const cursorPos = amountInput.selectionStart;
amountInput.value = cleanText;
// Restore cursor position
const newPos = Math.min(cursorPos, cleanText.length);
amountInput.setSelectionRange(newPos, newPos);
}
const value = getInputValue();
convert(value);
});
// Filter numeric input
amountInput.addEventListener('keydown', filterNumericInput);
// Handle paste events
amountInput.addEventListener('paste', (e) => {
e.preventDefault();
const paste = (e.clipboardData || window.clipboardData).getData('text');
let numericPaste = paste.replace(/[^0-9.]/g, '');
// Ensure only one decimal point
const parts = numericPaste.split('.');
if (parts.length > 2) {
numericPaste = parts[0] + '.' + parts.slice(1).join('');
}
if (numericPaste) {
// Clear current content and insert new
amountInput.textContent = numericPaste;
// Move cursor to end
const range = document.createRange();
const sel = window.getSelection();
range.selectNodeContents(amountInput);
range.collapse(false);
sel.removeAllRanges();
sel.addRange(range);
// Trigger input event
const inputEvent = new Event('input', { bubbles: true });
amountInput.dispatchEvent(inputEvent);
}
});
resultCards.forEach(card => {
card.addEventListener('click', () => {
currentCurrency = card.dataset.currency;
const value = getInputValue();
convert(value);
});
});
// Set initial state
updateActiveCard(currentCurrency);
convert(getInputValue() || '0');
}
init();