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.
pull/765/head
Kevin Payravi 2024-07-23 17:02:19 -07:00
parent fb202486dc
commit 2221d98dc9
1 changed files with 3 additions and 4 deletions

View File

@ -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 });