feat: fetch currency exchange rate

exchange rates are fetched at script startup, and at most once daily, then cached. attribution is added as per ExchangeRate-API's requirements
This commit is contained in:
xenticore
2025-04-16 18:23:07 -04:00
parent 84daf5b2d6
commit fcf077c877
3 changed files with 73 additions and 5 deletions

View File

@@ -9,7 +9,9 @@ import { fetchPrice, fetchKeyPrice, ItemPriceData } from './priceService'
import { createPriceRow, createStoreButton } from './uiRenderer'
import { findFirstElement, findFirstChildElement } from './utils/dom'
import { extractPageTitleFromURL } from './utils/url';
import { ExchangeRates, prepareExchangeRates } from './exchangeRateService';
var itemSchema: ItemSchema | null;
var exchangeRates: ExchangeRates | null;
var locale: string = 'en'
@@ -425,7 +427,8 @@ async function inject() {
label.style.fontSize = "85%";
const updateText = $T("Updated %@.", locale).replace('%@', updateTime.toLocaleString(locale, { year: 'numeric', month: 'long', day: 'numeric', hour: '2-digit', minute: '2-digit', timeZoneName: 'short' }))
const attributionText = $T("Trade prices sourced from %@. Currency conversions are approximate.", locale).replace('%@', '<a rel="nofollow" class="external text" href="https://prices.tf">prices.tf</a>');
label.innerHTML = `${updateText}<br>${attributionText}`;
const exchangeRateAttribution = `<a rel="nofollow" class="external text" href="https://www.exchangerate-api.com">Rates By Exchange Rate API</a>.`;
label.innerHTML = `${updateText}<br>${attributionText}<br>${exchangeRateAttribution}`;
row.appendChild(label);
priceProgressRow.insertAdjacentElement('afterend', row);
@@ -440,15 +443,22 @@ function addStyles() {
style.innerHTML = styleCss;
}
prepareSchema().then(function (schema) {
prepareSchema()
.then(schema => {
itemSchema = schema;
if (!itemSchema) {
logError("No item schema ready, exiting.");
wipeSchema(); // FIXME: ugly hack. requires additional page reload. if prepareSchema returns null, we should handle it properly
return;
throw new Error("No item schema ready");
}
})
.then(prepareExchangeRates)
.then(rates => exchangeRates = rates)
.then(() => {
locale = extractLocaleFromURL(document.URL)
addStyles();
inject();
// TODO: Purge expired price data
});
})
.catch((error) => {
logError(error);
})