Skip to content

Instantly share code, notes, and snippets.

@bobpaw
Last active August 28, 2023 14:55
Show Gist options
  • Save bobpaw/0b4ad6cb8070546bdd3aac9b341d2ad3 to your computer and use it in GitHub Desktop.
Save bobpaw/0b4ad6cb8070546bdd3aac9b341d2ad3 to your computer and use it in GitHub Desktop.
Adds a total hours textbox summing the total gameplay hours for a Steam account.
// ==UserScript==
// @name Steam Total Hours
// @namespace https://gist.github.com/bobpaw
// @version 3.0.2
// @author Aiden Woodruff
// @description Adds a total hours textbox summing the total gameplay hours for a Steam account.
// @source https://gist.github.com/bobpaw/0b4ad6cb8070546bdd3aac9b341d2ad3
// @updateURL https://gist.githubusercontent.com/bobpaw/0b4ad6cb8070546bdd3aac9b341d2ad3/raw/steam_totalhours.user.js
// @downloadURL https://gist.githubusercontent.com/bobpaw/0b4ad6cb8070546bdd3aac9b341d2ad3/raw/steam_totalhours.user.js
// @match https://steamcommunity.com/id/*/games/*
// @match https://steamcommunity.com/profiles/*/games/*
// @grant none
// @run-at document-idle
// ==/UserScript==
(function() {
"use strict";
let total_hours_span = document.createElement("span");
total_hours_span.appendChild(document.createTextNode("Total hours: "));
let total_hours = document.createTextNode("Calculating...");
total_hours_span.appendChild(total_hours);
total_hours_span.style.paddingLeft = "1em";
document.querySelector(".profile_small_header_text").appendChild(total_hours_span);
// Make sure all games are loaded.
let timeout_handle = window.setInterval(function () {
const activeTab = document.getElementsByClassName("sectionTab active");
if (activeTab.length === 0) {console.error("No active tab found"); return;}
// Get the number of games.
const num_text = activeTab[0].innerText;
// Silently error if we are not on the All Games tab.
if (!num_text.startsWith("All Games (") || !num_text.endsWith(")")) {console.error("Not on all games tab. on " + num_text); return;}
const num_games = parseInt(num_text.substring(11, num_text.length - 1));
const game_items = document.getElementsByClassName("gameslistitems_GamesListItemContainer_29H3o");
if (num_games === game_items.length) {
window.clearInterval(timeout_handle);
total_hours.textContent = Array.from(document.getElementsByClassName("gameslistitems_Hours_26nl3"))
.map(x => parseFloat(x.lastChild.textContent))
.filter(x => !isNaN(x))
.reduce((x, y) => x + y, 0)
.toFixed(1);
} else {
console.error("Wanted " + num_games + ". got " + game_items.length);
}
}, 500);
})();

Version 3.0.2

  • Add filename to updateURL.

Version 3.0.1

  • Fix updateURL and downloadURL.

Version 3.0.0

  • Updated to work with new Steam website.

Version 2.0.2

  • Test update.

Verson 2.0.1

  • Updated meta-info
  • Updated Gist description

Version 2.0

  • Changed to functioning download URL
  • Added eslint global specifier
  • Added version_history.md

Version 1.0

  • Initial release
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment