GODrums / BetterFloat

Enhance your website experience on CSFloat, Skinport & Skinbid
https://betterfloat.com/
Other
39 stars 10 forks source link

fix: Fixed a case where the currency could be invalid on narrow windows. #45

Closed alxcar closed 8 months ago

alxcar commented 8 months ago

Fixed a bug where the user's currency could be invalid if their window's width was too small, breaking the extension.

If the width of the browser window is not wide enough, float might not be able to properly display the currency code (ex: USD, CAD, AUD) and would display a symbol instead (ex: $, C$, A$. In that case, the following query would not be able to identify the currency,

const userCurrencyRaw = document.querySelector('mat-select-trigger')?.textContent?.trim() ?? 'USD';

and because it would not be null, would not default to 'USD'.

image image

In such cases, we now extract the symbol and match it with the proper currency code:

const symbolToCurrencyCodeMap: { [key: string]: string } = {
        'C$': 'CAD',
        'AED': 'AED',
        'A$': 'AUD',
        'R$': 'BRL',
        'CHF': 'CHF',
        '¥': 'CNY',
        'Kč': 'CZK',
        'kr': 'DKK',
        '£': 'GBP',
        'PLN': 'PLN',
        'SAR': 'SAR',
        'SEK': 'SEK',
        'S$': 'SGD',
    };

Not necessarily an ideal solution if float were to add more currencies, but we cannot use the current module "currency-symbol-map" to perform this operation.

GODrums commented 8 months ago

Great find!

While looking at possible solutions for the issue, I found a more reliable solution, which should also be safe regarding newly introduced currencies.

For that, we can use the fact that CSFloat stores the currency in the localStorage, which we can just retrieve:

export const userCurrency = localStorage.getItem('selected_currency') ?? 'USD';

I adjusted the code in the main-branch accordingly (commit 18d314f).