Moving more common functions into shared file

pull/433/head
Kevin Payravi 2024-01-10 23:40:59 -06:00
parent 42bd9b7d1a
commit 3919fbc913
4 changed files with 62 additions and 128 deletions

View File

@ -181,50 +181,7 @@ async function main(url, tabId) {
// Check if redirects are enabled for the site
if (siteSetting === 'redirect') {
// 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 originArticle = decodeURIComponent(url.split(matchingSite['origin_base_url'] + matchingSite['origin_content_path'])[1] || '');
let destinationArticle = matchingSite['destination_content_prefix'] + originArticle;
// Set up URL to redirect user to based on wiki platform
let newURL = '';
if (originArticle) {
// Check if main page
if (originArticle === matchingSite['origin_main_page']) {
switch (matchingSite['destination_platform']) {
case 'doku':
destinationArticle = '';
break;
default:
destinationArticle = matchingSite['destination_main_page'];
}
}
// Replace underscores with spaces as that performs better in search
destinationArticle = destinationArticle.replaceAll('_', ' ');
// If a Fextralife wiki, replace plus signs with spaces
// When there are multiple plus signs together, this regex will only replace only the first
if (matchingSite['origin_base_url'].includes('.wiki.fextralife.com')) {
destinationArticle = destinationArticle.replace(/(?<!\+)\+/g, ' ');
}
// Encode article
destinationArticle = encodeURIComponent(destinationArticle);
let searchParams = '';
switch (matchingSite['destination_platform']) {
case 'mediawiki':
searchParams = '?search=' + destinationArticle;
break;
case 'doku':
searchParams = 'start?do=search&q=' + destinationArticle;
break;
}
newURL = 'https://' + matchingSite["destination_base_url"] + matchingSite["destination_search_path"] + searchParams;
} else {
newURL = 'https://' + matchingSite["destination_base_url"];
}
let newURL = getNewURL(url, matchingSite);
// Perform redirect
chrome.tabs.update(tabId, { url: newURL });

View File

@ -76,6 +76,63 @@ async function commonFunctionFindMatchingSite(site, crossLanguageSetting) {
return matchingSite;
}
function getOriginArticle(originURL, matchingSite) {
return decodeURIComponent(originURL.split(matchingSite['origin_base_url'] + matchingSite['origin_content_path'])[1] || '');
}
function getDestinationArticle(matchingSite, article) {
return matchingSite['destination_content_prefix'] + article;
}
function getNewURL(originURL, matchingSite) {
// 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 originArticle = getOriginArticle(originURL, matchingSite);
let destinationArticle = getDestinationArticle(matchingSite, originArticle);
// Set up URL to redirect user to based on wiki platform
let newURL = '';
if (originArticle) {
// Check if main page
if (originArticle === matchingSite['origin_main_page']) {
switch (matchingSite['destination_platform']) {
case 'doku':
destinationArticle = '';
break;
default:
destinationArticle = matchingSite['destination_main_page'];
}
}
// Replace underscores with spaces as that performs better in search
destinationArticle = destinationArticle.replaceAll('_', ' ');
// If a Fextralife wiki, replace plus signs with spaces
// When there are multiple plus signs together, this regex will only replace only the first
if (matchingSite['origin_base_url'].includes('.wiki.fextralife.com')) {
destinationArticle = destinationArticle.replace(/(?<!\+)\+/g, ' ');
}
// Encode article
destinationArticle = encodeURIComponent(destinationArticle);
let searchParams = '';
switch (matchingSite['destination_platform']) {
case 'mediawiki':
searchParams = '?search=' + destinationArticle;
break;
case 'doku':
searchParams = 'start?do=search&q=' + destinationArticle;
break;
}
newURL = 'https://' + matchingSite["destination_base_url"] + matchingSite["destination_search_path"] + searchParams;
} else {
newURL = 'https://' + matchingSite["destination_base_url"];
}
return newURL;
}
// Temporary function to migrate user data to IWB version 3.0+
async function commonFunctionMigrateToV3() {
await chrome.storage.sync.get(async (storage) => {

View File

@ -363,50 +363,8 @@ function main() {
// Notify if enabled for the wiki:
if (siteSetting === 'alert') {
// 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 originArticle = decodeURIComponent(origin.split(matchingSite['origin_base_url'] + matchingSite['origin_content_path'])[1] || '');
let destinationArticle = matchingSite['destination_content_prefix'] + originArticle;
// Set up URL to redirect user to based on wiki platform:
let newURL = '';
if (originArticle) {
// Check if main page
if (originArticle === matchingSite['origin_main_page']) {
switch (matchingSite['destination_platform']) {
case 'doku':
destinationArticle = '';
break;
default:
destinationArticle = matchingSite['destination_main_page'];
}
}
let newURL = getNewURL(origin, matchingSite);
// Replace underscores with spaces as that performs better in search
destinationArticle = destinationArticle.replaceAll('_', ' ');
// If a Fextralife wiki, replace plus signs with spaces
// When there are multiple plus signs together, this regex will only replace only the first
if (matchingSite['origin_base_url'].includes('.wiki.fextralife.com')) {
destinationArticle = destinationArticle.replace(/(?<!\+)\+/g, ' ');
}
// Encode article
destinationArticle = encodeURIComponent(destinationArticle);
let searchParams = '';
switch (matchingSite['destination_platform']) {
case 'mediawiki':
searchParams = '?search=' + destinationArticle;
break;
case 'doku':
searchParams = 'start?do=search&q=' + destinationArticle;
break;
}
newURL = 'https://' + matchingSite["destination_base_url"] + matchingSite["destination_search_path"] + searchParams;
} else {
newURL = 'https://' + matchingSite["destination_base_url"];
}
// When head elem is loaded, notify that another wiki is available
const docObserver = new MutationObserver((mutations, mutationInstance) => {
const headElement = document.querySelector('head');

View File

@ -159,47 +159,9 @@ function escapeRegex(string) {
}
function replaceSearchResults(searchResultContainer, site, link) {
// Build new URL:
let originArticle = decodeURIComponent(link.split(site['origin_base_url'] + site['origin_content_path'])[1] || '');
let destinationArticle = site['destination_content_prefix'] + originArticle;
let newURL = '';
if (originArticle) {
// Check if 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
destinationArticle = destinationArticle.replaceAll('_', ' ');
// If a Fextralife wiki, replace plus signs with spaces
// When there are multiple plus signs together, this regex will only replace only the first
if (site['origin_base_url'].includes('.wiki.fextralife.com')) {
destinationArticle = destinationArticle.replace(/(?<!\+)\+/g, ' ');
}
// Encode article
destinationArticle = encodeURIComponent(destinationArticle);
let searchParams = '';
switch (site['destination_platform']) {
case 'mediawiki':
searchParams = '?search=' + destinationArticle;
break;
case 'doku':
searchParams = 'start?do=search&q=' + destinationArticle;
break;
}
newURL = 'https://' + site['destination_base_url'] + site['destination_search_path'] + searchParams;
} else {
newURL = 'https://' + site['destination_base_url'];
}
let originArticle = getOriginArticle(link, site);
let destinationArticle = getDestinationArticle(site, originArticle);
let newURL = getNewURL(link, site);
if (searchResultContainer && !searchResultContainer.querySelector('.iwb-new-link')) {
if (!searchResultContainer.classList.contains('iwb-detected')) {