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