diff --git a/src/js/main.js b/src/js/main.js index 788e95a..aca7c6b 100644 --- a/src/js/main.js +++ b/src/js/main.js @@ -12,134 +12,134 @@ let lastModifiedField = 'xmr'; const exchangeRates = {}; const runConvert = () => - lastModifiedField === 'xmr' ? xmrConvert() : fiatConvert(); + lastModifiedField === 'xmr' ? xmrConvert() : fiatConvert(); document.addEventListener('DOMContentLoaded', () => { - const copyXMRBtn = document.getElementById('copyXMRBtn'); - const copyFiatBtn = document.getElementById('copyFiatBtn'); - const xmrInput = document.getElementById('xmrInput'); - const fiatInput = document.getElementById('fiatInput'); - const selectBox = document.getElementById('selectBox'); - const convertXMRToFiatBtn = document.getElementById('convertXMRToFiat'); - const convertFiatToXMRBtn = document.getElementById('convertFiatToXMR'); - const fiatButtons = document.querySelectorAll('.fiat-btn'); - - // Add event listeners for the currency buttons - fiatButtons.forEach(button => { - button.addEventListener('click', (e) => { - e.preventDefault(); - selectBox.value = button.textContent; - runConvert(); - history.pushState(null, '', `?in=${button.textContent}`); + const copyXMRBtn = document.getElementById('copyXMRBtn'); + const copyFiatBtn = document.getElementById('copyFiatBtn'); + const xmrInput = document.getElementById('xmrInput'); + const fiatInput = document.getElementById('fiatInput'); + const selectBox = document.getElementById('selectBox'); + const convertXMRToFiatBtn = document.getElementById('convertXMRToFiat'); + const convertFiatToXMRBtn = document.getElementById('convertFiatToXMR'); + const fiatButtons = document.querySelectorAll('.fiat-btn'); + + // Add event listeners for the currency buttons + fiatButtons.forEach(button => { + button.addEventListener('click', (e) => { + e.preventDefault(); + selectBox.value = button.textContent; + runConvert(); + history.pushState(null, '', `?in=${button.textContent}`); + }); }); - }); - - // Add event listeners for the copy buttons - copyXMRBtn.addEventListener('click', copyToClipboardXMR); - copyFiatBtn.addEventListener('click', copyToClipboardFiat); - - // Add event listeners for the XMR input field - xmrInput.addEventListener('change', xmrConvert); - xmrInput.addEventListener('keyup', () => { - xmrInput.value = xmrInput.value.replace(/[^\.^,\d]/g, '').replace(/\,/, '.'); - if (xmrInput.value.split('.').length > 2) { - xmrInput.value = xmrInput.value.slice(0, -1); - } - xmrConvert(); - }); - xmrInput.addEventListener('input', () => lastModifiedField = 'xmr'); - - // Add event listeners for the fiat input field - fiatInput.addEventListener('change', fiatConvert); - fiatInput.addEventListener('keyup', () => { - fiatInput.value = fiatInput.value.replace(/[^\.^,\d]/g, '').replace(/\,/, '.'); - if (fiatInput.value.split('.').length > 2) { - fiatInput.value = fiatInput.value.slice(0, -1); - } - fiatConvert(); - }); - fiatInput.addEventListener('input', () => lastModifiedField = 'fiat'); - - // Add event listener for the select box to change the conversion - selectBox.addEventListener('change', runConvert); - - // Hide the conversion buttons if JavaScript is enabled - convertXMRToFiatBtn.style.display = 'none'; - convertFiatToXMRBtn.style.display = 'none'; - - // Fetch updated exchange rates immediately, then every 5 seconds - fetchUpdatedExchangeRates(true) - setInterval(fetchUpdatedExchangeRates, 5000); + + // Add event listeners for the copy buttons + copyXMRBtn.addEventListener('click', copyToClipboardXMR); + copyFiatBtn.addEventListener('click', copyToClipboardFiat); + + // Add event listeners for the XMR input field + xmrInput.addEventListener('change', xmrConvert); + xmrInput.addEventListener('keyup', () => { + xmrInput.value = xmrInput.value.replace(/[^\.^,\d]/g, '').replace(/\,/, '.'); + if (xmrInput.value.split('.').length > 2) { + xmrInput.value = xmrInput.value.slice(0, -1); + } + xmrConvert(); + }); + xmrInput.addEventListener('input', () => lastModifiedField = 'xmr'); + + // Add event listeners for the fiat input field + fiatInput.addEventListener('change', fiatConvert); + fiatInput.addEventListener('keyup', () => { + fiatInput.value = fiatInput.value.replace(/[^\.^,\d]/g, '').replace(/\,/, '.'); + if (fiatInput.value.split('.').length > 2) { + fiatInput.value = fiatInput.value.slice(0, -1); + } + fiatConvert(); + }); + fiatInput.addEventListener('input', () => lastModifiedField = 'fiat'); + + // Add event listener for the select box to change the conversion + selectBox.addEventListener('change', runConvert); + + // Hide the conversion buttons if JavaScript is enabled + convertXMRToFiatBtn.style.display = 'none'; + convertFiatToXMRBtn.style.display = 'none'; + + // Fetch updated exchange rates immediately, then every 5 seconds + fetchUpdatedExchangeRates(true) + setInterval(fetchUpdatedExchangeRates, 5000); }); function fetchUpdatedExchangeRates(showAlert = false) { - fetch('/coingecko.php') + fetch('/coingecko.php') .then(response => response.json()) .then(data => { - // Update the exchangeRates object with the new values - for (const [currency, value] of Object.entries(data)) { - exchangeRates[currency.toUpperCase()] = value.lastValue; - } - - updateTimeElement(data.time); - - // Re-execute the appropriate conversion function - runConvert(); + // Update the exchangeRates object with the new values + for (const [currency, value] of Object.entries(data)) { + exchangeRates[currency.toUpperCase()] = value.lastValue; + } + + updateTimeElement(data.time); + + // Re-execute the appropriate conversion function + runConvert(); }) .catch(e => { - const msg = `Error fetching exchange rates: ${e}`; - showAlert ? alert(msg) : console.error(msg); + const msg = `Error fetching exchange rates: ${e}`; + showAlert ? alert(msg) : console.error(msg); }); } function updateTimeElement(unixTimestamp) { - const date = new Date(unixTimestamp * 1000); - const hours = String(date.getHours()).padStart(2, '0'); - const minutes = String(date.getMinutes()).padStart(2, '0'); - const seconds = String(date.getSeconds()).padStart(2, '0'); - const formattedTime = `${hours}:${minutes}:${seconds}`; - - const u = document.querySelector('u'); - u.textContent = formattedTime; - u.parentElement.innerHTML = u.parentElement.innerHTML.replace('Europe/Berlin', Intl.DateTimeFormat().resolvedOptions().timeZone); + const date = new Date(unixTimestamp * 1000); + const hours = String(date.getHours()).padStart(2, '0'); + const minutes = String(date.getMinutes()).padStart(2, '0'); + const seconds = String(date.getSeconds()).padStart(2, '0'); + const formattedTime = `${hours}:${minutes}:${seconds}`; + + const u = document.querySelector('u'); + u.textContent = formattedTime; + u.parentElement.innerHTML = u.parentElement.innerHTML.replace('Europe/Berlin', Intl.DateTimeFormat().resolvedOptions().timeZone); } function copyToClipboardXMR() { - const content = document.getElementById('xmrInput'); - content.select(); - - // Using deprecated execCommand for compatibility with older browsers - document.execCommand('copy'); + const content = document.getElementById('xmrInput'); + content.select(); + + // Using deprecated execCommand for compatibility with older browsers + document.execCommand('copy'); } function copyToClipboardFiat() { - const content = document.getElementById('fiatInput'); - content.select(); - - // Using deprecated execCommand for compatibility with older browsers - document.execCommand('copy'); + const content = document.getElementById('fiatInput'); + content.select(); + + // Using deprecated execCommand for compatibility with older browsers + document.execCommand('copy'); } function fiatConvert() { - const fiatAmount = document.getElementById('fiatInput').value; - const xmrValue = document.getElementById('xmrInput'); - const selectBox = document.getElementById('selectBox').value; - - if (exchangeRates[selectBox]) { - const value = fiatAmount / exchangeRates[selectBox]; - xmrValue.value = value.toFixed(12); - } + const fiatAmount = document.getElementById('fiatInput').value; + const xmrValue = document.getElementById('xmrInput'); + const selectBox = document.getElementById('selectBox').value; + + if (exchangeRates[selectBox]) { + const value = fiatAmount / exchangeRates[selectBox]; + xmrValue.value = value.toFixed(12); + } } function xmrConvert() { - const xmrAmount = document.getElementById('xmrInput').value; - const fiatValue = document.getElementById('fiatInput'); - const selectBox = document.getElementById('selectBox').value; - - if (exchangeRates[selectBox]) { - const value = xmrAmount * exchangeRates[selectBox]; - fiatValue.value = value.toFixed(['BTC', 'LTC', 'ETH', 'XAG', 'XAU'].includes(selectBox) ? 8 : 2); - } + const xmrAmount = document.getElementById('xmrInput').value; + const fiatValue = document.getElementById('fiatInput'); + const selectBox = document.getElementById('selectBox').value; + + if (exchangeRates[selectBox]) { + const value = xmrAmount * exchangeRates[selectBox]; + fiatValue.value = value.toFixed(['BTC', 'LTC', 'ETH', 'XAG', 'XAU'].includes(selectBox) ? 8 : 2); + } } \ No newline at end of file