Mobile search engine filtering improvements

pull/327/head
Kevin Payravi 2023-11-05 20:25:47 -06:00
parent 5ee56c5231
commit ae70730683
1 changed files with 102 additions and 95 deletions

View File

@ -12,6 +12,8 @@ Object.prototype.set = function (prop, value) {
}
// Function to create an observer to watch for mutations on search pages
// This is used for search engines that paginate via JavaScript,
// or overwrite their results and remove IWB's elements
function addLocationObserver(callback) {
const config = {
attributes: false,
@ -190,7 +192,6 @@ function escapeRegex(string) {
}
function replaceSearchResults(searchResultContainer, site, link) {
let countFiltered = 0;
// Build new URL:
let article = link.split(site['origin_content_path'])[1]?.split('#')[0].split('?')[0].split('&')[0];
let newURL = '';
@ -214,6 +215,7 @@ function replaceSearchResults(searchResultContainer, site, link) {
searchResultContainer.classList.add('iwb-detected');
searchResultContainer.classList.add('iwb-disavow');
}
// Using aside to avoid conflicts with website CSS and listeners:
let indieContainer = document.createElement('aside');
indieContainer.classList.add('iwb-new-link-container');
let indieResultLink = document.createElement('a');
@ -260,13 +262,13 @@ function replaceSearchResults(searchResultContainer, site, link) {
indieContainer.appendChild(indieResultLink);
indieContainer.appendChild(resultControls);
searchResultContainer.prepend(indieContainer);
countFiltered++;
return 1;
}
return countFiltered;
return 0;
}
function hideSearchResults(searchResultContainer, searchEngine, site) {
let countFiltered = 0;
// Insert search result removal notice
if (!filteredWikis.includes(site.lang + ' ' + site.origin_group)) {
filteredWikis.push(site.lang + ' ' + site.origin_group);
@ -274,6 +276,7 @@ function hideSearchResults(searchResultContainer, searchEngine, site) {
let elementId = stringToId(site.lang + '-' + site.origin_group);
hiddenWikisRevealed[elementId] = false;
// Using aside to avoid conflicts with website CSS and listeners:
let searchRemovalNotice = document.createElement('aside');
searchRemovalNotice.id = 'iwb-notice-' + elementId;
searchRemovalNotice.classList.add('iwb-notice');
@ -370,13 +373,14 @@ function hideSearchResults(searchResultContainer, searchEngine, site) {
let elementId = stringToId(site.lang + '-' + site.origin_group);
searchResultContainer.classList.add('iwb-search-result-' + elementId);
searchResultContainer.classList.add('iwb-hide');
countFiltered++;
searchResultContainer.classList.add('iwb-detected');
if (hiddenWikisRevealed[elementId]) {
searchResultContainer.classList.add('iwb-show');
}
return 1;
}
return countFiltered;
return 0;
}
function filterSearchResults(searchResults, searchEngine, storage) {
@ -386,6 +390,8 @@ function filterSearchResults(searchResults, searchEngine, storage) {
for (let searchResult of searchResults) {
try {
// Check that result isn't within another result
if (!searchResult.closest('.iwb-detected')) {
let searchResultLink = '';
if (searchEngine === 'bing') {
searchResultLink = searchResult.innerHTML.replaceAll('<strong>', '').replaceAll('</strong>', '');
@ -403,9 +409,9 @@ function filterSearchResults(searchResults, searchEngine, storage) {
// Check if site is in our list of wikis:
if (crossLanguageSetting === 'on') {
matchingSites = sites.filter(el => link.replace(/^https?:\/\//, '').startsWith(el.origin_base_url));
matchingSites = sites.filter(el => link.replace(/.*https?:\/\//, '').startsWith(el.origin_base_url));
} else {
matchingSites = sites.filter(el => link.replace(/^https?:\/\//, '').startsWith(el.origin_base_url + el.origin_content_path));
matchingSites = sites.filter(el => link.replace(/.*https?:\/\//, '').startsWith(el.origin_base_url + el.origin_content_path));
}
if (matchingSites.length > 0) {
// Select match with longest base URL
@ -486,6 +492,7 @@ function filterSearchResults(searchResults, searchEngine, storage) {
}
}
}
}
} catch (e) {
console.log('Indie Wiki Buddy failed to properly parse search results with error: ' + e);
}