Skip to content

Instantly share code, notes, and snippets.

@teyfix
Created September 29, 2022 05:57
Show Gist options
  • Save teyfix/174a83ef0c87501b395c63783df97968 to your computer and use it in GitHub Desktop.
Save teyfix/174a83ef0c87501b395c63783df97968 to your computer and use it in GitHub Desktop.
User Scripts/Steam History - Calculates the payment history at https://store.steampowered.com/account/history
(() => {
const pattern = new RegExp("https://store.steampowered.com/account/history");
if (!pattern.test(window.location.href)) {
return;
}
let last;
const content = (item) =>
(item.querySelector("div") ?? item).innerText.replace(/\s+/g, " ").trim();
const calculate = () => {
const result = Object.entries(
Array.from(
document.querySelectorAll(".wallet_history_table tbody tr"),
).reduce((groups, tr) => {
const [, title, , total, change] = Array.from(
tr.querySelectorAll("td"),
).map(content);
if (title && total && change && /^-/.test(change)) {
const totalNum = parseInt(
total.replace(/[^\d,]/g, "").replace(",", "."),
);
groups[title] ??= 0;
groups[title] = (groups[title] ?? 0) + totalNum;
}
return groups;
}, {}),
)
.map(([title, amount]) => ({ title, amount }))
.filter(({ amount }) => amount > 10)
.sort((a, b) => b.amount - a.amount);
const dump = JSON.stringify(result);
if (last === dump) {
return;
}
last = dump;
console.clear();
console.table(result);
return calculate;
};
const debounce = (fn, ms = 300) => {
let timer;
return (...args) => {
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(() => fn(...args), ms);
};
};
const calculateFn = debounce(calculate);
calculate();
document.body.addEventListener("DOMSubtreeModified", calculateFn);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment