fix script failing to load on page load
All checks were successful
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Successful in 1m19s

This commit is contained in:
Lee 2024-04-25 07:34:22 +01:00
parent 4cd6c27b2b
commit 2b8b135cf8

@ -7,12 +7,13 @@
// @match https://scoresaber.com/* // @match https://scoresaber.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=scoresaber.com // @icon https://www.google.com/s2/favicons?sz=64&domain=scoresaber.com
// @license MIT // @license MIT
// @run-at document-end
// @updateURL https://git.fascinated.cc/Fascinated/ScoreSaberUtils-Backend/raw/branch/master/scoresaber-utils.user.js // @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 // @downloadURL https://git.fascinated.cc/Fascinated/ScoreSaberUtils-Backend/raw/branch/master/scoresaber-utils.user.js
// @grant none // @run-at document-end
// ==/UserScript== // ==/UserScript==
let loaded = false;
/** /**
* Fetches the player from ScoreSaber Utils. * Fetches the player from ScoreSaber Utils.
* *
@ -28,6 +29,7 @@ async function fetchPlayer(id) {
* Handles the player page logic. * Handles the player page logic.
*/ */
async function playerPage() { async function playerPage() {
console.log("Player page loaded!");
const id = window.location.pathname.split("/")[2]; const id = window.location.pathname.split("/")[2];
const player = await fetchPlayer(id); const player = await fetchPlayer(id);
@ -59,13 +61,27 @@ function addStat(stat, value, hoverText) {
statsElement.appendChild(statElement); statsElement.appendChild(statElement);
} }
(function async() { function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function load() {
"use strict"; "use strict";
if (loaded) return;
const path = window.location.pathname; const path = window.location.pathname;
const isPlayerPage = path.startsWith("/u/"); const isPlayerPage = path.startsWith("/u/");
if (isPlayerPage) { if (isPlayerPage) {
// Wait for the stats container to load
while (document.getElementsByClassName("stats-container").length === 0) {
await sleep(250);
}
playerPage(); playerPage();
} }
})();
loaded = true;
}
load();