More null handling + fix for Google top-mount querying
parent
ffc5f4a91f
commit
14c12c3872
|
@ -139,44 +139,41 @@ function mountToTopOfSearchResults(element) {
|
|||
switch (searchEngine) {
|
||||
case 'google':
|
||||
if (document.querySelector('#search')) {
|
||||
document.querySelector('#search').prepend(element);
|
||||
document.querySelector('#search')?.prepend(element);
|
||||
} else if (document.querySelector('#topstuff')) {
|
||||
document.querySelector('#topstuff').prepend(element);
|
||||
document.querySelector('#topstuff')?.prepend(element);
|
||||
} else if (document.querySelector('#main')) {
|
||||
var main = document.querySelector('#main');
|
||||
if (main) {
|
||||
const hveid = main.querySelector('div div[data-hveid]');
|
||||
if (hveid) {
|
||||
hveid.parentElement?.insertBefore(element, hveid);
|
||||
}
|
||||
const hveid = document.querySelector('#main div div[data-hveid]');
|
||||
if (hveid) {
|
||||
hveid.closest('#main > div')?.insertAdjacentElement('beforebegin', element);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'bing':
|
||||
var li = document.createElement('li');
|
||||
li.appendChild(element);
|
||||
document.querySelector('#b_results').prepend(li);
|
||||
document.querySelector('#b_results')?.prepend(li);
|
||||
break;
|
||||
case 'duckduckgo':
|
||||
if (document.getElementById('web_content_wrapper')) {
|
||||
var li = document.createElement('li');
|
||||
li.appendChild(element);
|
||||
document.querySelector('#web_content_wrapper ol').prepend(li);
|
||||
document.querySelector('#web_content_wrapper ol')?.prepend(li);
|
||||
} else {
|
||||
document.getElementById('links').prepend(element);
|
||||
document.getElementById('links')?.prepend(element);
|
||||
}
|
||||
break;
|
||||
case 'brave':
|
||||
document.getElementById('results').prepend(element);
|
||||
document.getElementById('results')?.prepend(element);
|
||||
break;
|
||||
case 'ecosia':
|
||||
document.querySelector('section.mainline').prepend(element);
|
||||
document.querySelector('section.mainline')?.prepend(element);
|
||||
break;
|
||||
case 'qwant':
|
||||
document.querySelector('div[data-testid=sectionWeb]').prepend(element);
|
||||
document.querySelector('div[data-testid=sectionWeb]')?.prepend(element);
|
||||
break;
|
||||
case 'startpage':
|
||||
document.querySelector('#main').prepend(element);
|
||||
document.querySelector('#main')?.prepend(element);
|
||||
break;
|
||||
case 'yandex':
|
||||
var searchResultsContainer = document.querySelector('#search-result') || document.querySelector('.main__content .content');
|
||||
|
@ -186,19 +183,19 @@ function mountToTopOfSearchResults(element) {
|
|||
if (document.querySelector('#web > ol')) {
|
||||
var li = document.createElement('li');
|
||||
li.appendChild(element);
|
||||
document.querySelector('#web > ol').prepend(li);
|
||||
document.querySelector('#web > ol')?.prepend(li);
|
||||
} else {
|
||||
document.querySelector('#main-algo').prepend(element);
|
||||
document.querySelector('#main-algo')?.prepend(element);
|
||||
}
|
||||
break;
|
||||
case 'kagi':
|
||||
document.querySelector('#main').prepend(element);
|
||||
document.querySelector('#main')?.prepend(element);
|
||||
break;
|
||||
case 'searxng':
|
||||
document.querySelector('#results').prepend(element);
|
||||
document.querySelector('#results')?.prepend(element);
|
||||
break;
|
||||
case 'whoogle':
|
||||
document.querySelector('#main').prepend(element);
|
||||
document.querySelector('#main')?.prepend(element);
|
||||
break;
|
||||
default:
|
||||
}
|
||||
|
@ -339,12 +336,12 @@ function getResultContainer(searchEngine, searchResult) {
|
|||
|
||||
switch (searchEngine) {
|
||||
case 'google':
|
||||
/** @type {HTMLElement} */
|
||||
/** @type {HTMLElement | null} */
|
||||
const closestJsController = searchResult.closest('div[jscontroller]');
|
||||
/** @type {HTMLElement} */
|
||||
/** @type {HTMLElement | null} */
|
||||
const closestDataDiv = searchResult.closest('div[data-hveid].g') || searchResult.closest('div[data-hveid]');
|
||||
// For Google search results, get the parentNode of the result container as that tends to be more reliable:
|
||||
searchResultContainer = findClosestElement(searchResult, [closestJsController, closestDataDiv]).parentElement;
|
||||
searchResultContainer = findClosestElement(searchResult, [closestJsController, closestDataDiv])?.parentElement;
|
||||
break;
|
||||
case 'bing':
|
||||
searchResultContainer = searchResult.closest('li.b_algo');
|
||||
|
@ -356,7 +353,7 @@ function getResultContainer(searchEngine, searchResult) {
|
|||
searchResultContainer = searchResult.closest('div.snippet');
|
||||
break;
|
||||
case 'ecosia':
|
||||
searchResultContainer = searchResult.closest('div.mainline__result-wrapper article').parentElement;
|
||||
searchResultContainer = searchResult.closest('div.mainline__result-wrapper article')?.parentElement;
|
||||
break;
|
||||
case 'qwant':
|
||||
if (searchResult.closest('div[data-testid=webResult]')) {
|
||||
|
@ -485,49 +482,51 @@ async function filterSearchResults(searchResults) {
|
|||
let crossLanguageSetting = storage.crossLanguage || 'off';
|
||||
const searchResultContainer = getResultContainer(searchEngine, searchResult);
|
||||
|
||||
// Handle source -> destination filtering, i.e. non-indie/commercial wikis
|
||||
let matchingNonIndieWiki = await commonFunctionFindMatchingSite(searchResultLink, crossLanguageSetting);
|
||||
if (matchingNonIndieWiki) {
|
||||
console.debug('Indie Wiki Buddy: Filtering search result:', searchResultLink);
|
||||
// Site found in db, process search result
|
||||
countFiltered += filterSearchResult(matchingNonIndieWiki, searchResult);
|
||||
processedCache.push({
|
||||
url: searchResultLink,
|
||||
isNonIndie: true,
|
||||
siteData: matchingNonIndieWiki,
|
||||
container: searchResultContainer,
|
||||
anchor: searchResult,
|
||||
});
|
||||
} else {
|
||||
// handle destination -> source, i.e. indie wikis
|
||||
let matchingIndieWiki = await commonFunctionFindMatchingSite(searchResultLink, crossLanguageSetting, true);
|
||||
|
||||
if (matchingIndieWiki) {
|
||||
console.debug('Indie Wiki Buddy: Found indie wiki for search result:', searchResultLink);
|
||||
const cacheInfo = {
|
||||
if (searchResultContainer) {
|
||||
// Handle source -> destination filtering, i.e. non-indie/commercial wikis
|
||||
let matchingNonIndieWiki = await commonFunctionFindMatchingSite(searchResultLink, crossLanguageSetting);
|
||||
if (matchingNonIndieWiki) {
|
||||
console.debug('Indie Wiki Buddy: Filtering search result:', searchResultLink);
|
||||
// Site found in db, process search result
|
||||
countFiltered += filterSearchResult(matchingNonIndieWiki, searchResult);
|
||||
processedCache.push({
|
||||
url: searchResultLink,
|
||||
isNonIndie: false,
|
||||
siteData: matchingIndieWiki,
|
||||
isNonIndie: true,
|
||||
siteData: matchingNonIndieWiki,
|
||||
container: searchResultContainer,
|
||||
anchor: searchResult,
|
||||
};
|
||||
});
|
||||
} else {
|
||||
// handle destination -> source, i.e. indie wikis
|
||||
let matchingIndieWiki = await commonFunctionFindMatchingSite(searchResultLink, crossLanguageSetting, true);
|
||||
|
||||
if ((storage.reorderResults ?? 'on') == 'on' && processedCache[0]?.isNonIndie) {
|
||||
console.debug('Indie Wiki Buddy: Reordering search result:', searchResultLink);
|
||||
swapDOMElements(searchResultContainer, processedCache[0].container);
|
||||
// now swap the cache to not reorder the same element multiple times
|
||||
const old = processedCache[0];
|
||||
processedCache[0] = cacheInfo;
|
||||
processedCache.push(old);
|
||||
if (matchingIndieWiki) {
|
||||
console.debug('Indie Wiki Buddy: Found indie wiki for search result:', searchResultLink);
|
||||
const cacheInfo = {
|
||||
url: searchResultLink,
|
||||
isNonIndie: false,
|
||||
siteData: matchingIndieWiki,
|
||||
container: searchResultContainer,
|
||||
anchor: searchResult,
|
||||
};
|
||||
|
||||
// re-filter the element that was swapped
|
||||
countFiltered += filterSearchResult(old.siteData, old.anchor);
|
||||
} else {
|
||||
processedCache.push(cacheInfo);
|
||||
};
|
||||
if ((storage.reorderResults ?? 'on') == 'on' && searchResultContainer && processedCache[0] && processedCache[0].isNonIndie && processedCache[0].container) {
|
||||
console.debug('Indie Wiki Buddy: Reordering search result:', searchResultLink);
|
||||
swapDOMElements(searchResultContainer, processedCache[0].container);
|
||||
// now swap the cache to not reorder the same element multiple times
|
||||
const old = processedCache[0];
|
||||
processedCache[0] = cacheInfo;
|
||||
processedCache.push(old);
|
||||
|
||||
// re-filter the element that was swapped
|
||||
countFiltered += filterSearchResult(old.siteData, old.anchor);
|
||||
} else {
|
||||
processedCache.push(cacheInfo);
|
||||
};
|
||||
}
|
||||
}
|
||||
searchResultContainer.classList.add('iwb-detected');
|
||||
}
|
||||
searchResultContainer?.classList.add('iwb-detected');
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Indie Wiki Buddy failed to properly parse search results with error: ', e);
|
||||
|
@ -707,7 +706,7 @@ function filterAnchors(newAnchors) {
|
|||
}
|
||||
}
|
||||
|
||||
/** @type {HTMLElement} */
|
||||
/** @type {HTMLElement | null} */
|
||||
let pageChangeDetector = null;
|
||||
|
||||
function checkRevalidate() {
|
||||
|
|
Loading…
Reference in New Issue