From df3b40fbdddeb9a7381d86529d61e2a9dd81458b Mon Sep 17 00:00:00 2001 From: xenticore Date: Thu, 24 Apr 2025 17:38:02 -0400 Subject: [PATCH] lint: `var` to `let`/`const` --- src/content/content.ts | 42 +++++++++++++++--------------- src/content/exchangeRateService.ts | 6 ++--- src/content/priceService.ts | 2 +- src/content/pricing/pricestf.ts | 2 +- src/content/schemaService.ts | 16 ++++++------ src/content/storage.ts | 4 +-- src/content/uiRenderer.ts | 6 ++--- src/content/utils/formatting.ts | 4 +-- src/content/utils/localization.ts | 2 +- src/content/utils/log.ts | 3 +-- src/content/utils/url.ts | 2 +- 11 files changed, 44 insertions(+), 45 deletions(-) diff --git a/src/content/content.ts b/src/content/content.ts index 5f2b11e..051b6e9 100644 --- a/src/content/content.ts +++ b/src/content/content.ts @@ -10,10 +10,10 @@ 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; +let itemSchema: ItemSchema | null; +let exchangeRates: ExchangeRates | null; -var locale: string = 'en' +let locale: string = 'en' /** Exclude these from the pricelist. */ const excludedQualities = new Set([ @@ -33,8 +33,8 @@ async function inject() { // Not an item page return; } - var itemIndex: number | null = null; - var itemName: string | null = null; + let itemIndex: number | null = null; + let itemName: string | null = null; // Find buy buttons const buyButton = findFirstChildElement('.btn_buynow', itemInfobox); @@ -94,7 +94,7 @@ async function inject() { return; } - var qualities: number[] = [] + const qualities: number[] = [] const firstQualityTag = findFirstChildElement('.quality-tag', itemInfobox); @@ -122,7 +122,7 @@ async function inject() { // th.infobox-header (Basic Information) // ... - var storeButtons: HTMLTableRowElement[] = []; + const storeButtons: HTMLTableRowElement[] = []; // backpack.tf button storeButtons.push(createStoreButton("backpack.tf", new URL(`https://backpack.tf/classifieds?item=${encodeURIComponent(itemName)}`))); @@ -171,7 +171,7 @@ async function inject() { priceProgressRow.appendChild(priceProgressData); priceInfoboxHeadingRow.insertAdjacentElement('afterend', priceProgressRow); - var token: string | null; + let token: string | null; // Steam Community Market // TODO: Change this to lazy-load, so that it doesn't make network requests when we have cached data. @@ -187,7 +187,7 @@ async function inject() { log('Failed to get an access token for prices.tf: ' + err); } - var updateTime: Date | null = null; + let updateTime: Date | null = null; enum PriceRowCategory { None, @@ -201,19 +201,19 @@ async function inject() { row: HTMLTableRowElement category: PriceRowCategory } - var priceRows: PriceRow[]= []; + const priceRows: PriceRow[]= []; // Get current key price const keyPrice = await fetchKeyPrice(token); - var currentTime = new Date() + const currentTime = new Date() const promises = qualities.filter(x => !excludedQualities.has(x)).map(async (quality) => { const qualifiedName = ((quality != 6 ? itemQualities[quality as unknown as keyof typeof itemQualities].toString() : '') + ' ' + itemName).trim() // logDebug(`Fetching price for ${qualifiedName}`) - var data: ItemPriceData | null + let data: ItemPriceData | null try { data = await fetchPrice(token, itemIndex + ";" + quality, currentTime); updateTime = new Date(data.update) @@ -231,7 +231,7 @@ async function inject() { if(itemSchema[itemIndex].hasAustraliumVariant) { promises.push(new Promise(async (resolve) => { logDebug(`Fetching price for Australium ${itemName}`) - var data: ItemPriceData | null + let data: ItemPriceData | null try { data = await fetchPrice(token, `${itemIndex};11;australium`, currentTime); updateTime = new Date(data.update) @@ -247,7 +247,7 @@ async function inject() { })) } - var festiveHeadingRow: HTMLTableRowElement | null + let festiveHeadingRow: HTMLTableRowElement | null // Check item schema for Festive variant of current defindex if(itemSchema[itemIndex].festiveVariant != null) { /// Create subheading @@ -263,7 +263,7 @@ async function inject() { promises.push(new Promise(async (resolve) => { logDebug(`Fetching price for Festive ${itemName}`) - var data: ItemPriceData | null + let data: ItemPriceData | null try { data = await fetchPrice(token, `${itemSchema[itemIndex].festiveVariant};6`, currentTime); updateTime = new Date(data.update) @@ -279,7 +279,7 @@ async function inject() { })) promises.push(new Promise(async (resolve) => { logDebug(`Fetching price for Strange Festive ${itemName}`) - var data: ItemPriceData | null + let data: ItemPriceData | null try { data = await fetchPrice(token, `${itemSchema[itemIndex].festiveVariant};11`, currentTime); updateTime = new Date(data.update) @@ -295,7 +295,7 @@ async function inject() { })) } - var killstreakKitHeadingRow: HTMLTableRowElement | null + let killstreakKitHeadingRow: HTMLTableRowElement | null // Check for Killstreak Kits if(itemSchema[itemIndex].slot == ItemSlot.Primary || itemSchema[itemIndex].slot == ItemSlot.Secondary || @@ -314,9 +314,9 @@ async function inject() { [1,2,3].map((tier) => { promises.push(new Promise(async (resolve) => { logDebug(`Fetching price for ${itemName} Killstreak Kit Tier ${tier}`) - var data: ItemPriceData | null + let data: ItemPriceData | null try { - var kitIndex: number + let kitIndex: number switch (tier) { default: case 1: kitIndex = 6527; break; @@ -351,7 +351,7 @@ async function inject() { "Silver Mk.II", "Gold Mk.II", ] - var botKillerHeadingRow: HTMLTableRowElement | null + let botKillerHeadingRow: HTMLTableRowElement | null if(itemSchema[itemIndex].botkillerVariants != null && itemSchema[itemIndex].botkillerVariants.length > 0) { /// Create subheading botKillerHeadingRow = document.createElement("tr") @@ -370,7 +370,7 @@ async function inject() { const variantName = itemName.includes('Mk.II') ? itemName.split(' ')[0] + ' Mk.II' : itemName.split(' ')[0] promises.push(new Promise(async (resolve) => { logDebug(`Fetching price for ${itemName}`) - var data: ItemPriceData | null + let data: ItemPriceData | null try { data = await fetchPrice(token, `${variantIndex};11`, currentTime); updateTime = new Date(data.update) diff --git a/src/content/exchangeRateService.ts b/src/content/exchangeRateService.ts index fe3a24a..5b41de7 100644 --- a/src/content/exchangeRateService.ts +++ b/src/content/exchangeRateService.ts @@ -16,8 +16,8 @@ export async function wipeExchangeRates(): Promise { } export async function prepareExchangeRates(): Promise { - var needsUpdate: Boolean = false - var rates: ExchangeRates | null = null + let needsUpdate: boolean = false + let rates: ExchangeRates | null = null rates = await getStorageValue(storage_exchangerates, null); const update = await getStorageValue(storage_exchangerates_update, null) @@ -39,7 +39,7 @@ export async function prepareExchangeRates(): Promise { const response = await GM_fetch(url); if (response.ok) { await setStorageValue(storage_exchangerates_update, new Date().toISOString()) - var json = await response.json() + const json = await response.json() if(json != null){ rates = json['rates'] await setStorageValue(storage_exchangerates, rates) diff --git a/src/content/priceService.ts b/src/content/priceService.ts index 5aa675d..a7fa811 100644 --- a/src/content/priceService.ts +++ b/src/content/priceService.ts @@ -41,7 +41,7 @@ export async function fetchKeyPrice(token: string) { */ export async function fetchPrice(token: string, sku: string, update: Date = new Date(), ttl: number = 30 * 60 * 1000): Promise { return new Promise(async (resolve, reject) => { - var data: ItemPriceData | null + let data: ItemPriceData | null const cached: ItemPriceData = await getStorageValue(storage_priceprefix + sku, null) if (cached != null && 'keys' in cached && 'metal' in cached && !isNaN(cached.update)) { diff --git a/src/content/pricing/pricestf.ts b/src/content/pricing/pricestf.ts index 1d6989d..4cec571 100644 --- a/src/content/pricing/pricestf.ts +++ b/src/content/pricing/pricestf.ts @@ -46,7 +46,7 @@ async function priceUsingPricesTF(token: string, sku: string, retries: number = if (!token) { reject(401) } - var response = await GM_fetch(`https://api2.prices.tf/prices/${encodeURIComponent(sku)}`, { + let response = await GM_fetch(`https://api2.prices.tf/prices/${encodeURIComponent(sku)}`, { method: 'get', headers: new Headers({ 'Accept': 'application/json', diff --git a/src/content/schemaService.ts b/src/content/schemaService.ts index 0a95ca8..eff2feb 100644 --- a/src/content/schemaService.ts +++ b/src/content/schemaService.ts @@ -14,8 +14,8 @@ export function checkAustraliumVariant(defindex: number): boolean { export declare const __VERSION__: string; function isDateAfterOneDay(date1: Date, date2: Date): boolean { - var diff = date2.getTime() - date1.getTime(); - var diffDays = Math.round(diff / (1000 * 3600 * 24)); + const diff = date2.getTime() - date1.getTime(); + const diffDays = Math.round(diff / (1000 * 3600 * 24)); return diffDays > 1; } @@ -119,8 +119,8 @@ export async function wipeSchema(): Promise { } export async function prepareSchema(): Promise { - var needsUpdate: Boolean = false - var itemSchema: ItemSchema | null = null + let needsUpdate: boolean = false + let itemSchema: ItemSchema | null = null const storedVersion: string | null = await getStorageValue(storage_version, null) if(!storedVersion || !semver.valid(storedVersion)) { @@ -150,14 +150,14 @@ export async function prepareSchema(): Promise { if (response.ok) { await setStorageValue(storage_lastUpdateTime, new Date().getTime()); - var cacheItems = {} + const cacheItems = {} - var responseItems: any[] = await response.json() + const responseItems: any[] = await response.json() // We want to keep the keys `defindex`, `item_name`, and `attributes` responseItems.forEach((item: any) => { const defindex: number = item['defindex'] - var tradable: Boolean = true + let tradable: boolean = true try { if(item['attributes'] != null) { if(item['attributes'].find((attribute: {}) => (attribute as any)['class'] == "cannot_trade")) { @@ -169,7 +169,7 @@ export async function prepareSchema(): Promise { log(item) } - var canKillstreakify: Boolean = false + let canKillstreakify: boolean = false try { if(item['capabilities'] != null) { if(item['capabilities']['can_killstreakify'] != null && item['capabilities']['can_killstreakify'] == true) { diff --git a/src/content/storage.ts b/src/content/storage.ts index d14fea8..1707b91 100644 --- a/src/content/storage.ts +++ b/src/content/storage.ts @@ -1,5 +1,5 @@ -declare var __ENV_USERSCRIPT: boolean; -declare var __ENV_WEBEXTENSION: boolean; +declare let __ENV_USERSCRIPT: boolean; +declare let __ENV_WEBEXTENSION: boolean; function getStorageValue(name: string, defaultValue: string): Promise { if(__ENV_USERSCRIPT) { diff --git a/src/content/uiRenderer.ts b/src/content/uiRenderer.ts index f555759..3f60e3d 100644 --- a/src/content/uiRenderer.ts +++ b/src/content/uiRenderer.ts @@ -22,14 +22,14 @@ export function createPriceRow(qualityName: string, data: ItemPriceData, keyPric const priceData = document.createElement("td"); const priceLink = document.createElement("span"); - var priceString: string = '' + let priceString: string = '' if(data) { const gamePrice = formatPrice(data.keys, data.metal, keyPrice.metal, locale).trim() const realPriceUSD = convertTF2PriceToUSD(data.keys, data.metal, keyPrice.metal) const USDFormatter = new Intl.NumberFormat(locale, { style: "currency", currency: 'USD' }) - var realPriceString = USDFormatter.format(realPriceUSD) + let realPriceString = USDFormatter.format(realPriceUSD) const currency = defaultCurrencyForPageLocale(locale) ?? 'USD' if(currency !== 'USD') { try { @@ -56,7 +56,7 @@ export function createPriceRow(qualityName: string, data: ItemPriceData, keyPric export function createStoreButton(storeName: string, url: URL) { const button = document.createElement("tr") - var source = `` + let source = `` source = source.replace("{link}", url.toString()) source = source.replace("{title}", $T("View listings on %@").replace('%@', storeName)) button.innerHTML = source diff --git a/src/content/utils/formatting.ts b/src/content/utils/formatting.ts index 4c27a1e..391c98c 100644 --- a/src/content/utils/formatting.ts +++ b/src/content/utils/formatting.ts @@ -2,14 +2,14 @@ import { conversion_ref_usd } from '../config'; import { $T } from './localization' function toFixed(num: number, fixed: number) { - var re = new RegExp('^-?\\d+(?:\.\\d{0,' + (fixed || -1) + '})?'); + const re = new RegExp('^-?\\d+(?:\.\\d{0,' + (fixed || -1) + '})?'); return num.toString().match(re)[0]; } export function formatPrice(keys: number, metal: number, keyPrice: number, locale: string = 'en') { const formattedKeys = +(keys + (metal / keyPrice)).toFixed(2) - var output: string = '' + let output: string = '' if(keys > 0) { output += (formattedKeys == 1.0 ? $T("%@ key") : $T("%@ keys")).replace('%@', formattedKeys.toLocaleString(locale)) } else { diff --git a/src/content/utils/localization.ts b/src/content/utils/localization.ts index 7ef2e1b..5d21dfc 100644 --- a/src/content/utils/localization.ts +++ b/src/content/utils/localization.ts @@ -30,7 +30,7 @@ export function $T(s: string, locale?: Intl.LocalesArgument): string { } export function extractLocaleFromURL(url: string): string { - var split = url.substring(url.indexOf("/wiki/") + "/wiki/".length); + const split = url.substring(url.indexOf("/wiki/") + "/wiki/".length); if (split.indexOf('/') != -1) { // Remove language suffix e.g. `/es` return split.substring(split.indexOf('/') + 1); diff --git a/src/content/utils/log.ts b/src/content/utils/log.ts index 23ababe..49fbe16 100644 --- a/src/content/utils/log.ts +++ b/src/content/utils/log.ts @@ -1,5 +1,4 @@ -declare var __PRODUCTION: boolean; -declare var __EXTENSION_NAME: string; +declare let __EXTENSION_NAME: string; const logHeader = `[${__EXTENSION_NAME}] `; /** `console.debug` with header; automatically NO-OP on production build */ diff --git a/src/content/utils/url.ts b/src/content/utils/url.ts index 3aa388c..566fd4f 100644 --- a/src/content/utils/url.ts +++ b/src/content/utils/url.ts @@ -1,5 +1,5 @@ export function extractPageTitleFromURL(url: string): string { - var split = url.substring(url.indexOf("/wiki/") + "/wiki/".length); + let split = url.substring(url.indexOf("/wiki/") + "/wiki/".length); if (split.indexOf('/') != -1) { // Remove language suffix (/es) split = split.substring(0, split.indexOf('/'));