From 2221d98dc96bc75766a1e19894c8ea85842098f5 Mon Sep 17 00:00:00 2001 From: Kevin Payravi Date: Tue, 23 Jul 2024 17:02:19 -0700 Subject: [PATCH] Moving change observer to beginning of filtering Adding the change observer after the initial filter occurs seems to be able to cause race conditions, particularly on Startpage. This commit moves the change observer addition to the beginning of the filtering function. The downside is that adding the change observer before filtering starts isn't ideal, since filtering manipulates the DOM and can trigger a re-check. --- scripts/content-search-filtering.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/scripts/content-search-filtering.js b/scripts/content-search-filtering.js index 1f3f7b8..8b8aab2 100644 --- a/scripts/content-search-filtering.js +++ b/scripts/content-search-filtering.js @@ -395,7 +395,6 @@ async function reorderSearchResults(searchResults, searchEngine, storage) { // Get the first Fandom/Fextralife/Neoseeker result, if it exists const nonIndieResults = Array.from(document.querySelectorAll(`div[data-hveid] a:first-of-type:not([href*=".google.com/"]):not([href^="/search"]):not([role='button']):not([target='_self'])`)).filter(el => isNonIndieSite(el.href)); - console.log(nonIndieResults); const firstNonIndieResult = Array.from(nonIndieResults).filter((e) => !e.closest('g-section-with-header, div[aria-expanded], div[data-q], div[data-minw], div[data-num-cols], div[data-docid], div[data-lpage]'))[0]; if (!resultsFirstChild || !firstNonIndieResult) return; @@ -459,6 +458,9 @@ async function reorderSearchResults(searchResults, searchEngine, storage) { async function filterSearchResults(searchResults, searchEngine, storage, reorderedHrefs = []) { let countFiltered = 0; + // Add location observer to check for additional mutations + addDOMChangeObserver(startFiltering, searchEngine, storage); + for (const searchResult of searchResults) { try { // Check that result isn't within another result @@ -502,9 +504,6 @@ async function filterSearchResults(searchResults, searchEngine, storage, reorder } }; - // Add location observer to check for additional mutations - addDOMChangeObserver(startFiltering, searchEngine, storage); - // If any results were filtered, update search filter count if (countFiltered > 0) { extensionAPI.storage.sync.set({ 'countSearchFilters': (storage.countSearchFilters ?? 0) + countFiltered });