From 2b8b135cf814bb2612a3f29a09c3a1dd2f0f0123 Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 25 Apr 2024 07:34:22 +0100 Subject: [PATCH] fix script failing to load on page load --- scoresaber-utils.user.js | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) 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();