diff --git a/src/zona/data/server/inject.js b/src/zona/data/server/inject.js index 550830f..d199c03 100644 --- a/src/zona/data/server/inject.js +++ b/src/zona/data/server/inject.js @@ -1,6 +1,24 @@ -const ws = new WebSocket("__SOCKET_ADDRESS_"); -ws.onmessage = event => { +(() => { + // if user at the bottom before reload, scroll to new bottom + if (localStorage.getItem("wasAtBottom") === "1") { + localStorage.removeItem("wasAtBottom"); + window.addEventListener("load", () => { + requestAnimationFrame(() => { + window.scrollTo(0, document.body.scrollHeight); + }); + }); + } + + const ws = new WebSocket("__SOCKET_ADDRESS_"); + ws.onmessage = event => { if (event.data === "reload") { - location.reload(); + // store flag if user currently at bottom + const nearBottom = window.innerHeight + window.scrollY + >= document.body.scrollHeight - 100; + if (nearBottom) { + localStorage.setItem("wasAtBottom", "1"); + } + location.reload(); } -}; + }; +})();