From 6ae9f176d50bbf3d831c64e405a5c45467c36430 Mon Sep 17 00:00:00 2001 From: henryjw <3443648+henryjw@users.noreply.github.com> Date: Wed, 15 Jan 2025 12:34:16 -0500 Subject: [PATCH] Optimized difficulty toggle to work on very long lists (1000+ problems) --- content.js | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/content.js b/content.js index 53410fa..f024adc 100644 --- a/content.js +++ b/content.js @@ -59,13 +59,27 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { function toggleDifficulty(hide) { const difficulties = ["简单", "中等", "困难", "Easy", "Medium", "Med.", "Hard"]; - const elements = Array.from(document.querySelectorAll("span, div, p")); - - elements.forEach((el) => { - if (difficulties.some((difficulty) => el.innerText === difficulty)) { - el.style.display = hide ? "none" : ""; - } - }); + const elements = Array + .from(document.querySelectorAll("span, div, p")) + .filter(el => difficulties.some((difficulty) => el.innerText === difficulty)); + + const batchSize = 300; + const batches = []; + let nextElementIndex = 0; + + while (nextElementIndex < elements.length) { + batches.push(elements.slice(nextElementIndex, nextElementIndex + batchSize)) + nextElementIndex = nextElementIndex + batchSize + } + + // Process the elements in batches since some views can have 1000+ elements, which can easily overwhelm the browser + batches.forEach((batch, batchIndex) => { + setTimeout(() => { + batch.forEach(el => { + el.style.display = hide ? "none" : ""; + }) + }, batchIndex * 50); + }) } function toggleAcceptance(hide) {