Mobile search engine filtering improvements
parent
5ee56c5231
commit
ae70730683
|
@ -12,6 +12,8 @@ Object.prototype.set = function (prop, value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to create an observer to watch for mutations on search pages
|
// 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) {
|
function addLocationObserver(callback) {
|
||||||
const config = {
|
const config = {
|
||||||
attributes: false,
|
attributes: false,
|
||||||
|
@ -190,7 +192,6 @@ function escapeRegex(string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function replaceSearchResults(searchResultContainer, site, link) {
|
function replaceSearchResults(searchResultContainer, site, link) {
|
||||||
let countFiltered = 0;
|
|
||||||
// Build new URL:
|
// Build new URL:
|
||||||
let article = link.split(site['origin_content_path'])[1]?.split('#')[0].split('?')[0].split('&')[0];
|
let article = link.split(site['origin_content_path'])[1]?.split('#')[0].split('?')[0].split('&')[0];
|
||||||
let newURL = '';
|
let newURL = '';
|
||||||
|
@ -214,6 +215,7 @@ function replaceSearchResults(searchResultContainer, site, link) {
|
||||||
searchResultContainer.classList.add('iwb-detected');
|
searchResultContainer.classList.add('iwb-detected');
|
||||||
searchResultContainer.classList.add('iwb-disavow');
|
searchResultContainer.classList.add('iwb-disavow');
|
||||||
}
|
}
|
||||||
|
// Using aside to avoid conflicts with website CSS and listeners:
|
||||||
let indieContainer = document.createElement('aside');
|
let indieContainer = document.createElement('aside');
|
||||||
indieContainer.classList.add('iwb-new-link-container');
|
indieContainer.classList.add('iwb-new-link-container');
|
||||||
let indieResultLink = document.createElement('a');
|
let indieResultLink = document.createElement('a');
|
||||||
|
@ -260,13 +262,13 @@ function replaceSearchResults(searchResultContainer, site, link) {
|
||||||
indieContainer.appendChild(indieResultLink);
|
indieContainer.appendChild(indieResultLink);
|
||||||
indieContainer.appendChild(resultControls);
|
indieContainer.appendChild(resultControls);
|
||||||
searchResultContainer.prepend(indieContainer);
|
searchResultContainer.prepend(indieContainer);
|
||||||
countFiltered++;
|
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
return countFiltered;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
function hideSearchResults(searchResultContainer, searchEngine, site) {
|
function hideSearchResults(searchResultContainer, searchEngine, site) {
|
||||||
let countFiltered = 0;
|
|
||||||
// Insert search result removal notice
|
// Insert search result removal notice
|
||||||
if (!filteredWikis.includes(site.lang + ' ' + site.origin_group)) {
|
if (!filteredWikis.includes(site.lang + ' ' + site.origin_group)) {
|
||||||
filteredWikis.push(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);
|
let elementId = stringToId(site.lang + '-' + site.origin_group);
|
||||||
hiddenWikisRevealed[elementId] = false;
|
hiddenWikisRevealed[elementId] = false;
|
||||||
|
|
||||||
|
// Using aside to avoid conflicts with website CSS and listeners:
|
||||||
let searchRemovalNotice = document.createElement('aside');
|
let searchRemovalNotice = document.createElement('aside');
|
||||||
searchRemovalNotice.id = 'iwb-notice-' + elementId;
|
searchRemovalNotice.id = 'iwb-notice-' + elementId;
|
||||||
searchRemovalNotice.classList.add('iwb-notice');
|
searchRemovalNotice.classList.add('iwb-notice');
|
||||||
|
@ -370,13 +373,14 @@ function hideSearchResults(searchResultContainer, searchEngine, site) {
|
||||||
let elementId = stringToId(site.lang + '-' + site.origin_group);
|
let elementId = stringToId(site.lang + '-' + site.origin_group);
|
||||||
searchResultContainer.classList.add('iwb-search-result-' + elementId);
|
searchResultContainer.classList.add('iwb-search-result-' + elementId);
|
||||||
searchResultContainer.classList.add('iwb-hide');
|
searchResultContainer.classList.add('iwb-hide');
|
||||||
countFiltered++;
|
searchResultContainer.classList.add('iwb-detected');
|
||||||
if (hiddenWikisRevealed[elementId]) {
|
if (hiddenWikisRevealed[elementId]) {
|
||||||
searchResultContainer.classList.add('iwb-show');
|
searchResultContainer.classList.add('iwb-show');
|
||||||
}
|
}
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return countFiltered;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
function filterSearchResults(searchResults, searchEngine, storage) {
|
function filterSearchResults(searchResults, searchEngine, storage) {
|
||||||
|
@ -386,101 +390,104 @@ function filterSearchResults(searchResults, searchEngine, storage) {
|
||||||
|
|
||||||
for (let searchResult of searchResults) {
|
for (let searchResult of searchResults) {
|
||||||
try {
|
try {
|
||||||
let searchResultLink = '';
|
// Check that result isn't within another result
|
||||||
if (searchEngine === 'bing') {
|
if (!searchResult.closest('.iwb-detected')) {
|
||||||
searchResultLink = searchResult.innerHTML.replaceAll('<strong>', '').replaceAll('</strong>', '');
|
let searchResultLink = '';
|
||||||
} else {
|
if (searchEngine === 'bing') {
|
||||||
searchResultLink = searchResult.closest('a[href]').href;
|
searchResultLink = searchResult.innerHTML.replaceAll('<strong>', '').replaceAll('</strong>', '');
|
||||||
}
|
} else {
|
||||||
let link = String(decodeURIComponent(searchResultLink));
|
searchResultLink = searchResult.closest('a[href]').href;
|
||||||
|
|
||||||
if (searchEngine === 'google') {
|
|
||||||
// Break if image result:
|
|
||||||
if (link.includes('imgurl=')) {
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
let link = String(decodeURIComponent(searchResultLink));
|
||||||
|
|
||||||
// Check if site is in our list of wikis:
|
if (searchEngine === 'google') {
|
||||||
if (crossLanguageSetting === 'on') {
|
// Break if image result:
|
||||||
matchingSites = sites.filter(el => link.replace(/^https?:\/\//, '').startsWith(el.origin_base_url));
|
if (link.includes('imgurl=')) {
|
||||||
} else {
|
break;
|
||||||
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
|
|
||||||
let closestMatch = "";
|
|
||||||
matchingSites.forEach(site => {
|
|
||||||
if (site.origin_base_url.length > closestMatch.length) {
|
|
||||||
closestMatch = site.origin_base_url;
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
let site = matchingSites.find(site => site.origin_base_url === closestMatch);
|
|
||||||
if (site) {
|
// Check if site is in our list of wikis:
|
||||||
// Get user's settings for the wiki
|
if (crossLanguageSetting === 'on') {
|
||||||
let settings = storage.searchEngineSettings || {};
|
matchingSites = sites.filter(el => link.replace(/.*https?:\/\//, '').startsWith(el.origin_base_url));
|
||||||
let id = site['id'];
|
} else {
|
||||||
let searchFilterSetting = '';
|
matchingSites = sites.filter(el => link.replace(/.*https?:\/\//, '').startsWith(el.origin_base_url + el.origin_content_path));
|
||||||
if (settings.hasOwnProperty(id) && settings[id].action) {
|
}
|
||||||
searchFilterSetting = settings[id].action;
|
if (matchingSites.length > 0) {
|
||||||
} else if (storage.defaultSearchAction) {
|
// Select match with longest base URL
|
||||||
searchFilterSetting = storage.defaultSearchAction;
|
let closestMatch = "";
|
||||||
} else {
|
matchingSites.forEach(site => {
|
||||||
searchFilterSetting = 'replace';
|
if (site.origin_base_url.length > closestMatch.length) {
|
||||||
}
|
closestMatch = site.origin_base_url;
|
||||||
if (searchFilterSetting !== 'disabled') {
|
}
|
||||||
// Output stylesheet if not already done
|
});
|
||||||
if (filteredWikis.length === 0) {
|
let site = matchingSites.find(site => site.origin_base_url === closestMatch);
|
||||||
// Wait for head to be available
|
if (site) {
|
||||||
const headElement = document.querySelector('head');
|
// Get user's settings for the wiki
|
||||||
if (headElement && !document.querySelector('.iwb-styles')) {
|
let settings = storage.searchEngineSettings || {};
|
||||||
insertCSS();
|
let id = site['id'];
|
||||||
} else {
|
let searchFilterSetting = '';
|
||||||
const docObserver = new MutationObserver(function (mutations, mutationInstance) {
|
if (settings.hasOwnProperty(id) && settings[id].action) {
|
||||||
const headElement = document.querySelector('head');
|
searchFilterSetting = settings[id].action;
|
||||||
if (headElement && !document.querySelector('.iwb-styles')) {
|
} else if (storage.defaultSearchAction) {
|
||||||
insertCSS();
|
searchFilterSetting = storage.defaultSearchAction;
|
||||||
mutationInstance.disconnect();
|
} else {
|
||||||
}
|
searchFilterSetting = 'replace';
|
||||||
});
|
}
|
||||||
docObserver.observe(document, {
|
if (searchFilterSetting !== 'disabled') {
|
||||||
childList: true,
|
// Output stylesheet if not already done
|
||||||
subtree: true
|
if (filteredWikis.length === 0) {
|
||||||
});
|
// Wait for head to be available
|
||||||
|
const headElement = document.querySelector('head');
|
||||||
|
if (headElement && !document.querySelector('.iwb-styles')) {
|
||||||
|
insertCSS();
|
||||||
|
} else {
|
||||||
|
const docObserver = new MutationObserver(function (mutations, mutationInstance) {
|
||||||
|
const headElement = document.querySelector('head');
|
||||||
|
if (headElement && !document.querySelector('.iwb-styles')) {
|
||||||
|
insertCSS();
|
||||||
|
mutationInstance.disconnect();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
docObserver.observe(document, {
|
||||||
|
childList: true,
|
||||||
|
subtree: true
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
let searchResultContainer = null;
|
let searchResultContainer = null;
|
||||||
switch (searchEngine) {
|
switch (searchEngine) {
|
||||||
case 'google':
|
case 'google':
|
||||||
searchResultContainer = searchResult.closest('div[data-hveid]');
|
searchResultContainer = searchResult.closest('div[data-hveid]');
|
||||||
break;
|
break;
|
||||||
case 'bing':
|
case 'bing':
|
||||||
searchResultContainer = searchResult.closest('li.b_algo');
|
searchResultContainer = searchResult.closest('li.b_algo');
|
||||||
break;
|
break;
|
||||||
case 'duckduckgo':
|
case 'duckduckgo':
|
||||||
searchResultContainer = searchResult.closest('li[data-layout], div.web-result');
|
searchResultContainer = searchResult.closest('li[data-layout], div.web-result');
|
||||||
break;
|
break;
|
||||||
case 'brave':
|
case 'brave':
|
||||||
searchResultContainer = searchResult.closest('div.snippet');
|
searchResultContainer = searchResult.closest('div.snippet');
|
||||||
break;
|
break;
|
||||||
case 'ecosia':
|
case 'ecosia':
|
||||||
searchResultContainer = searchResult.closest('div.mainline__result-wrapper article div.result__body');
|
searchResultContainer = searchResult.closest('div.mainline__result-wrapper article div.result__body');
|
||||||
break;
|
break;
|
||||||
case 'startpage':
|
case 'startpage':
|
||||||
searchResultContainer = searchResult.closest('div.w-gl__result');
|
searchResultContainer = searchResult.closest('div.w-gl__result');
|
||||||
break;
|
break;
|
||||||
case 'yahoo':
|
case 'yahoo':
|
||||||
searchResultContainer = searchResult.closest('#web > ol > li div.itm .exp, #web > ol > li div.algo, #web > ol > li, section.algo');
|
searchResultContainer = searchResult.closest('#web > ol > li div.itm .exp, #web > ol > li div.algo, #web > ol > li, section.algo');
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
if (searchResultContainer) {
|
if (searchResultContainer) {
|
||||||
if (searchFilterSetting === 'hide') {
|
if (searchFilterSetting === 'hide') {
|
||||||
countFiltered += hideSearchResults(searchResultContainer, searchEngine, site);
|
countFiltered += hideSearchResults(searchResultContainer, searchEngine, site);
|
||||||
} else {
|
} else {
|
||||||
countFiltered += replaceSearchResults(searchResultContainer, site, link);
|
countFiltered += replaceSearchResults(searchResultContainer, site, link);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue