Query param handling fixes + other cleanup
parent
5959f7d233
commit
9992d2ccf1
|
@ -77,8 +77,9 @@ function redirectToBreezeWiki(storage, tabId, url) {
|
|||
function processRedirect(host) {
|
||||
// Extract article from URL
|
||||
const urlFormatted = new URL(url);
|
||||
urlFormatted.search = '';
|
||||
const subdomain = urlFormatted.hostname.split(".")[0];
|
||||
const article = url.split('fandom.com/wiki/')[1].replaceAll('%20', '_');
|
||||
const article = String(urlFormatted).split('fandom.com/wiki/')[1].replaceAll('%20', '_');
|
||||
|
||||
// Perform redirect
|
||||
if (article) {
|
||||
|
@ -158,30 +159,22 @@ async function main(url, tabId) {
|
|||
this[prop] = value;
|
||||
}
|
||||
|
||||
// Check if tab is actually available
|
||||
// This is mainly to prevent background processes from triggering an event
|
||||
let sites = [];
|
||||
|
||||
chrome.storage.local.get((localStorage) => {
|
||||
chrome.storage.sync.get(async (syncStorage) => {
|
||||
const storage = { ...syncStorage, ...localStorage };
|
||||
if ((storage.power ?? 'on') === 'on') {
|
||||
let crossLanguageSetting = storage.crossLanguage || 'off';
|
||||
let matchingSite = await commonFunctionFindMatchingSite(url, crossLanguageSetting);
|
||||
|
||||
if (matchingSite) {
|
||||
// Get user's settings for the wiki
|
||||
let settings = storage.wikiSettings || {};
|
||||
let id = matchingSite['id'];
|
||||
let siteSetting = settings[id] || storage.defaultWikiAction || 'alert';
|
||||
|
||||
// Remove query paramters
|
||||
let urlObj = new URL(url);
|
||||
urlObj.search = '';
|
||||
url = String(decodeURIComponent(urlObj.toString()));
|
||||
|
||||
// Check if redirects are enabled for the site
|
||||
if (siteSetting === 'redirect') {
|
||||
let newURL = getNewURL(url, matchingSite);
|
||||
let newURL = commonFunctionGetNewURL(url, matchingSite);
|
||||
|
||||
// Perform redirect
|
||||
chrome.tabs.update(tabId, { url: newURL });
|
||||
|
|
|
@ -76,20 +76,25 @@ async function commonFunctionFindMatchingSite(site, crossLanguageSetting) {
|
|||
return matchingSite;
|
||||
}
|
||||
|
||||
function getOriginArticle(originURL, matchingSite) {
|
||||
function commonFunctionGetOriginArticle(originURL, matchingSite) {
|
||||
return decodeURIComponent(originURL.split(matchingSite['origin_base_url'] + matchingSite['origin_content_path'])[1] || '');
|
||||
}
|
||||
|
||||
function getDestinationArticle(matchingSite, article) {
|
||||
function commonFunctionGetDestinationArticle(matchingSite, article) {
|
||||
return matchingSite['destination_content_prefix'] + article;
|
||||
}
|
||||
|
||||
function getNewURL(originURL, matchingSite) {
|
||||
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
|
||||
let originArticle = getOriginArticle(originURL, matchingSite);
|
||||
let destinationArticle = getDestinationArticle(matchingSite, originArticle);
|
||||
let originArticle = commonFunctionGetOriginArticle(originURL, matchingSite);
|
||||
let destinationArticle = commonFunctionGetDestinationArticle(matchingSite, originArticle);
|
||||
// Set up URL to redirect user to based on wiki platform
|
||||
let newURL = '';
|
||||
if (originArticle) {
|
||||
|
|
|
@ -343,11 +343,6 @@ function main() {
|
|||
}
|
||||
}
|
||||
|
||||
// Remove query paramters
|
||||
let urlObj = new URL(origin);
|
||||
urlObj.search = '';
|
||||
origin = String(decodeURIComponent(urlObj.toString()));
|
||||
|
||||
commonFunctionGetSiteDataByOrigin().then(async sites => {
|
||||
let crossLanguageSetting = storage.crossLanguage || 'off';
|
||||
let matchingSite = await commonFunctionFindMatchingSite(origin, crossLanguageSetting);
|
||||
|
@ -363,7 +358,7 @@ function main() {
|
|||
|
||||
// Notify if enabled for the wiki:
|
||||
if (siteSetting === 'alert') {
|
||||
let newURL = getNewURL(origin, matchingSite);
|
||||
let newURL = commonFunctionGetNewURL(origin, matchingSite);
|
||||
|
||||
// When head elem is loaded, notify that another wiki is available
|
||||
const docObserver = new MutationObserver((mutations, mutationInstance) => {
|
||||
|
|
|
@ -159,9 +159,9 @@ function escapeRegex(string) {
|
|||
}
|
||||
|
||||
function replaceSearchResults(searchResultContainer, site, link) {
|
||||
let originArticle = getOriginArticle(link, site);
|
||||
let destinationArticle = getDestinationArticle(site, originArticle);
|
||||
let newURL = getNewURL(link, site);
|
||||
let originArticle = commonFunctionGetOriginArticle(link, site);
|
||||
let destinationArticle = commonFunctionGetDestinationArticle(site, originArticle);
|
||||
let newURL = commonFunctionGetNewURL(link, site);
|
||||
|
||||
if (searchResultContainer && !searchResultContainer.querySelector('.iwb-new-link')) {
|
||||
if (!searchResultContainer.classList.contains('iwb-detected')) {
|
||||
|
@ -186,9 +186,9 @@ function replaceSearchResults(searchResultContainer, site, link) {
|
|||
if (originArticle && originArticle !== site['origin_main_page']) {
|
||||
destinationArticleTitle = destinationArticle.replace(site['destination_content_prefix'], '').replaceAll('_', ' ');
|
||||
if (site['language'] === 'EN' && link.match(/fandom\.com\/[a-z]{2}\/wiki\//)) {
|
||||
indieResultText.innerText = 'Look up "' + decodeURIComponent(decodeURIComponent(destinationArticleTitle)) + '" on ' + site.destination + ' (EN)';
|
||||
indieResultText.innerText = 'Look up "' + decodeURIComponent(destinationArticleTitle) + '" on ' + site.destination + ' (EN)';
|
||||
} else {
|
||||
indieResultText.innerText = 'Look up "' + decodeURIComponent(decodeURIComponent(destinationArticleTitle)) + '" on ' + site.destination;
|
||||
indieResultText.innerText = 'Look up "' + decodeURIComponent(destinationArticleTitle) + '" on ' + site.destination;
|
||||
}
|
||||
} else {
|
||||
if (site['language'] === 'EN' && link.match(/fandom\.com\/[a-z]{2}\/wiki\//)) {
|
||||
|
@ -352,19 +352,15 @@ function filterSearchResults(searchResults, searchEngine, storage) {
|
|||
searchResultLink = searchResult.closest('a[href]').href;
|
||||
}
|
||||
|
||||
let urlObj = new URL(searchResultLink);
|
||||
urlObj.search = '';
|
||||
let link = String(decodeURIComponent(urlObj.toString()));
|
||||
|
||||
if (searchEngine === 'google') {
|
||||
// Break if image result:
|
||||
if (link.includes('imgurl=')) {
|
||||
if (searchResultLink.includes('imgurl=')) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let crossLanguageSetting = storage.crossLanguage || 'off';
|
||||
let matchingSite = await commonFunctionFindMatchingSite(link, crossLanguageSetting);
|
||||
let matchingSite = await commonFunctionFindMatchingSite(searchResultLink, crossLanguageSetting);
|
||||
if (matchingSite) {
|
||||
// Get user's settings for the wiki
|
||||
let id = matchingSite['id'];
|
||||
|
@ -427,7 +423,7 @@ function filterSearchResults(searchResults, searchEngine, storage) {
|
|||
if (searchFilterSetting === 'hide') {
|
||||
countFiltered += hideSearchResults(searchResultContainer, searchEngine, matchingSite, storage['hiddenResultsBanner']);
|
||||
} else {
|
||||
countFiltered += replaceSearchResults(searchResultContainer, matchingSite, link);
|
||||
countFiltered += replaceSearchResults(searchResultContainer, matchingSite, searchResultLink);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue