From cf838788b2cdb4ab3fbbbf6eae94c70b293dfd58 Mon Sep 17 00:00:00 2001 From: Kevin Payravi Date: Wed, 1 May 2024 18:59:28 -0500 Subject: [PATCH] Splitting reordering into separate loop We now have on loop that checks which results we want to re-order, and then a second loop that actually does the re-ordering. --- scripts/content-search-filtering.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/scripts/content-search-filtering.js b/scripts/content-search-filtering.js index 7f21754..c2f9513 100644 --- a/scripts/content-search-filtering.js +++ b/scripts/content-search-filtering.js @@ -548,6 +548,7 @@ async function reorderSearchResults(searchResults, searchEngine, storage) { if (!resultsFirstChild) return; let crossLanguageSetting = storage.crossLanguage || 'off'; + let resultsToSort = []; for (const searchResult of searchResults) { try { @@ -569,8 +570,7 @@ async function reorderSearchResults(searchResults, searchEngine, storage) { console.debug('Indie Wiki Buddy is not re-ordering results, as an indie wiki is already the first result.'); break; } else { - await reorderDestinationSearchResult(resultsFirstChild, searchResult); - reorderedHrefs.push(searchResultLink); + resultsToSort.push(searchResult); } } } @@ -578,6 +578,19 @@ async function reorderSearchResults(searchResults, searchEngine, storage) { console.log('Indie Wiki Buddy failed to properly re-order search results with error: ' + e); } } + + // Reverse order of resultsToSort, + // to restore top-down order. + resultsToSort = resultsToSort.reverse(); + + for (const searchResult of resultsToSort) { + try { + await reorderDestinationSearchResult(resultsFirstChild, searchResult); + reorderedHrefs.push(searchResultLink); + } catch (e) { + console.log('Indie Wiki Buddy failed to properly re-order search results with error: ' + e); + } + } } return reorderedHrefs;