document.addEventListener('DOMContentLoaded', () => { const assetSelect = document.getElementById('asset-select'); const toggleButton = document.getElementById('day-night-toggle'); const body = document.body; // Parse query parameters from the URL const params = new URLSearchParams(window.location.search); let selectedAsset = params.get('asset'); // Function to populate the asset select dropdown (no await) function populateAssetSelect() { fetch('/api?cmd=getAssets') .then(response => { if (!response.ok) { throw new Error(`HTTP error! Status: ${response.status}`); } return response.json(); }) .then(data => { // Clear existing options assetSelect.innerHTML = ''; // Populate the dropdown with fetched data data.forEach(asset => { const option = document.createElement('option'); option.value = asset; // Assuming data is an array of strings option.textContent = 'KNOLIX / '+asset; assetSelect.appendChild(option); }); // Set the initial value if (selectedAsset) { // If URL has an asset parameter, use it assetSelect.value = selectedAsset; } }) .catch(error => { console.error('Error fetching assets:', error); assetSelect.innerHTML = ''; }); } // Populate the dropdown on page load populateAssetSelect(); if( selectedAsset===null ) { selectedAsset = 'TRX'; } // Handle asset selection change assetSelect.addEventListener('change', () => { const newAsset = assetSelect.value; window.location.href = `/?asset=${newAsset}`; }); // Toggle Night/Day Mode toggleButton.addEventListener('click', () => { body.classList.toggle('night-mode'); toggleButton.textContent = body.classList.contains('night-mode') ? '🌞' : '🌙'; }); // Buy / Sell buyButton.addEventListener('click', () => { const assetAddress = prompt(`Enter ${selectedAsset} address:`); if (assetAddress) { fetch(`/api.php`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ action: 'buy', address: assetAddress, asset: selectedAsset }) }) .then(response => response.json()) .then(data => { if (data.address) { //alert(`Buy confirmed for ${selectedAsset} at address: ${assetAddress}`); document.getElementById('modalpay').style.display='block'; document.getElementById('in-name').innerHTML = 'KNOLIX'; document.getElementById('in-address').innerHTML = data.address; document.getElementById('in-warning').innerHTML = 'Send only KNOLIX to this address, any other asset will be lost'; document.getElementById('in-qr').src = 'https://quickchart.io/qr?text='+data.address; document.getElementById('out-name').innerHTML = selectedAsset; document.getElementById('out-address').innerHTML = assetAddress; } if (data.err) { alert(data.err); } }) .catch(error => { console.error('Error:', error); alert(`An error occurred while validating ${selectedAsset} address.`); }); } else { alert(' ${selectedAsset} address is required to proceed.'); } }); sellButton.addEventListener('click', () => { const knolixAddress = prompt(`Enter KNOLIX address:`); if (knolixAddress) { fetch(`/api.php`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ action: 'sell', address: knolixAddress, asset: selectedAsset }) }) .then(response => response.json()) .then(data => { if (data.address) { //alert(`Sell confirmed for ${selectedAsset} at address: ${knolixAddress}`); document.getElementById('modalpay').style.display='block'; document.getElementById('in-name').innerHTML = selectedAsset; document.getElementById('in-address').innerHTML = data.address; document.getElementById('in-warning').innerHTML = 'Send only '+selectedAsset+' to this address, any other asset will be lost'; document.getElementById('in-qr').src = 'https://quickchart.io/qr?text='+data.address; document.getElementById('out-name').innerHTML = 'KNOLIX'; document.getElementById('out-address').innerHTML = knolixAddress; } if (data.err) { alert(data.err); } }) .catch(error => { console.error('Error:', error); alert(`An error occurred while validating KNOLIX address.`); }); } else { alert(' KNOLIX address is required to proceed.'); } }); const tradesTable = document.getElementById('trades-table').querySelector('tbody'); // Function to render trade rows function renderTrades(trades) { tradesTable.innerHTML = ''; // Clear the table trades.forEach(trade => { const row = document.createElement('tr'); row.innerHTML = `