2024-01-10 09:21:47 +00:00
|
|
|
if (typeof importScripts !== 'undefined') {
|
|
|
|
importScripts('common-functions-general.js');
|
|
|
|
}
|
|
|
|
|
2023-09-18 03:46:56 +00:00
|
|
|
// Capture web requests
|
|
|
|
chrome.webRequest.onBeforeSendHeaders.addListener(
|
2023-12-13 10:10:55 +00:00
|
|
|
async (event) => {
|
2023-11-05 05:52:22 +00:00
|
|
|
if (event.documentLifecycle !== 'prerender') {
|
2023-11-27 08:14:00 +00:00
|
|
|
if (event.frameType === 'sub_frame') {
|
|
|
|
let tabInfo = await chrome.tabs.get(event.tabId);
|
|
|
|
main(tabInfo.url, event.tabId);
|
|
|
|
} else {
|
|
|
|
main(event.url, event.tabId);
|
|
|
|
}
|
2023-11-05 05:52:22 +00:00
|
|
|
}
|
2023-09-18 03:46:56 +00:00
|
|
|
},
|
2023-11-05 05:52:22 +00:00
|
|
|
{ urls: ['*://*.fandom.com/*', '*://*.wiki.fextralife.com/*'], types: ['main_frame', 'sub_frame'] }
|
2023-09-18 03:46:56 +00:00
|
|
|
);
|
2023-02-06 00:48:53 +00:00
|
|
|
|
2023-06-23 04:34:04 +00:00
|
|
|
// Listen for user turning extension on or off, to update icon
|
2023-12-13 10:10:55 +00:00
|
|
|
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
|
2023-09-18 03:46:56 +00:00
|
|
|
if (msg.action === 'updateIcon') {
|
2023-06-23 04:34:04 +00:00
|
|
|
setPowerIcon(msg.value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Listen for browser starting, to set initial icon state
|
2023-12-13 10:10:55 +00:00
|
|
|
chrome.runtime.onStartup.addListener(() => {
|
|
|
|
chrome.storage.local.get({ 'power': 'on' }, (item) => {
|
2023-06-23 04:34:04 +00:00
|
|
|
setPowerIcon(item.power);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-09-18 09:05:46 +00:00
|
|
|
// Listen for extension installed/updating
|
2023-12-13 10:10:55 +00:00
|
|
|
chrome.runtime.onInstalled.addListener(async (detail) => {
|
2023-09-18 09:05:46 +00:00
|
|
|
// Set initial icon state
|
2023-12-13 10:10:55 +00:00
|
|
|
chrome.storage.local.get({ 'power': 'on' }, (item) => {
|
2023-06-23 04:34:04 +00:00
|
|
|
setPowerIcon(item.power);
|
|
|
|
});
|
2023-09-18 09:05:46 +00:00
|
|
|
|
|
|
|
// If new install, open settings with starter guide
|
|
|
|
if (detail.reason === 'install') {
|
|
|
|
chrome.tabs.create({ url: 'settings.html?newinstall=true' });
|
|
|
|
}
|
2023-10-16 06:08:08 +00:00
|
|
|
|
2023-11-16 10:07:24 +00:00
|
|
|
// If update, open changelog if setting is enabled
|
2023-12-13 10:10:55 +00:00
|
|
|
chrome.storage.sync.get({ 'openChangelog': 'off' }, (item) => {
|
2023-11-16 10:07:24 +00:00
|
|
|
if (item.openChangelog === 'on' && detail.reason === 'update') {
|
2023-11-27 08:19:14 +00:00
|
|
|
chrome.tabs.create({ url: 'https://getindie.wiki/changelog/?updated=true', active: false });
|
2023-11-16 10:07:24 +00:00
|
|
|
}
|
|
|
|
});
|
2023-11-04 07:54:41 +00:00
|
|
|
|
|
|
|
// Temporary functions for 3.0 migration
|
2023-11-02 08:12:35 +00:00
|
|
|
if (detail.reason === 'update') {
|
2024-01-10 09:21:47 +00:00
|
|
|
commonFunctionMigrateToV3();
|
2023-11-02 08:12:35 +00:00
|
|
|
}
|
2023-06-23 04:34:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function setPowerIcon(status) {
|
|
|
|
const manifestVersion = chrome.runtime.getManifest().manifest_version;
|
|
|
|
if (status === 'on') {
|
|
|
|
if (manifestVersion === 2) {
|
|
|
|
chrome.browserAction.setIcon({ path: "/images/logo-128.png" });
|
|
|
|
} else {
|
|
|
|
chrome.action.setIcon({ path: "/images/logo-128.png" });
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (manifestVersion === 2) {
|
|
|
|
chrome.browserAction.setIcon({ path: "/images/logo-off.png" });
|
|
|
|
} else {
|
|
|
|
chrome.action.setIcon({ path: "/images/logo-off.png" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-27 08:14:00 +00:00
|
|
|
function redirectToBreezeWiki(storage, tabId, url) {
|
2023-02-06 00:48:53 +00:00
|
|
|
function processRedirect(host) {
|
2023-12-12 10:03:31 +00:00
|
|
|
// Extract article from URL
|
2023-11-27 08:14:00 +00:00
|
|
|
const urlFormatted = new URL(url);
|
|
|
|
const subdomain = urlFormatted.hostname.split(".")[0];
|
|
|
|
const article = url.split('fandom.com/wiki/')[1].replaceAll('%20', '_');
|
2023-09-18 03:46:56 +00:00
|
|
|
|
2023-12-12 10:03:31 +00:00
|
|
|
// Perform redirect
|
2023-02-06 00:48:53 +00:00
|
|
|
if (article) {
|
2023-11-27 08:14:00 +00:00
|
|
|
chrome.tabs.update(tabId, { url: host + '/' + subdomain + '/wiki/' + article });
|
2023-02-06 00:48:53 +00:00
|
|
|
} else {
|
2023-11-27 08:14:00 +00:00
|
|
|
chrome.tabs.update(tabId, { url: host + '/' + subdomain });
|
2023-02-06 00:48:53 +00:00
|
|
|
}
|
2023-09-18 03:46:56 +00:00
|
|
|
|
|
|
|
// Increase BreezeWiki stat count
|
|
|
|
chrome.storage.sync.set({ 'countBreezeWiki': (storage.countBreezeWiki ?? 0) + 1 });
|
|
|
|
|
2023-02-06 00:48:53 +00:00
|
|
|
if ((storage.notifications ?? 'on') === 'on') {
|
|
|
|
// Notify that user is being redirected to BreezeWiki
|
|
|
|
let notifID = 'independent-wiki-redirector-notification-' + Math.floor(Math.random() * 1E16);
|
|
|
|
chrome.notifications.create(notifID, {
|
|
|
|
"type": "basic",
|
|
|
|
"iconUrl": 'images/logo-48.png',
|
|
|
|
"title": "You've been redirected to BreezeWiki!",
|
|
|
|
"message": "Indie Wiki Buddy has sent you to BreezeWiki for a cleaner, ad-free experience on Fandom."
|
|
|
|
});
|
2023-09-18 03:46:56 +00:00
|
|
|
// Self-clear notification after 6 seconds
|
2023-12-13 10:10:55 +00:00
|
|
|
setTimeout(() => { chrome.notifications.clear(notifID); }, 6000);
|
2023-02-06 00:48:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-04 09:16:12 +00:00
|
|
|
if (url.includes('fandom.com/wiki/') && !url.includes('fandom=allow')) {
|
2023-02-06 00:48:53 +00:00
|
|
|
if (!(storage.breezewikiHost ?? null)) {
|
2023-02-06 12:23:21 +00:00
|
|
|
fetch('https://bw.getindie.wiki/instances.json')
|
2023-04-28 06:19:55 +00:00
|
|
|
.then((response) => {
|
|
|
|
if (response.ok) {
|
|
|
|
return response.json();
|
|
|
|
}
|
|
|
|
throw new Error('Indie Wiki Buddy failed to get BreezeWiki data.');
|
|
|
|
}).then((breezewikiHosts) => {
|
|
|
|
breezewikiHosts = breezewikiHosts.filter(host =>
|
|
|
|
chrome.runtime.getManifest().version.localeCompare(host.iwb_version,
|
2023-04-29 05:43:24 +00:00
|
|
|
undefined,
|
2023-04-28 06:19:55 +00:00
|
|
|
{ numeric: true, sensitivity: 'base' }
|
|
|
|
) >= 0
|
|
|
|
);
|
2023-02-06 12:23:21 +00:00
|
|
|
// Check if BreezeWiki's main site is available
|
|
|
|
let breezewikiMain = breezewikiHosts.filter(host => host.instance === 'https://breezewiki.com');
|
|
|
|
if (breezewikiMain.length > 0) {
|
|
|
|
chrome.storage.sync.set({ 'breezewikiHost': breezewikiMain[0].instance });
|
|
|
|
} else {
|
|
|
|
// If BreezeWiki.com is not available, set to a random mirror
|
2023-04-28 06:19:55 +00:00
|
|
|
try {
|
|
|
|
chrome.storage.sync.set({ 'breezewikiHost': breezewikiHosts[Math.floor(Math.random() * breezewikiHosts.length)].instance });
|
|
|
|
} catch (e) {
|
|
|
|
console.log('Indie Wiki Buddy failed to get BreezeWiki data: ' + e);
|
|
|
|
}
|
2023-02-06 12:23:21 +00:00
|
|
|
}
|
2023-02-06 00:48:53 +00:00
|
|
|
chrome.storage.sync.set({ 'breezewikiHostOptions': breezewikiHosts });
|
|
|
|
chrome.storage.sync.set({ 'breezewikiHostFetchTimestamp': Date.now() });
|
|
|
|
processRedirect(host);
|
2023-04-28 06:19:55 +00:00
|
|
|
}).catch((e) => {
|
|
|
|
console.log('Indie Wiki Buddy failed to get BreezeWiki data: ' + e);
|
2023-10-16 05:29:31 +00:00
|
|
|
chrome.storage.sync.set({ 'breezewikiHost': 'https://breezewiki.com' });
|
2023-02-06 00:48:53 +00:00
|
|
|
});
|
|
|
|
} else {
|
2023-11-27 03:00:08 +00:00
|
|
|
if (storage.breezewikiHost === 'CUSTOM') {
|
|
|
|
processRedirect(storage.breezewikiCustomHost || 'https://breezewiki.com');
|
|
|
|
} else {
|
|
|
|
processRedirect(storage.breezewikiHost);
|
|
|
|
}
|
2023-02-06 00:48:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-27 08:14:00 +00:00
|
|
|
async function main(url, tabId) {
|
2023-11-05 05:52:22 +00:00
|
|
|
// Create object prototypes for getting and setting attributes
|
|
|
|
Object.prototype.get = function (prop) {
|
|
|
|
this[prop] = this[prop] || {};
|
|
|
|
return this[prop];
|
|
|
|
};
|
|
|
|
Object.prototype.set = function (prop, value) {
|
|
|
|
this[prop] = value;
|
|
|
|
}
|
2023-09-18 03:46:56 +00:00
|
|
|
|
2023-11-05 05:52:22 +00:00
|
|
|
// Check if tab is actually available
|
|
|
|
// This is mainly to prevent background processes from triggering an event
|
|
|
|
let sites = [];
|
2023-02-06 00:48:53 +00:00
|
|
|
|
2023-12-13 10:10:55 +00:00
|
|
|
chrome.storage.local.get((localStorage) => {
|
2024-01-10 09:21:47 +00:00
|
|
|
chrome.storage.sync.get(async (syncStorage) => {
|
2023-11-05 05:52:22 +00:00
|
|
|
const storage = { ...syncStorage, ...localStorage };
|
|
|
|
if ((storage.power ?? 'on') === 'on') {
|
|
|
|
let crossLanguageSetting = storage.crossLanguage || 'off';
|
2024-01-10 09:21:47 +00:00
|
|
|
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') {
|
|
|
|
// 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']) {
|
2023-11-05 05:52:22 +00:00
|
|
|
case 'doku':
|
2024-01-10 09:21:47 +00:00
|
|
|
destinationArticle = '';
|
2023-11-05 05:52:22 +00:00
|
|
|
break;
|
2024-01-10 09:21:47 +00:00
|
|
|
default:
|
|
|
|
destinationArticle = matchingSite['destination_main_page'];
|
2023-11-05 05:52:22 +00:00
|
|
|
}
|
2023-09-18 03:46:56 +00:00
|
|
|
}
|
2023-02-06 00:48:53 +00:00
|
|
|
|
2024-01-10 09:21:47 +00:00
|
|
|
// Replace underscores with spaces as that performs better in search
|
|
|
|
destinationArticle = destinationArticle.replaceAll('_', ' ');
|
2023-06-27 08:12:42 +00:00
|
|
|
|
2024-01-10 09:21:47 +00:00
|
|
|
// 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, ' ');
|
|
|
|
}
|
2023-02-06 00:48:53 +00:00
|
|
|
|
2024-01-10 09:21:47 +00:00
|
|
|
// 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;
|
2023-02-06 00:48:53 +00:00
|
|
|
}
|
2024-01-10 09:21:47 +00:00
|
|
|
newURL = 'https://' + matchingSite["destination_base_url"] + matchingSite["destination_search_path"] + searchParams;
|
|
|
|
} else {
|
|
|
|
newURL = 'https://' + matchingSite["destination_base_url"];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform redirect
|
|
|
|
chrome.tabs.update(tabId, { url: newURL });
|
|
|
|
|
|
|
|
// Increase redirect count
|
|
|
|
chrome.storage.sync.set({ 'countRedirects': (storage.countRedirects ?? 0) + 1 });
|
|
|
|
|
|
|
|
// Notify if enabled
|
|
|
|
if ((storage.notifications ?? 'on') === 'on') {
|
|
|
|
// Notify that user is being redirected
|
|
|
|
let notifID = 'independent-wiki-redirector-notification-' + Math.floor(Math.random() * 1E16);
|
|
|
|
chrome.notifications.create(notifID, {
|
|
|
|
"type": "basic",
|
|
|
|
"iconUrl": 'images/logo-48.png',
|
|
|
|
"title": "You've been redirected!",
|
|
|
|
"message": "Indie Wiki Buddy has sent you from " + matchingSite['origin'] + " to " + matchingSite['destination']
|
|
|
|
});
|
|
|
|
// Self-clear notification after 6 seconds
|
|
|
|
setTimeout(() => { chrome.notifications.clear(notifID); }, 6000);
|
2023-02-06 00:48:53 +00:00
|
|
|
}
|
2024-01-10 09:21:47 +00:00
|
|
|
} else if ((storage.breezewiki ?? 'off') === 'on' || (storage.breezewiki ?? 'off') === 'redirect') {
|
|
|
|
redirectToBreezeWiki(storage, tabId, url);
|
2023-09-18 03:46:56 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-10 09:21:47 +00:00
|
|
|
} else if ((storage.breezewiki ?? 'off') === 'on' || (storage.breezewiki ?? 'off') === 'redirect') {
|
|
|
|
redirectToBreezeWiki(storage, tabId, url);
|
2023-11-05 05:52:22 +00:00
|
|
|
}
|
2023-02-06 00:48:53 +00:00
|
|
|
});
|
2023-11-05 05:52:22 +00:00
|
|
|
});
|
2023-02-06 00:48:53 +00:00
|
|
|
}
|