diff --git a/scoresaber-utils.user.js b/scoresaber-utils.user.js index 92a3bfa..8aaed9b 100644 --- a/scoresaber-utils.user.js +++ b/scoresaber-utils.user.js @@ -7,12 +7,13 @@ // @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 +// @run-at document-end // ==/UserScript== +let loaded = false; + /** * Fetches the player from ScoreSaber Utils. * @@ -28,6 +29,7 @@ async function fetchPlayer(id) { * Handles the player page logic. */ async function playerPage() { + console.log("Player page loaded!"); const id = window.location.pathname.split("/")[2]; const player = await fetchPlayer(id); @@ -59,13 +61,27 @@ function addStat(stat, value, hoverText) { statsElement.appendChild(statElement); } -(function async() { +function sleep(ms) { + return new Promise((resolve) => setTimeout(resolve, ms)); +} + +async function load() { "use strict"; + if (loaded) return; const path = window.location.pathname; const isPlayerPage = path.startsWith("/u/"); if (isPlayerPage) { + // Wait for the stats container to load + while (document.getElementsByClassName("stats-container").length === 0) { + await sleep(250); + } + playerPage(); } -})(); + + loaded = true; +} + +load();