From d17c60d12ba5238e26b978cc93478a7c765112b9 Mon Sep 17 00:00:00 2001 From: OhItsTom <22730962+ohitstom@users.noreply.github.com> Date: Tue, 19 Sep 2023 07:16:40 +0100 Subject: [PATCH] feat(settings): cache scroll pos - boilerplate --- Comfy/theme.script.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Comfy/theme.script.js b/Comfy/theme.script.js index dcd9ae4..1f119db 100644 --- a/Comfy/theme.script.js +++ b/Comfy/theme.script.js @@ -955,6 +955,7 @@ isLarge: true, }); + // Discord Text const header = document.querySelector(".main-trackCreditsModal-header"); const container = document.createElement("div"); const extraText = document.createElement("a"); @@ -965,6 +966,25 @@ container.appendChild(document.querySelector("h1.main-type-alto")); container.appendChild(extraText); header.prepend(container); + + // Scroll Position + const section = document.querySelector(".main-trackCreditsModal-mainSection"); + const cache = sessionStorage.getItem("comfy-settings-scroll"); + const scrollVal = cache ? cache * (section.scrollHeight - section.clientHeight) : 0; + + function handleScroll(event) { + const a = section.scrollTop; + const b = section.scrollHeight - section.clientHeight; + const c = a / b; + sessionStorage.setItem("comfy-settings-scroll", c); + + // If section is no longer in the DOM, remove the event listener + if (!document.contains(section)) { + section.removeEventListener("scroll", handleScroll); + } + } + section.scrollTo(null, scrollVal); + section.addEventListener("scroll", handleScroll); }, false, true