From e314db56c92fb5b32029264c7dd724f17904f51d Mon Sep 17 00:00:00 2001 From: Kevin Payravi Date: Sun, 7 Jan 2024 04:01:46 -0600 Subject: [PATCH] Better handling of main page redirection For main pages, DokuWiki page title is now set to empty string to take the user directly to the main page. In addition, for main pages in search engine results, the button text is cleaner ("Visit X Wiki instead"). --- background.js | 21 ++++++++++++++------- content-banners.js | 21 ++++++++++++++------- content-search-filtering.js | 32 +++++++++++++++++--------------- 3 files changed, 45 insertions(+), 29 deletions(-) diff --git a/background.js b/background.js index 7bffc82..10a3760 100644 --- a/background.js +++ b/background.js @@ -285,25 +285,32 @@ async function main(url, tabId) { // 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 - let article = decodeURIComponent(url.split(site['origin_base_url'] + site['origin_content_path'])[1] || ''); + let originArticle = decodeURIComponent(url.split(site['origin_base_url'] + site['origin_content_path'])[1] || ''); + let destinationArticle = originArticle; // Set up URL to redirect user to based on wiki platform let newURL = ''; - if (article) { + if (originArticle) { // Check if main page - if (article === site['origin_main_page']) { - article = site['destination_main_page']; + if (originArticle === site['origin_main_page']) { + switch (site['destination_platform']) { + case 'doku': + destinationArticle = ''; + break; + default: + destinationArticle = site['destination_main_page']; + } } // Replace underscores with spaces as that performs better in search - article = article.replaceAll('_', ' '); + destinationArticle = destinationArticle.replaceAll('_', ' '); let searchParams = ''; switch (site['destination_platform']) { case 'mediawiki': - searchParams = '?search=' + site['destination_content_prefix'] + article; + searchParams = '?search=' + site['destination_content_prefix'] + destinationArticle; break; case 'doku': - searchParams = 'start?do=search&q=' + article; + searchParams = 'start?do=search&q=' + destinationArticle; break; } newURL = 'https://' + site["destination_base_url"] + site["destination_search_path"] + searchParams; diff --git a/content-banners.js b/content-banners.js index 20825ac..5d97fee 100644 --- a/content-banners.js +++ b/content-banners.js @@ -410,25 +410,32 @@ function main() { // 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: - let article = decodeURIComponent(String(origin).split(site['origin_base_url'] + site['origin_content_path'])[1] || ''); + let originArticle = decodeURIComponent(String(origin).split(site['origin_base_url'] + site['origin_content_path'])[1] || ''); + let destinationArticle = originArticle; // Set up URL to redirect user to based on wiki platform: let newURL = ''; - if (article) { + if (originArticle) { // Check if main page - if (article === site['origin_main_page']) { - article = site['destination_main_page']; + if (originArticle === site['origin_main_page']) { + switch (site['destination_platform']) { + case 'doku': + destinationArticle = ''; + break; + default: + destinationArticle = site['destination_main_page']; + } } // Replace underscores with spaces as that performs better in search - article = article.replaceAll('_', ' '); + destinationArticle = destinationArticle.replaceAll('_', ' '); let searchParams = ''; switch (site['destination_platform']) { case 'mediawiki': - searchParams = '?search=' + site['destination_content_prefix'] + article; + searchParams = '?search=' + site['destination_content_prefix'] + destinationArticle; break; case 'doku': - searchParams = 'start?do=search&q=' + article; + searchParams = 'start?do=search&q=' + destinationArticle; break; } newURL = 'https://' + site["destination_base_url"] + site["destination_search_path"] + searchParams.replaceAll('+', '_'); diff --git a/content-search-filtering.js b/content-search-filtering.js index 0304ad7..bfa5ae7 100644 --- a/content-search-filtering.js +++ b/content-search-filtering.js @@ -195,24 +195,31 @@ function escapeRegex(string) { function replaceSearchResults(searchResultContainer, site, link) { // Build new URL: - let article = decodeURIComponent((link.split(site['origin_base_url'] + site['origin_content_path'])[1] || '').split('#')[0].split('?')[0].split('&')[0]); + let originArticle = decodeURIComponent((link.split(site['origin_base_url'] + site['origin_content_path'])[1] || '').split('#')[0].split('?')[0].split('&')[0]); + let destinationArticle = originArticle; let newURL = ''; - if (article) { + if (originArticle) { // Check if main page - if (article === site['origin_main_page']) { - article = site['destination_main_page']; + if (originArticle === site['origin_main_page']) { + switch (site['destination_platform']) { + case 'doku': + destinationArticle = ''; + break; + default: + destinationArticle = site['destination_main_page']; + } } // Replace underscores with spaces as that performs better in search - article = article.replaceAll('_', ' '); + destinationArticle = destinationArticle.replaceAll('_', ' '); let searchParams = ''; switch (site['destination_platform']) { case 'mediawiki': - searchParams = '?search=' + site['destination_content_prefix'] + article; + searchParams = '?search=' + site['destination_content_prefix'] + destinationArticle; break; case 'doku': - searchParams = 'start?do=search&q=' + article; + searchParams = 'start?do=search&q=' + destinationArticle; break; } newURL = 'https://' + site['destination_base_url'] + site['destination_search_path'] + searchParams; @@ -240,16 +247,11 @@ function replaceSearchResults(searchResultContainer, site, link) { indieResultFavicon.src = chrome.runtime.getURL('favicons/' + site.lang.toLowerCase() + '/' + site.destination_icon); indieResultFaviconContainer.append(indieResultFavicon); let indieResultText = document.createElement('span'); - if (article) { - // Check if main page - if (article === site['origin_main_page']) { - article = site['destination_main_page']; - } - + if (originArticle && originArticle !== site['origin_main_page']) { if (site['lang'] === 'EN' && link.match(/fandom\.com\/[a-z]{2}\/wiki\//)) { - indieResultText.innerText = 'Look up "' + decodeURIComponent(decodeURIComponent(article.replaceAll('_', ' '))) + '" on ' + site.destination + ' (EN)'; + indieResultText.innerText = 'Look up "' + decodeURIComponent(decodeURIComponent(destinationArticle.replaceAll('_', ' '))) + '" on ' + site.destination + ' (EN)'; } else { - indieResultText.innerText = 'Look up "' + decodeURIComponent(decodeURIComponent(article.replaceAll('_', ' '))) + '" on ' + site.destination; + indieResultText.innerText = 'Look up "' + decodeURIComponent(decodeURIComponent(destinationArticle.replaceAll('_', ' '))) + '" on ' + site.destination; } } else { if (site['lang'] === 'EN' && link.match(/fandom\.com\/[a-z]{2}\/wiki\//)) {