diff --git a/scoresaber-utils.user.js b/scoresaber-utils.user.js index e69de29..92a3bfa 100644 --- a/scoresaber-utils.user.js +++ b/scoresaber-utils.user.js @@ -0,0 +1,71 @@ +// ==UserScript== +// @name ScoreSaber Utils +// @namespace https://ssu.fascinated.cc +// @version 1.0.0 +// @description Useful additions to ScoreSaber! +// @author Fascinated +// @match https://scoresaber.com/* +// @icon https://www.google.com/s2/favicons?sz=64&domain=scoresaber.com +// @license MIT +// @run-at document-end +// @updateURL https://git.fascinated.cc/Fascinated/ScoreSaberUtils-Backend/raw/branch/master/scoresaber-utils.user.js +// @downloadURL https://git.fascinated.cc/Fascinated/ScoreSaberUtils-Backend/raw/branch/master/scoresaber-utils.user.js +// @grant none +// ==/UserScript== + +/** + * Fetches the player from ScoreSaber Utils. + * + * @param {string} id the player's ID + * @returns the player's data + */ +async function fetchPlayer(id) { + const response = await fetch(`https://ssu.fascinated.cc/account/${id}`); + return await response.json(); +} + +/** + * Handles the player page logic. + */ +async function playerPage() { + const id = window.location.pathname.split("/")[2]; + const player = await fetchPlayer(id); + + addStat( + "+1 PP", + player.rawPerGlobalPerformancePoints.toFixed(2) + "pp", + "The amount of pp to increase the global pp by 1pp" + ); +} + +/** + * Inserts a stat into the player's stats. + * + * @param {string} stat The stat name + * @param {string} value The stat value + * @param {string} hoverText The hover text + */ +function addStat(stat, value, hoverText) { + const statsElement = document.getElementsByClassName("stats-container")[0]; + const svelteClass = statsElement.getAttribute("class").split(" ")[1]; + + const statElement = document.createElement("div"); + statElement.className = `stat-item ${svelteClass}`; + statElement.innerHTML = ` + ${stat} + + ${value} + `; + statsElement.appendChild(statElement); +} + +(function async() { + "use strict"; + + const path = window.location.pathname; + const isPlayerPage = path.startsWith("/u/"); + + if (isPlayerPage) { + playerPage(); + } +})();