diff --git a/Chrome/content.js b/Chrome/content.js index 5545144..4711d4b 100644 --- a/Chrome/content.js +++ b/Chrome/content.js @@ -1,5 +1,4 @@ // Config - const CACHE_TTL = 5 * 60 * 1000; // Cache Time-To-Live in milliseconds const CONCURRENT_LIMIT = 4; // Max Parallel Fetch Requests @@ -198,6 +197,7 @@ async function refreshScores(force = true) { updateSidebar(); updateTotalScore(); + await storeStorageCache(); } @@ -244,6 +244,33 @@ async function refreshSingleTask(url, task) { } function createControls() { + const prev = document.createElement("button"); + const next = document.createElement("button"); + prev.setAttribute("hidden",true); + next.setAttribute("hidden",true); + prev.className = "move-button"; + next.className = "move-button"; + if(!!window.location.href.match(`${baseURL}/tasks`)){ + prev.textContent = "Prev"; + next.textContent = "Next"; + const prevUrl = moveProblem(-1); + const nextUrl = moveProblem(+1); + if(!!prevUrl){ + prev.removeAttribute("hidden"); + } + if(!!nextUrl){ + next.removeAttribute("hidden"); + } + prev.addEventListener("click", () => { + sessionStorage.setItem("cms-extension-scroll", window.scrollY); + window.location.href = prevUrl; + }) + next.addEventListener("click", () => { + sessionStorage.setItem("cms-extension-scroll", window.scrollY); + window.location.href = nextUrl; + }) + } + const controlsContainer = document.createElement("div"); controlsContainer.className = "cms-extension-controls"; const totalScoreContainer = document.createElement("div"); @@ -263,6 +290,8 @@ function createControls() { } }) ); + controlsContainer.appendChild(prev); + controlsContainer.appendChild(next); controlsContainer.appendChild(totalScoreContainer); controlsContainer.appendChild(refreshButton); // last refreshed timestamp @@ -351,3 +380,55 @@ function updateSidebar() { } }); } + +function moveProblem(delta) { + const urlSplit = splitProblemURL(); + const problem = urlSplit[1]; + + let query; + if(delta === +1){ + query = handleMove(problem,1) + }else if(delta === -1){ + query = handleMove(problem,-1) + } + + if(!query) + return query; + + return urlSplit[0]+query+urlSplit[2]; +} + +function handleMove(item,delta){ + const headers = document.getElementsByClassName("nav-header"); + + + let index = Array.from(headers).findIndex(el => { + const span = el.querySelector("span"); + return span && span.textContent.trim() === item; + }); + + index += delta; + + if(index < 0 || index >= headers.length){ + return null; + } + + const target = headers[index].querySelector("span").textContent; + return target; +} + +function splitProblemURL() { + const u = window.location.href + .split(/\?submission_id/)[0]; + + const parts = u.split("/").filter(Boolean); + + const i = parts.indexOf("tasks"); + if (i === -1 || i + 2 >= parts.length) return null; + + const prefix = "https://" + parts.slice(1, i + 1).join("/") + "/"; + const problem = parts[i + 1]; + const page = "/"+parts.slice(i + 2).join("/"); + + return [prefix, problem, page]; +} \ No newline at end of file diff --git a/Chrome/style.css b/Chrome/style.css index 1fb3b12..9a4dd4e 100644 --- a/Chrome/style.css +++ b/Chrome/style.css @@ -37,3 +37,15 @@ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2); font-weight: bold; } +.move-button { + background-color: #f0f0f0; + padding: 10px; + border: none; + cursor: pointer; + border-radius: 5px; + box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2); + font-weight: bold; +} +.move-button:hover { + background-color: #e5e5e5; +} \ No newline at end of file