Fixes and improvements for Bing filtering

Resolves issue with Bing truncating links
pull/443/head
Kevin Payravi 2024-01-24 03:18:19 -07:00
parent cf48a5d568
commit 8242e203e4
2 changed files with 18 additions and 6 deletions

View File

@ -57,7 +57,10 @@ async function commonFunctionFindMatchingSite(site, crossLanguageSetting) {
if (crossLanguageSetting === 'on') {
matchingSites = sites.filter(el => site.replace(/^https?:\/\//, '').startsWith(el.origin_base_url));
} else {
matchingSites = sites.filter(el => site.replace(/^https?:\/\//, '').startsWith(el.origin_base_url + el.origin_content_path));
matchingSites = sites.filter(el =>
site.replace(/^https?:\/\//, '').startsWith(el.origin_base_url + el.origin_content_path)
|| site.replace(/^https?:\/\//, '') === el.origin_base_url
);
}
if (matchingSites.length > 0) {
// Select match with longest base URL
@ -85,11 +88,6 @@ function commonFunctionGetDestinationArticle(matchingSite, article) {
}
function commonFunctionGetNewURL(originURL, matchingSite) {
// Remove query paramters
let urlObj = new URL(originURL);
urlObj.search = '';
originURL = String(decodeURIComponent(urlObj.toString()));
// Get article name from the end of the URL;
// We can't just take the last part of the path due to subpages;
// Instead, we take everything after the wiki's base URL + content path

View File

@ -347,6 +347,19 @@ function filterSearchResults(searchResults, searchEngine, storage) {
let searchResultLink = '';
if (searchEngine === 'bing') {
searchResultLink = searchResult.innerHTML.replaceAll('<strong>', '').replaceAll('</strong>', '');
// Bing sometimes truncates links
// When this happens, we try to pull the article name from the link title
if (searchResultLink.endsWith('…')) {
let resultTitle = searchResult.closest('li')?.querySelector('h2 a')?.innerText || '';
let article = resultTitle.substring(0, resultTitle.indexOf('|')).trim() || '';
if (searchResultLink.includes('.fandom.com/') && searchResultLink.includes('/wiki/')) {
searchResultLink = searchResultLink.split('/wiki/')[0] + '/wiki/' + article;
} else if (searchResultLink.includes('.wiki.fextralife.com/')) {
searchResultLink = searchResultLink.split('.wiki.fextralife.com/')[0] + '.wiki.fextralife.com/' + article;
}
}
} else {
searchResultLink = searchResult.closest('a[href]').href;
}
@ -486,6 +499,7 @@ function main(mutations = null, observer = null) {
} else if (currentURL.hostname.endsWith('.bing.com')) {
// Function to filter search results in Bing
function filterBing() {
// Since Bing obfuscates links, we grab the link from the cite element that displays the plaintext link
let searchResults = Array.from(document.querySelectorAll('.b_attribution>cite')).filter(el =>
el.innerHTML.replaceAll('<strong>', '').replaceAll('</strong>', '').includes('fandom.com')
|| el.innerHTML.replaceAll('<strong>', '').replaceAll('</strong>', '').includes('fextralife.com')