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").
pull/400/merge
Kevin Payravi 2024-01-07 04:01:46 -06:00
parent 5b3bacfbf6
commit e314db56c9
3 changed files with 45 additions and 29 deletions

View File

@ -285,25 +285,32 @@ async function main(url, tabId) {
// Get article name from the end of the URL; // Get article name from the end of the URL;
// We can't just take the last part of the path due to subpages; // 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 // 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 // Set up URL to redirect user to based on wiki platform
let newURL = ''; let newURL = '';
if (article) { if (originArticle) {
// Check if main page // Check if main page
if (article === site['origin_main_page']) { if (originArticle === site['origin_main_page']) {
article = site['destination_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 // Replace underscores with spaces as that performs better in search
article = article.replaceAll('_', ' '); destinationArticle = destinationArticle.replaceAll('_', ' ');
let searchParams = ''; let searchParams = '';
switch (site['destination_platform']) { switch (site['destination_platform']) {
case 'mediawiki': case 'mediawiki':
searchParams = '?search=' + site['destination_content_prefix'] + article; searchParams = '?search=' + site['destination_content_prefix'] + destinationArticle;
break; break;
case 'doku': case 'doku':
searchParams = 'start?do=search&q=' + article; searchParams = 'start?do=search&q=' + destinationArticle;
break; break;
} }
newURL = 'https://' + site["destination_base_url"] + site["destination_search_path"] + searchParams; newURL = 'https://' + site["destination_base_url"] + site["destination_search_path"] + searchParams;

View File

@ -410,25 +410,32 @@ function main() {
// Get article name from the end of the URL; // Get article name from the end of the URL;
// We can't just take the last part of the path due to subpages; // 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: // 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: // Set up URL to redirect user to based on wiki platform:
let newURL = ''; let newURL = '';
if (article) { if (originArticle) {
// Check if main page // Check if main page
if (article === site['origin_main_page']) { if (originArticle === site['origin_main_page']) {
article = site['destination_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 // Replace underscores with spaces as that performs better in search
article = article.replaceAll('_', ' '); destinationArticle = destinationArticle.replaceAll('_', ' ');
let searchParams = ''; let searchParams = '';
switch (site['destination_platform']) { switch (site['destination_platform']) {
case 'mediawiki': case 'mediawiki':
searchParams = '?search=' + site['destination_content_prefix'] + article; searchParams = '?search=' + site['destination_content_prefix'] + destinationArticle;
break; break;
case 'doku': case 'doku':
searchParams = 'start?do=search&q=' + article; searchParams = 'start?do=search&q=' + destinationArticle;
break; break;
} }
newURL = 'https://' + site["destination_base_url"] + site["destination_search_path"] + searchParams.replaceAll('+', '_'); newURL = 'https://' + site["destination_base_url"] + site["destination_search_path"] + searchParams.replaceAll('+', '_');

View File

@ -195,24 +195,31 @@ function escapeRegex(string) {
function replaceSearchResults(searchResultContainer, site, link) { function replaceSearchResults(searchResultContainer, site, link) {
// Build new URL: // 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 = ''; let newURL = '';
if (article) { if (originArticle) {
// Check if main page // Check if main page
if (article === site['origin_main_page']) { if (originArticle === site['origin_main_page']) {
article = site['destination_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 // Replace underscores with spaces as that performs better in search
article = article.replaceAll('_', ' '); destinationArticle = destinationArticle.replaceAll('_', ' ');
let searchParams = ''; let searchParams = '';
switch (site['destination_platform']) { switch (site['destination_platform']) {
case 'mediawiki': case 'mediawiki':
searchParams = '?search=' + site['destination_content_prefix'] + article; searchParams = '?search=' + site['destination_content_prefix'] + destinationArticle;
break; break;
case 'doku': case 'doku':
searchParams = 'start?do=search&q=' + article; searchParams = 'start?do=search&q=' + destinationArticle;
break; break;
} }
newURL = 'https://' + site['destination_base_url'] + site['destination_search_path'] + searchParams; 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); indieResultFavicon.src = chrome.runtime.getURL('favicons/' + site.lang.toLowerCase() + '/' + site.destination_icon);
indieResultFaviconContainer.append(indieResultFavicon); indieResultFaviconContainer.append(indieResultFavicon);
let indieResultText = document.createElement('span'); let indieResultText = document.createElement('span');
if (article) { if (originArticle && originArticle !== site['origin_main_page']) {
// Check if main page
if (article === site['origin_main_page']) {
article = site['destination_main_page'];
}
if (site['lang'] === 'EN' && link.match(/fandom\.com\/[a-z]{2}\/wiki\//)) { 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 { } else {
indieResultText.innerText = 'Look up "' + decodeURIComponent(decodeURIComponent(article.replaceAll('_', ' '))) + '" on ' + site.destination; indieResultText.innerText = 'Look up "' + decodeURIComponent(decodeURIComponent(destinationArticle.replaceAll('_', ' '))) + '" on ' + site.destination;
} }
} else { } else {
if (site['lang'] === 'EN' && link.match(/fandom\.com\/[a-z]{2}\/wiki\//)) { if (site['lang'] === 'EN' && link.match(/fandom\.com\/[a-z]{2}\/wiki\//)) {