Last active
April 10, 2024 17:29
-
-
Save kus/d3d3fcb004e837075c0bc3479ff3cf94 to your computer and use it in GitHub Desktop.
Find out how much you have spent on Counter-Strike keys
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Open https://store.steampowered.com/account/history and make sure you are logged in to Steam. | |
// Scroll to the bottom and load all history. | |
// Open your browsers console (Ctrl + Shift + J on Chrome or Ctrl + Shift + K on Firefox) | |
// and paste all of this code (select all) in the text box (bottom) and hit enter | |
// If you get an error from the script you likely aren't running a modern browser | |
// you can paste the code here https://babeljs.io/repl and copy the output and run that. | |
(() => { | |
const HISTORY_URL = 'https://store.steampowered.com/account/history'; | |
// English and Cyrillic alphabet | |
const CURRENCY_REGEXP = /([A-Za-z\u0400-\u04FF]+)?([^0-9]{1})?([0-9.]+)([A-Za-z€¥\$£\u0400-\u04FF\.]+)?/; | |
// USD must be first | |
const CURRENCY_MAP = [ | |
{ | |
name: 'USD', | |
rateToUSD: 1, | |
symbolPrefix: '$', | |
symbolSuffix: '', | |
a: undefined, | |
b: '$', | |
c: 'USD', | |
}, | |
{ | |
name: 'AUD', | |
rateToUSD: 0.68, | |
symbolPrefix: '$', | |
symbolSuffix: '', | |
a: 'A', | |
b: '$', | |
c: undefined, | |
}, | |
{ | |
name: 'Euro', | |
rateToUSD: 1.1, | |
symbolPrefix: '', | |
symbolSuffix: '€', | |
a: undefined, | |
b: undefined, | |
c: '€', | |
}, | |
{ | |
name: 'Pound', | |
rateToUSD: 1.22, | |
symbolPrefix: '£', | |
symbolSuffix: '', | |
a: undefined, | |
b: '£', | |
c: undefined, | |
}, | |
{ | |
name: 'Yen', | |
rateToUSD: 0.0094, | |
symbolPrefix: '¥', | |
symbolSuffix: '', | |
a: undefined, | |
b: '¥', | |
c: undefined, | |
}, | |
{ | |
name: 'Ruble', | |
rateToUSD: 0.0152442, | |
symbolPrefix: '', | |
symbolSuffix: 'pуб.', | |
a: undefined, | |
b: undefined, | |
c: 'pуб.', | |
}, | |
]; | |
const normalizeCurrency = (str) => { | |
str = str.trim(); | |
// Remove whitespace | |
str = str.replace(/\s/g, ''); | |
// Some amounts have dashes for cents? i.e.: 350,--€ | |
str = str.replace(/-/g, '0'); | |
// Replace decimal point comma with . | |
str = str.replace(/,([0-9]{2}($|[A-Za-z€¥\$£\u0400-\u04FF]).*)/, '.$1'); | |
// Remove commas | |
str = str.replace(',', ''); | |
const match = str.match(CURRENCY_REGEXP); | |
if (match) { | |
return { | |
amount: +match[3], | |
a: match[1], | |
b: match[2], | |
c: match[4] | |
} | |
} | |
return null; | |
}; | |
if (window.location.href !== HISTORY_URL) { | |
alert(`Redirecting you to the correct page, you will need to run the script again.`); | |
return window.location.href = HISTORY_URL; | |
} | |
if (document.querySelector('#load_more_button').style.display !== 'none') { | |
window.scrollTo(0,document.body.scrollHeight); | |
return alert(`Scroll to the bottom and click Load More until all results have loaded.`); | |
} | |
const trs = document.querySelectorAll('table.wallet_history_table > tbody > tr'); | |
let unsupportedCurrency = {}; | |
let localCurrencyIndex = undefined; | |
let keys = 0; | |
let cost = 0; | |
if (trs) { | |
[...trs].forEach((tr) => { | |
const divs = tr.querySelectorAll('td.wht_items > div'); | |
if (divs && divs[0] && divs[1]) { | |
const game = divs[0].innerHTML.trim(); | |
const items = divs[1].innerHTML.trim(); | |
if (game === 'Counter-Strike 2') { | |
if (items.indexOf('Key') > -1) { | |
const price = tr.querySelector('td.wht_total'); | |
if (price) { | |
const amountSplit = normalizeCurrency(price.innerHTML); | |
if (amountSplit) { | |
let currencyFound = false; | |
CURRENCY_MAP.forEach(({name, rateToUSD, a, b, c}, index) => { | |
if (amountSplit.a === a && amountSplit.b === b && amountSplit.c === c) { | |
currencyFound = true; | |
if (typeof localCurrencyIndex === 'undefined' && name !== 'USD') { | |
localCurrencyIndex = index; | |
} | |
cost += amountSplit.amount * rateToUSD; | |
} | |
}); | |
if (!currencyFound) { | |
// Fallback for US users that have only ever bought with USD and no other currency | |
if (amountSplit.b === '$' && amountSplit.a === undefined && amountSplit.c === undefined) { | |
currencyFound = true; | |
cost += amountSplit.amount; | |
} | |
} | |
if (!currencyFound) { | |
const currencyKey = (amountSplit.a || '') + ',' + (amountSplit.b || '') + ',' + (amountSplit.c || ''); | |
if (!unsupportedCurrency[currencyKey]) { | |
unsupportedCurrency[currencyKey] = { | |
a: amountSplit.a + '', | |
b: amountSplit.b + '', | |
c: amountSplit.c + '', | |
example: price.innerHTML.trim() | |
}; | |
} | |
console.warn(`Unknown currency "${(amountSplit.a || '') + (amountSplit.b || '') + (amountSplit.c || '')}"`); | |
} | |
} else { | |
console.warn(`Couldn't find price for ${items}`); | |
} | |
} else { | |
console.warn(`Couldn't find price for ${items}`); | |
} | |
const match = items.match(/^([0-9]+)\s/); | |
if (match) { | |
keys += +match[1]; | |
} else { | |
keys += 1; | |
} | |
} | |
} | |
} | |
}); | |
} | |
if (typeof localCurrencyIndex === 'undefined') { | |
localCurrencyIndex = 0; | |
} | |
window.alert(`You bought ${keys} keys costing ${CURRENCY_MAP[localCurrencyIndex].symbolPrefix}${(cost * (1/CURRENCY_MAP[localCurrencyIndex].rateToUSD)).toFixed(2)}${CURRENCY_MAP[localCurrencyIndex].symbolSuffix} ${CURRENCY_MAP[localCurrencyIndex].name}.`); | |
if (Object.keys(unsupportedCurrency).length > 0) { | |
window.prompt(`Unsupported currency. Copy the text below and send to developer!`, JSON.stringify(unsupportedCurrency)); | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment