From d22ba5df73b438591ddcf84655ef8af6c9df8163 Mon Sep 17 00:00:00 2001 From: MxtOUT <14999077+MxtOUT@users.noreply.github.com> Date: Tue, 17 Sep 2019 12:27:50 +0200 Subject: [PATCH] Fix highlighting on search page for dynamically loaded items --- js/content/store.js | 62 ++++++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/js/content/store.js b/js/content/store.js index fc883d3d9..aad7df43d 100644 --- a/js/content/store.js +++ b/js/content/store.js @@ -3046,40 +3046,44 @@ let SearchPageClass = (function(){ let inputObserver = new MutationObserver(modifyLinks); inputObserver.observe(hiddenInput, {attributes: true, attributeFilter: ["value"]}); - let contscrollObserver; - if (SyncedStorage.get("contscroll")) { - contscrollObserver = new MutationObserver(mutations => { - EarlyAccess.showEarlyAccess(); - mutations.forEach(mutation => { - Highlights.highlightAndTag(mutation.addedNodes); - filtersChanged(mutation.addedNodes); - }); - }); - contscrollObserver.observe(document.querySelectorAll("#search_result_container > div")[1], {childList: true}); - } - - Messenger.addMessageListener("ajaxCompleted", () => { - if (SyncedStorage.get("contscroll")) { - mutations.forEach(mutation => { - for (let node of mutation.removedNodes) { - // Under certain circumstances the search result container will get removed and then added again, thus disconnecting the MutationObserver - if (node.id && node.id === "search_result_container") { - contscrollObserver.observe(document.querySelectorAll("#search_result_container > div")[1], {childList: true}); - break; + let removeObserver = new MutationObserver(mutations => { + mutations.forEach(mutation => { + for (let node of mutation.addedNodes) { + // Under certain circumstances the search result container will get removed and then added again, thus disconnecting the MutationObserver + if (node.id === "search_result_container") { + observeAjax(node.querySelectorAll(".search_result_row")); + + if (!SyncedStorage.get("contscroll")) { + toggleFilter("price-above", "#es_notpriceabove"); + toggleFilter("reviews-below", "#es_noreviewsbelow"); + modifyLinks(); + filtersChanged(); } + ajaxObserver.observe(node.querySelector("#search_resultsRows"), {childList: true}); + break; } - }) - } else { - toggleFilter("price-above", "#es_notpriceabove"); - toggleFilter("reviews-below", "#es_noreviewsbelow"); - modifyLinks(); - } + } + }); + }); + removeObserver.observe(document.querySelector("#search_results"), { childList: true }); + function observeAjax(addedNodes) { EarlyAccess.showEarlyAccess(); + + Highlights.highlightAndTag(addedNodes); + filtersChanged(addedNodes); + } - Highlights.highlightAndTag(document.querySelectorAll(".search_result_row")); - filtersChanged(); - }, false); + let ajaxObserver = new MutationObserver(mutations => { + let rows = []; + for (let mutation of mutations) { + rows = rows.concat( + Array.from(mutation.addedNodes).filter(node => node.classList && node.classList.contains("search_result_row")) + ); + } + observeAjax(rows); + }); + ajaxObserver.observe(document.querySelector("#search_resultsRows"), {childList: true}); }; return SearchPageClass;