2024-01-10 09:21:47 +00:00
|
|
|
if (typeof importScripts !== 'undefined') {
|
2024-01-11 04:27:23 +00:00
|
|
|
importScripts('scripts/common-functions.js');
|
2024-01-10 09:21:47 +00:00
|
|
|
}
|
|
|
|
|
2024-03-21 05:09:18 +00:00
|
|
|
let cachedStorage = {};
|
|
|
|
|
2024-04-23 05:18:42 +00:00
|
|
|
function getLocalStorageData() {
|
|
|
|
// Wrap the extensionAPI.storage.sync.get method in a promise
|
|
|
|
// Needed for Firefox manifest v2
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
extensionAPI.storage.local.get(null, (items) => {
|
|
|
|
resolve(items);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSyncStorageData() {
|
|
|
|
// Wrap the extensionAPI.storage.sync.get method in a promise
|
|
|
|
// Needed for Firefox manifest v2
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
extensionAPI.storage.sync.get(null, (items) => {
|
|
|
|
resolve(items);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-03-21 05:09:18 +00:00
|
|
|
async function updateCachedStorage() {
|
2024-04-23 05:18:42 +00:00
|
|
|
let localStorage = await getLocalStorageData();
|
|
|
|
let syncStorage = await getSyncStorageData();
|
2024-03-21 05:09:18 +00:00
|
|
|
cachedStorage = {...localStorage, ...syncStorage};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getCachedStorage() {
|
|
|
|
if (Object.keys(cachedStorage).length === 0) {
|
|
|
|
await updateCachedStorage();
|
|
|
|
}
|
|
|
|
return cachedStorage;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache Chrome's storage in memory so that we don't need to make repeated calls
|
|
|
|
updateCachedStorage();
|
|
|
|
|
2023-09-18 03:46:56 +00:00
|
|
|
// Capture web requests
|
2024-04-23 05:18:42 +00:00
|
|
|
extensionAPI.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') {
|
2024-04-23 05:18:42 +00:00
|
|
|
let tabInfo = await extensionAPI.tabs.get(event.tabId);
|
2023-11-27 08:14:00 +00:00
|
|
|
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
|
|
|
},
|
2024-02-17 08:07:15 +00:00
|
|
|
{ urls: ['*://*.fandom.com/*', '*://*.wiki.fextralife.com/*', '*://*.neoseeker.com/wiki/*'], 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
|
2024-04-23 05:18:42 +00:00
|
|
|
extensionAPI.runtime.onMessage.addListener(function(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);
|
2024-03-21 05:09:18 +00:00
|
|
|
} else if (msg.action === 'getStorage') {
|
|
|
|
getCachedStorage().then((res) => {
|
2024-04-23 05:18:42 +00:00
|
|
|
sendResponse(res);
|
|
|
|
return res;
|
2024-03-21 05:09:18 +00:00
|
|
|
});
|
|
|
|
return true;
|
2023-06-23 04:34:04 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Listen for browser starting, to set initial icon state
|
2024-04-23 05:18:42 +00:00
|
|
|
extensionAPI.runtime.onStartup.addListener(() => {
|
|
|
|
extensionAPI.storage.local.get({ 'power': 'on' }, (item) => {
|
2023-06-23 04:34:04 +00:00
|
|
|
setPowerIcon(item.power);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-03-21 05:09:18 +00:00
|
|
|
// Listen for changes to stored data, and updated our cached data
|
2024-04-23 05:18:42 +00:00
|
|
|
extensionAPI.storage.onChanged.addListener(() => {
|
2024-03-21 05:09:18 +00:00
|
|
|
updateCachedStorage();
|
|
|
|
})
|
|
|
|
|
2023-09-18 09:05:46 +00:00
|
|
|
// Listen for extension installed/updating
|
2024-04-23 05:18:42 +00:00
|
|
|
extensionAPI.runtime.onInstalled.addListener(async (detail) => {
|
2023-09-18 09:05:46 +00:00
|
|
|
// Set initial icon state
|
2024-04-23 05:18:42 +00:00
|
|
|
extensionAPI.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') {
|
2024-04-23 05:18:42 +00:00
|
|
|
extensionAPI.tabs.create({ url: 'pages/settings/index.html?newinstall=true' });
|
2023-09-18 09:05:46 +00:00
|
|
|
}
|
2023-10-16 06:08:08 +00:00
|
|
|
|
2023-11-16 10:07:24 +00:00
|
|
|
// If update, open changelog if setting is enabled
|
2024-04-23 05:18:42 +00:00
|
|
|
extensionAPI.storage.sync.get({ 'openChangelog': 'off' }, (item) => {
|
2023-11-16 10:07:24 +00:00
|
|
|
if (item.openChangelog === 'on' && detail.reason === 'update') {
|
2024-04-23 05:18:42 +00:00
|
|
|
extensionAPI.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) {
|
2024-04-23 05:18:42 +00:00
|
|
|
const manifestVersion = extensionAPI.runtime.getManifest().manifest_version;
|
2023-06-23 04:34:04 +00:00
|
|
|
if (status === 'on') {
|
|
|
|
if (manifestVersion === 2) {
|
2024-04-23 05:18:42 +00:00
|
|
|
extensionAPI.browserAction.setIcon({ path: "/images/logo-128.png" });
|
2023-06-23 04:34:04 +00:00
|
|
|
} else {
|
2024-04-23 05:18:42 +00:00
|
|
|
extensionAPI.action.setIcon({ path: "/images/logo-128.png" });
|
2023-06-23 04:34:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (manifestVersion === 2) {
|
2024-04-23 05:18:42 +00:00
|
|
|
extensionAPI.browserAction.setIcon({ path: "/images/logo-off.png" });
|
2023-06-23 04:34:04 +00:00
|
|
|
} else {
|
2024-04-23 05:18:42 +00:00
|
|
|
extensionAPI.action.setIcon({ path: "/images/logo-off.png" });
|
2023-06-23 04:34:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2024-01-11 08:53:30 +00:00
|
|
|
urlFormatted.search = '';
|
2023-11-27 08:14:00 +00:00
|
|
|
const subdomain = urlFormatted.hostname.split(".")[0];
|
2024-01-11 08:53:30 +00:00
|
|
|
const article = String(urlFormatted).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) {
|
2024-04-23 05:18:42 +00:00
|
|
|
extensionAPI.tabs.update(tabId, { url: host + '/' + subdomain + '/wiki/' + article });
|
2023-02-06 00:48:53 +00:00
|
|
|
} else {
|
2024-04-23 05:18:42 +00:00
|
|
|
extensionAPI.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
|
2024-04-23 05:18:42 +00:00
|
|
|
extensionAPI.storage.sync.set({ 'countBreezeWiki': (storage.countBreezeWiki ?? 0) + 1 });
|
2023-09-18 03:46:56 +00:00
|
|
|
|
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);
|
2024-04-23 05:18:42 +00:00
|
|
|
extensionAPI.notifications.create(notifID, {
|
2023-02-06 00:48:53 +00:00
|
|
|
"type": "basic",
|
|
|
|
"iconUrl": 'images/logo-48.png',
|
2024-06-29 10:20:53 +00:00
|
|
|
"title": extensionAPI.i18n.getMessage('notificationTitleBreezeWiki'),
|
|
|
|
"message": extensionAPI.i18n.getMessage('notificationMessageBreezeWiki')
|
2023-02-06 00:48:53 +00:00
|
|
|
});
|
2023-09-18 03:46:56 +00:00
|
|
|
// Self-clear notification after 6 seconds
|
2024-04-23 05:18:42 +00:00
|
|
|
setTimeout(() => { extensionAPI.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 =>
|
2024-04-23 05:18:42 +00:00
|
|
|
extensionAPI.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) {
|
2024-04-23 05:18:42 +00:00
|
|
|
extensionAPI.storage.sync.set({ 'breezewikiHost': breezewikiMain[0].instance });
|
2023-02-06 12:23:21 +00:00
|
|
|
} else {
|
|
|
|
// If BreezeWiki.com is not available, set to a random mirror
|
2023-04-28 06:19:55 +00:00
|
|
|
try {
|
2024-04-23 05:18:42 +00:00
|
|
|
extensionAPI.storage.sync.set({ 'breezewikiHost': breezewikiHosts[Math.floor(Math.random() * breezewikiHosts.length)].instance });
|
2023-04-28 06:19:55 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.log('Indie Wiki Buddy failed to get BreezeWiki data: ' + e);
|
|
|
|
}
|
2023-02-06 12:23:21 +00:00
|
|
|
}
|
2024-04-23 05:18:42 +00:00
|
|
|
extensionAPI.storage.sync.set({ 'breezewikiHostOptions': breezewikiHosts });
|
|
|
|
extensionAPI.storage.sync.set({ 'breezewikiHostFetchTimestamp': Date.now() });
|
2023-02-06 00:48:53 +00:00
|
|
|
processRedirect(host);
|
2023-04-28 06:19:55 +00:00
|
|
|
}).catch((e) => {
|
|
|
|
console.log('Indie Wiki Buddy failed to get BreezeWiki data: ' + e);
|
2024-04-23 05:18:42 +00:00
|
|
|
extensionAPI.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) {
|
2024-03-21 05:09:18 +00:00
|
|
|
let storage = await getCachedStorage();
|
|
|
|
|
|
|
|
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 = await commonFunctionDecompressJSON(storage.wikiSettings) || {};
|
|
|
|
let id = matchingSite['id'];
|
|
|
|
let siteSetting = settings[id] || storage.defaultWikiAction || 'alert';
|
|
|
|
|
|
|
|
// Check if redirects are enabled for the site
|
|
|
|
if (siteSetting === 'redirect') {
|
|
|
|
let newURL = commonFunctionGetNewURL(url, matchingSite);
|
|
|
|
|
|
|
|
// Perform redirect
|
2024-04-23 05:18:42 +00:00
|
|
|
extensionAPI.tabs.update(tabId, { url: newURL });
|
2024-03-21 05:09:18 +00:00
|
|
|
|
|
|
|
// Increase redirect count
|
2024-04-23 05:18:42 +00:00
|
|
|
extensionAPI.storage.sync.set({ 'countRedirects': (storage.countRedirects ?? 0) + 1 });
|
2024-03-21 05:09:18 +00:00
|
|
|
|
|
|
|
// 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);
|
2024-04-23 05:18:42 +00:00
|
|
|
extensionAPI.notifications.create(notifID, {
|
2024-03-21 05:09:18 +00:00
|
|
|
"type": "basic",
|
|
|
|
"iconUrl": 'images/logo-48.png',
|
2024-06-29 10:20:53 +00:00
|
|
|
"title": extensionAPI.i18n.getMessage('notificationTitle'),
|
|
|
|
"message": extensionAPI.i18n.getMessage('notificationMessage', [matchingSite['origin'], matchingSite['destination']])
|
2024-03-21 05:09:18 +00:00
|
|
|
});
|
|
|
|
// Self-clear notification after 6 seconds
|
2024-04-23 05:18:42 +00:00
|
|
|
setTimeout(() => { extensionAPI.notifications.clear(notifID); }, 6000);
|
2023-09-18 03:46:56 +00:00
|
|
|
}
|
2024-03-21 05:09:18 +00:00
|
|
|
} else if ((storage.breezewiki ?? 'off') === 'on' || (storage.breezewiki ?? 'off') === 'redirect') {
|
|
|
|
redirectToBreezeWiki(storage, tabId, url);
|
2023-11-05 05:52:22 +00:00
|
|
|
}
|
2024-03-21 05:09:18 +00:00
|
|
|
} else if ((storage.breezewiki ?? 'off') === 'on' || (storage.breezewiki ?? 'off') === 'redirect') {
|
|
|
|
redirectToBreezeWiki(storage, tabId, url);
|
|
|
|
}
|
|
|
|
}
|
2023-02-06 00:48:53 +00:00
|
|
|
}
|