all script functions are isolated in their own js file, currency select pulled out of price tracker

This commit is contained in:
0ceanSlim 2023-08-09 11:08:03 -04:00
parent 113a320850
commit d910ac19a7
6 changed files with 118 additions and 59 deletions

View File

@ -5,18 +5,7 @@
<link rel="stylesheet" type="text/css" href="style.css"> <link rel="stylesheet" type="text/css" href="style.css">
</head> </head>
<body> <div class="currency-dropdown">
<div class="container">
<div class="price-display">
<div class="bitcoin-logo">
<img src='file:///android_asset/images/logo.png' alt='BTC' />
</div>
<div id="bitcoin-price"></div>
</div>
<div id="dollars-to-sats"></div>
<div class="currency-dropdown">
<label for="currency-select">Select Currency: </label> <label for="currency-select">Select Currency: </label>
<select id="currency-select"> <select id="currency-select">
<option value="usd">Dollars</option> <option value="usd">Dollars</option>
@ -35,7 +24,19 @@
<option value="uah">Ukrainian Hryvnia</option> <option value="uah">Ukrainian Hryvnia</option>
<option value="vnd">Dong</option> <option value="vnd">Dong</option>
</select> </select>
</div>
<body>
<div class="container">
<div class="price-display">
<div class="bitcoin-logo">
<img src='file:///android_asset/images/logo.png' alt='BTC' />
</div> </div>
<div id="bitcoin-price"></div>
</div>
<div id="dollars-to-sats"></div>
</div> </div>
</body> </body>

View File

