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,17 +5,6 @@
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<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>
<select id="currency-select">
@ -36,6 +25,18 @@
<option value="vnd">Dong</option>
</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 id="bitcoin-price"></div>
</div>
<div id="dollars-to-sats"></div>
</div>
</body>

View File

@ -1,8 +1,5 @@
// Function to fetch and update the Bitcoin price
function updateBitcoinPrice() {
const currencySelect = document.getElementById('currency-select');
const selectedCurrency = currencySelect.value;
// bitcoinPriceTracker.js
function updateBitcoinPrice(selectedCurrency) {
fetch(`https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=${selectedCurrency}`)
.then(response => response.json())
.then(data => {
@ -14,24 +11,16 @@ function updateBitcoinPrice() {
const formattedPrice = `${selectedCurrency.toUpperCase()} ` + bitcoinPrice.toLocaleString(undefined, { minimumFractionDigits: 0, maximumFractionDigits: 0 });
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));
}
// Function to refresh the Bitcoin price every 30 seconds
function refreshBitcoinPrice() {
updateBitcoinPrice(); // Call the function immediately to display the price on page load
setInterval(updateBitcoinPrice, 1000); // Call the function every 1 second (1,000 milliseconds)
const currencySelect = document.getElementById('currency-select');
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

View File

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

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
val javaScriptCode = "$firstScript\n$secondScript"
// Load the content of the second script file
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
val cssCode = try {