From ca34a1eb6f32fc15a39cd7a8b7c78d6b10347328 Mon Sep 17 00:00:00 2001 From: Samq64 <81489795+Samq64@users.noreply.github.com> Date: Sat, 2 Nov 2024 20:33:56 -0400 Subject: [PATCH] Run waitForElement in a loop --- addons/percentage-stats/userscript.js | 46 +++++++++++++++------------ 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/addons/percentage-stats/userscript.js b/addons/percentage-stats/userscript.js index 8766a91406..33d2ac92c0 100644 --- a/addons/percentage-stats/userscript.js +++ b/addons/percentage-stats/userscript.js @@ -1,4 +1,6 @@ export default async function ({ addon, console }) { + let els = null; + function getStats() { const stats = { views: els.views.innerHTML, @@ -29,25 +31,14 @@ export default async function ({ addon, console }) { return percentages; } - await addon.tab.waitForElement(".stats"); - const els = { - views: document.querySelector(".project-views"), - loves: document.querySelector(".project-loves"), - favs: document.querySelector(".project-favorites"), - remixes: document.querySelector(".project-remixes"), - }; - const modalL = document.createElement("div"); modalL.className = "loves-modal"; - document.body.appendChild(modalL); const modalF = document.createElement("div"); modalF.className = "favs-modal"; - document.body.appendChild(modalF); const modalR = document.createElement("div"); modalR.className = "remixes-modal"; - document.body.appendChild(modalR); function showModal(showKey) { let showModal; @@ -78,16 +69,29 @@ export default async function ({ addon, console }) { hideModal.style.opacity = 0; } - for (const key in els) { - if (els.hasOwnProperty(key) && key !== "views") { - els[key].addEventListener("mouseover", () => showModal(key)); - els[key].addEventListener("mouseout", () => hideModal(key)); - els[key].addEventListener("mousedown", () => { - // Update modal on click of stat - setTimeout(() => { - showModal(key); - }, 300); - }); + while (true) { + await addon.tab.waitForElement(".stats", { markAsSeen: true }); + els = { + views: document.querySelector(".project-views"), + loves: document.querySelector(".project-loves"), + favs: document.querySelector(".project-favorites"), + remixes: document.querySelector(".project-remixes"), + }; + document.body.appendChild(modalL); + document.body.appendChild(modalF) + document.body.appendChild(modalR); + + for (const key in els) { + if (els.hasOwnProperty(key) && key !== "views") { + els[key].addEventListener("mouseover", () => showModal(key)); + els[key].addEventListener("mouseout", () => hideModal(key)); + els[key].addEventListener("mousedown", () => { + // Update modal on click of stat + setTimeout(() => { + showModal(key); + }, 300); + }); + } } } }