@ -1,8 +1,5 @@
// Function to fetch and update the Bitcoin price // bitcoinPriceTracker.js
function updateBitcoinPrice() { function updateBitcoinPrice(selectedCurrency) {
const currencySelect = document.getElementById('currency-select');
const selectedCurrency = currencySelect.value;
fetch(`https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=${selectedCurrency}`) fetch(`https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=${selectedCurrency}`)
.then(response => response.json()) .then(response => response.json())
.then(data => { .then(data => {
@ -14,24 +11,16 @@ function updateBitcoinPrice() {
const formattedPrice = `${selectedCurrency.toUpperCase()} ` + bitcoinPrice.toLocaleString(undefined, { minimumFractionDigits: 0, maximumFractionDigits: 0 }); const formattedPrice = `${selectedCurrency.toUpperCase()} ` + bitcoinPrice.toLocaleString(undefined, { minimumFractionDigits: 0, maximumFractionDigits: 0 });
bitcoinPriceElement.textContent = formattedPrice; // Display the formatted Bitcoin price bitcoinPriceElement.textContent = formattedPrice; // Display the formatted Bitcoin price
// Calculate currency to sats
const currencyToSatsElement = document.getElementById('dollars-to-sats');
const satsConversionFactor = 100000000; // 1 bitcoin = 100 million sats
// Calculate how many sats you can get for 1 unit of the selected currency based on the fetched Bitcoin price
const currencyValue = 1;
const sats = (currencyValue / bitcoinPrice) * satsConversionFactor;
currencyToSatsElement.textContent = `1 ${selectedCurrency.toUpperCase()} = ${sats.toFixed(0)} sats`; // Display the result
}) })
.catch(error => console.error(error)); .catch(error => console.error(error));
} }
// Function to refresh the Bitcoin price every 30 seconds
function refreshBitcoinPrice() { function refreshBitcoinPrice() {
updateBitcoinPrice(); // Call the function immediately to display the price on page load const currencySelect = document.getElementById('currency-select');
setInterval(updateBitcoinPrice, 1000); // Call the function every 1 second (1,000 milliseconds) const selectedCurrency = currencySelect.value;
updateBitcoinPrice(selectedCurrency); // Call the function immediately to display the price on page load
setInterval(() => updateBitcoinPrice(selectedCurrency), 10000); // Call the function every 30 seconds
} }
// Call the function to refresh the Bitcoin price // Call the function to refresh the Bitcoin price

View File

@ -1,19 +1,28 @@
fetch('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd') // fiatToSatsConverter.js
.then(response => response.json()) function updateFiatToSatsConversion(selectedCurrency) {
.then(data => { fetch(`https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=${selectedCurrency}`)
const bitcoinPrice = data['bitcoin']['usd']; .then(response => response.json())
const satoshisToDollars = (satoshis) => { .then(data => {
const bitcoinAmount = satoshis / 100000000; const bitcoinPrice = data['bitcoin'][selectedCurrency];
const dollarsAmount = bitcoinAmount * bitcoinPrice; const satsConversionFactor = 100000000; // 1 bitcoin = 100 million sats
return dollarsAmount.toFixed(2);
}; const currencyValue = 1;
const satoshisInputElement = document.getElementById('sats-input'); const sats = (currencyValue / bitcoinPrice) * satsConversionFactor;
const dollarsOutputElement = document.getElementById('dollars-output');
// Listen for changes to the satoshis input and update the dollars output const currencyToSatsElement = document.getElementById('dollars-to-sats');
satoshisInputElement.addEventListener('input', (event) => { currencyToSatsElement.textContent = `1 ${selectedCurrency.toUpperCase()} = ${sats.toFixed(0)} sats`; // Display the result
const satoshis = event.target.value; })
const dollars = satoshisToDollars(satoshis); .catch(error => console.error(error));
dollarsOutputElement.textContent = `$${dollars}`; }
document.addEventListener('DOMContentLoaded', () => {
const currencySelect = document.getElementById('currency-select');
const selectedCurrency = currencySelect.value;
updateFiatToSatsConversion(selectedCurrency); // Call the function on page load
currencySelect.addEventListener('change', () => {
const selectedCurrency = currencySelect.value;
updateFiatToSatsConversion(selectedCurrency);
}); });
}) });
.catch(error => console.error(error));

View File

@ -0,0 +1,10 @@
// currencySelection.js
document.addEventListener('DOMContentLoaded', () => {
const currencySelect = document.getElementById('currency-select');
currencySelect.addEventListener('change', () => {
const selectedCurrency = currencySelect.value;
updateBitcoinPrice(selectedCurrency); // Update Bitcoin price when currency is changed
updateFiatToSatsConversion(selectedCurrency); // Update fiat to sats conversion when currency is changed
});
});

View File

@ -0,0 +1,32 @@
// satsToDollarsCalc.js
function updateSatsToDollarsConversion() {
const satsInputElement = document.getElementById('sats-input');
const dollarsOutputElement = document.getElementById('dollars-output');
const satsValue = parseFloat(satsInputElement.value);
if (!isNaN(satsValue)) {
const currencySelect = document.getElementById('currency-select');
const selectedCurrency = currencySelect.value;
fetch(`https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=${selectedCurrency}`)
.then(response => response.json())
.then(data => {
const bitcoinPrice = data['bitcoin'][selectedCurrency];
const satsConversionFactor = 100000000; // 1 bitcoin = 100 million sats
const dollarsValue = (satsValue / satsConversionFactor) * bitcoinPrice;
dollarsOutputElement.textContent = `${dollarsValue.toFixed(2)} ${selectedCurrency.toUpperCase()}`;
})
.catch(error => console.error(error));
} else {
dollarsOutputElement.textContent = 'Invalid input';
}
}
document.addEventListener('DOMContentLoaded', () => {
const satsInputElement = document.getElementById('sats-input');
satsInputElement.addEventListener('input', updateSatsToDollarsConversion);
});

View File

@ -52,8 +52,26 @@ class MainActivity : AppCompatActivity() {
"" ""
} }
// Combine the content of both scripts // Load the content of the second script file
val javaScriptCode = "$firstScript\n$secondScript" val thirdScript = try {
assets.open("script3.js").bufferedReader().use { it.readText() }
} catch (e: IOException) {
e.printStackTrace()
// If loading the third script fails, set an empty script
""
}
// Load the content of the second script file
val fourthScript = try {
assets.open("script4.js").bufferedReader().use { it.readText() }
} catch (e: IOException) {
e.printStackTrace()
// If loading the fourth script fails, set an empty script
""
}
// Combine the content of scripts
val javaScriptCode = "$firstScript\n$secondScript\n$thirdScript\n$fourthScript"
// Load the CSS code from the "style.css" file // Load the CSS code from the "style.css" file
val cssCode = try { val cssCode = try {