2023-07-26 18:04:29 +00:00
|
|
|
const breezeWikiRegex = /breezewiki\.com$|breeze\.hostux\.net$|bw\.projectsegfau\.lt$|antifandom\.com$|breezewiki\.pussthecat\.org$|bw\.vern\.cc$|breezewiki\.esmailelbob\.xyz$|bw\.artemislena\.eu$|bw\.hamstro\.dev$|nerd\.whatever\.social$|breeze\.nohost\.network$|breeze\.whateveritworks\.org$/;
|
2023-02-06 00:48:53 +00:00
|
|
|
const currentURL = new URL(document.location);
|
|
|
|
|
|
|
|
// 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-04-12 07:19:33 +00:00
|
|
|
// Load website data:
|
2023-02-06 00:48:53 +00:00
|
|
|
async function getData() {
|
2023-03-09 08:44:32 +00:00
|
|
|
const LANGS = ["DE", "EN", "ES", "FR", "IT", "PL", "TOK"];
|
2023-02-06 00:48:53 +00:00
|
|
|
let sites = [];
|
|
|
|
let promises = [];
|
|
|
|
for (let i = 0; i < LANGS.length; i++) {
|
|
|
|
promises.push(fetch(chrome.runtime.getURL('data/sites' + LANGS[i] + '.json'))
|
|
|
|
.then((resp) => resp.json())
|
|
|
|
.then(function (jsonData) {
|
2023-04-12 07:19:33 +00:00
|
|
|
jsonData.forEach((site) => {
|
|
|
|
site.origins.forEach((origin) => {
|
|
|
|
sites.push({
|
|
|
|
"id": site.id,
|
|
|
|
"origin": origin.origin,
|
|
|
|
"origin_base_url": origin.origin_base_url,
|
|
|
|
"origin_content_path": origin.origin_content_path,
|
|
|
|
"destination": site.destination,
|
2023-07-17 05:17:50 +00:00
|
|
|
"destination_base_url": site.destination_base_url,
|
|
|
|
"destination_content_path": site.destination_content_path,
|
2023-07-17 04:41:11 +00:00
|
|
|
"destination_content_prefix": (site.destination_content_prefix ? site.destination_content_prefix : ""),
|
2023-07-17 05:17:50 +00:00
|
|
|
"destination_platform": site.destination_platform,
|
|
|
|
"destination_icon": site.destination_icon,
|
2023-04-12 07:19:33 +00:00
|
|
|
"lang": LANGS[i]
|
|
|
|
})
|
|
|
|
})
|
|
|
|
});
|
2023-02-06 00:48:53 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
await Promise.all(promises);
|
|
|
|
return sites;
|
|
|
|
}
|
|
|
|
|
2023-11-05 05:52:22 +00:00
|
|
|
function displayRedirectBanner(origin, newUrl, id, destinationName, destinationLanguage, storage) {
|
2023-06-26 01:49:04 +00:00
|
|
|
// Output CSS
|
|
|
|
styleString = `
|
|
|
|
#indie-wiki-banner {
|
|
|
|
font-family: sans-serif;
|
|
|
|
width: 100%;
|
|
|
|
z-index: 2147483647;
|
2023-06-30 06:44:20 +00:00
|
|
|
position: sticky;
|
|
|
|
top: 0;
|
2023-06-26 01:49:04 +00:00
|
|
|
text-align: center;
|
|
|
|
background-color: #acdae2;
|
2023-07-24 08:08:44 +00:00
|
|
|
padding: 8px 10px;
|
2023-06-26 01:49:04 +00:00
|
|
|
}
|
|
|
|
#indie-wiki-banner-exit {
|
|
|
|
float: right;
|
2023-07-24 08:08:44 +00:00
|
|
|
font-size: 20px;
|
2023-06-26 01:49:04 +00:00
|
|
|
color: #333;
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
2023-06-30 06:44:20 +00:00
|
|
|
#indie-wiki-banner-controls {
|
2023-07-24 08:08:44 +00:00
|
|
|
padding-bottom: 5px;
|
2023-06-30 06:44:20 +00:00
|
|
|
}
|
2023-07-01 06:11:13 +00:00
|
|
|
.indie-wiki-banner-big-text {
|
|
|
|
font-size: 14px;
|
|
|
|
line-height: 24px;
|
|
|
|
margin-top: 5px;
|
|
|
|
}
|
2023-06-26 01:49:04 +00:00
|
|
|
.indie-wiki-banner-link {
|
2023-06-30 06:44:20 +00:00
|
|
|
font-size: 16px;
|
2023-06-26 01:49:04 +00:00
|
|
|
font-weight: 600;
|
2023-07-01 06:11:13 +00:00
|
|
|
color: #000080;
|
2023-06-26 01:49:04 +00:00
|
|
|
cursor: pointer;
|
|
|
|
padding: 0 10px;
|
|
|
|
display: block;
|
|
|
|
width: fit-content;
|
|
|
|
margin: 0 auto;
|
|
|
|
}
|
|
|
|
.indie-wiki-banner-link:hover {
|
|
|
|
text-decoration: underline;
|
2023-07-01 06:11:13 +00:00
|
|
|
color: #000080;
|
2023-06-26 01:49:04 +00:00
|
|
|
}
|
|
|
|
.indie-wiki-banner-link-small {
|
|
|
|
display: inline-block;
|
2023-07-24 08:08:44 +00:00
|
|
|
font-size: 12px;
|
2023-06-26 01:49:04 +00:00
|
|
|
min-width: 180px;
|
|
|
|
}
|
|
|
|
.indie-wiki-banner-disabled {
|
|
|
|
color: #333;
|
|
|
|
cursor: default;
|
|
|
|
}
|
|
|
|
.indie-wiki-banner-disabled:hover {
|
|
|
|
text-decoration: none;
|
|
|
|
}
|
|
|
|
.indie-wiki-banner-hidden {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
`
|
|
|
|
style = document.createElement('style');
|
|
|
|
style.textContent = styleString;
|
|
|
|
document.head.append(style);
|
|
|
|
|
2023-02-06 00:48:53 +00:00
|
|
|
// Output banner
|
|
|
|
var banner = document.createElement('div');
|
|
|
|
banner.id = 'indie-wiki-banner';
|
|
|
|
var bannerExit = document.createElement('div');
|
2023-06-26 01:49:04 +00:00
|
|
|
bannerExit.id = 'indie-wiki-banner-exit';
|
2023-02-06 00:48:53 +00:00
|
|
|
banner.appendChild(bannerExit);
|
2023-08-07 06:44:21 +00:00
|
|
|
bannerExit.textContent = '✕';
|
2023-02-06 00:48:53 +00:00
|
|
|
bannerExit.onclick = function () { this.parentElement.remove(); };
|
2023-06-26 01:49:04 +00:00
|
|
|
|
2023-06-30 06:44:20 +00:00
|
|
|
// Output control links container
|
|
|
|
var bannerControls = document.createElement('div');
|
|
|
|
bannerControls.id = 'indie-wiki-banner-controls';
|
|
|
|
banner.appendChild(bannerControls);
|
|
|
|
|
2023-06-26 01:49:04 +00:00
|
|
|
// Output "restore banner" link
|
|
|
|
var bannerRestoreLink = document.createElement('div');
|
|
|
|
bannerRestoreLink.id = 'indie-wiki-banner-restore';
|
|
|
|
bannerRestoreLink.classList.add('indie-wiki-banner-link');
|
|
|
|
bannerRestoreLink.classList.add('indie-wiki-banner-link-small');
|
|
|
|
bannerRestoreLink.classList.add('indie-wiki-banner-hidden');
|
2023-08-07 06:44:21 +00:00
|
|
|
bannerRestoreLink.textContent = '⎌ Restore banner';
|
2023-06-30 06:44:20 +00:00
|
|
|
bannerControls.appendChild(bannerRestoreLink);
|
2023-06-26 01:49:04 +00:00
|
|
|
bannerRestoreLink.onclick = function (e) {
|
2023-11-04 07:54:41 +00:00
|
|
|
chrome.storage.sync.get({ 'wikiSettings': {} }, function (response) {
|
|
|
|
response.wikiSettings.set(id, 'alert');
|
|
|
|
chrome.storage.sync.set({ 'wikiSettings': response.wikiSettings });
|
2023-08-07 06:44:21 +00:00
|
|
|
e.target.textContent = '✓ Banner restored';
|
2023-06-26 01:49:04 +00:00
|
|
|
e.target.classList.add('indie-wiki-banner-disabled');
|
2023-08-07 06:44:21 +00:00
|
|
|
document.getElementById('indie-wiki-banner-redirect').textContent = '↪ Auto redirect this wiki';
|
2023-06-26 01:49:04 +00:00
|
|
|
document.getElementById('indie-wiki-banner-redirect').classList.remove('indie-wiki-banner-disabled');
|
2023-08-07 06:44:21 +00:00
|
|
|
document.getElementById('indie-wiki-banner-disable').textContent = '✕ Disable banner for this wiki';
|
2023-06-26 01:49:04 +00:00
|
|
|
document.getElementById('indie-wiki-banner-disable').classList.remove('indie-wiki-banner-disabled');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output "disable banner" link
|
|
|
|
var bannerDisableLink = document.createElement('div');
|
|
|
|
bannerDisableLink.id = 'indie-wiki-banner-disable';
|
|
|
|
bannerDisableLink.classList.add('indie-wiki-banner-link');
|
|
|
|
bannerDisableLink.classList.add('indie-wiki-banner-link-small');
|
2023-08-07 06:44:21 +00:00
|
|
|
bannerDisableLink.textContent = '✕ Disable banner for this wiki';
|
2023-06-30 06:44:20 +00:00
|
|
|
bannerControls.appendChild(bannerDisableLink);
|
2023-06-26 01:49:04 +00:00
|
|
|
bannerDisableLink.onclick = function (e) {
|
2023-11-04 07:54:41 +00:00
|
|
|
chrome.storage.sync.get({ 'wikiSettings': {} }, function (response) {
|
|
|
|
response.wikiSettings.set(id, 'disabled');
|
|
|
|
chrome.storage.sync.set({ 'wikiSettings': response.wikiSettings });
|
2023-08-07 06:44:21 +00:00
|
|
|
e.target.textContent = '✓ Banner disabled';
|
2023-06-26 01:49:04 +00:00
|
|
|
e.target.classList.add('indie-wiki-banner-disabled');
|
2023-08-07 06:44:21 +00:00
|
|
|
document.getElementById('indie-wiki-banner-redirect').textContent = '↪ Auto redirect this wiki';
|
2023-06-26 01:49:04 +00:00
|
|
|
document.getElementById('indie-wiki-banner-redirect').classList.remove('indie-wiki-banner-disabled');
|
2023-08-07 06:44:21 +00:00
|
|
|
document.getElementById('indie-wiki-banner-restore').textContent = '⎌ Restore banner';
|
2023-06-26 01:49:04 +00:00
|
|
|
document.getElementById('indie-wiki-banner-restore').classList.remove('indie-wiki-banner-hidden');
|
|
|
|
document.getElementById('indie-wiki-banner-restore').classList.remove('indie-wiki-banner-disabled');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output "auto redirect" link
|
|
|
|
var bannerRedirectLink = document.createElement('div');
|
|
|
|
bannerRedirectLink.id = 'indie-wiki-banner-redirect';
|
|
|
|
bannerRedirectLink.classList.add('indie-wiki-banner-link');
|
|
|
|
bannerRedirectLink.classList.add('indie-wiki-banner-link-small');
|
2023-08-07 06:44:21 +00:00
|
|
|
bannerRedirectLink.textContent = '↪ Auto redirect this wiki';
|
2023-06-30 06:44:20 +00:00
|
|
|
bannerControls.appendChild(bannerRedirectLink);
|
2023-06-26 01:49:04 +00:00
|
|
|
bannerRedirectLink.onclick = function (e) {
|
2023-11-04 07:54:41 +00:00
|
|
|
chrome.storage.sync.get({ 'wikiSettings': {} }, function (response) {
|
|
|
|
response.wikiSettings.set(id, 'redirect');
|
|
|
|
chrome.storage.sync.set({ 'wikiSettings': response.wikiSettings });
|
2023-08-07 06:44:21 +00:00
|
|
|
e.target.textContent = '✓ Redirect enabled';
|
2023-06-26 01:49:04 +00:00
|
|
|
e.target.classList.add('indie-wiki-banner-disabled');
|
2023-08-07 06:44:21 +00:00
|
|
|
document.getElementById('indie-wiki-banner-disable').textContent = '✕ Disable banner for this wiki';
|
2023-06-26 01:49:04 +00:00
|
|
|
document.getElementById('indie-wiki-banner-disable').classList.remove('indie-wiki-banner-disabled');
|
2023-08-07 06:44:21 +00:00
|
|
|
document.getElementById('indie-wiki-banner-restore').textContent = '⎌ Restore banner';
|
2023-06-26 01:49:04 +00:00
|
|
|
document.getElementById('indie-wiki-banner-restore').classList.remove('indie-wiki-banner-hidden');
|
|
|
|
document.getElementById('indie-wiki-banner-restore').classList.remove('indie-wiki-banner-disabled');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output main banner text
|
2023-02-06 00:48:53 +00:00
|
|
|
var bannerText = document.createElement('span');
|
2023-06-30 06:44:20 +00:00
|
|
|
bannerText.classList.add('indie-wiki-banner-big-text');
|
2023-02-06 00:48:53 +00:00
|
|
|
banner.appendChild(bannerText);
|
2023-11-05 05:52:22 +00:00
|
|
|
if (destinationLanguage === 'EN' && origin.href.match(/fandom\.com\/[a-z]{2}\/wiki\//)) {
|
|
|
|
bannerText.textContent = 'There is an independent wiki covering this topic in English!';
|
|
|
|
} else {
|
|
|
|
bannerText.textContent = 'There is an independent wiki covering this topic!';
|
|
|
|
}
|
2023-06-26 01:49:04 +00:00
|
|
|
var bannerWikiLink = document.createElement('a');
|
|
|
|
bannerWikiLink.classList.add('indie-wiki-banner-link');
|
|
|
|
bannerText.appendChild(bannerWikiLink);
|
2023-11-05 05:52:22 +00:00
|
|
|
bannerWikiLink.href = newUrl;
|
|
|
|
bannerWikiLink.textContent = 'Visit ' + destinationName + ' →';
|
2023-06-26 01:49:04 +00:00
|
|
|
|
2023-09-21 06:37:17 +00:00
|
|
|
// Function to insert banner into DOM before body element
|
|
|
|
function addBannerToDOM() {
|
|
|
|
// Check if document is in a ready state
|
|
|
|
if (document.readyState === 'interactive' || document.readyState === 'complete') {
|
2023-09-21 06:44:50 +00:00
|
|
|
// Ensure banner isn't already outputted
|
|
|
|
if (!document.querySelector(':root > #indie-wiki-banner')) {
|
|
|
|
document.body.insertAdjacentElement('beforeBegin', banner);
|
2023-09-21 06:37:17 +00:00
|
|
|
|
2023-09-21 06:44:50 +00:00
|
|
|
// Increment banner count
|
|
|
|
if (storage.breezewiki === 'on') {
|
|
|
|
if (currentURL.hostname.match(breezeWikiRegex)) {
|
|
|
|
chrome.storage.sync.set({ 'countAlerts': (storage.countAlerts ?? 0) + 1 });
|
|
|
|
}
|
|
|
|
} else {
|
2023-02-06 00:48:53 +00:00
|
|
|
chrome.storage.sync.set({ 'countAlerts': (storage.countAlerts ?? 0) + 1 });
|
|
|
|
}
|
|
|
|
}
|
2023-09-21 06:44:50 +00:00
|
|
|
|
|
|
|
// Remove readystatechange listener
|
|
|
|
document.removeEventListener('readystatechange', addBannerToDOM);
|
2023-09-21 06:37:17 +00:00
|
|
|
}
|
2023-02-06 00:48:53 +00:00
|
|
|
}
|
2023-09-21 06:37:17 +00:00
|
|
|
|
|
|
|
document.addEventListener('readystatechange', addBannerToDOM);
|
|
|
|
addBannerToDOM();
|
2023-02-06 00:48:53 +00:00
|
|
|
}
|
|
|
|
|
2023-07-25 06:17:46 +00:00
|
|
|
function main() {
|
2023-04-28 05:17:29 +00:00
|
|
|
chrome.storage.local.get(function (localStorage) {
|
|
|
|
chrome.storage.sync.get(function (syncStorage) {
|
2023-07-17 05:17:50 +00:00
|
|
|
const storage = { ...syncStorage, ...localStorage };
|
2023-04-28 05:17:29 +00:00
|
|
|
// Check if extension is on:
|
|
|
|
if ((storage.power ?? 'on') === 'on') {
|
2023-07-25 06:17:46 +00:00
|
|
|
// Check if there is a pathname, to ensure we're looking at an article
|
|
|
|
if (currentURL.pathname.length > 1) {
|
2023-04-28 05:17:29 +00:00
|
|
|
let origin = currentURL;
|
|
|
|
// If on a BreezeWiki site, convert to Fandom link to match with our list of wikis:
|
|
|
|
if (currentURL.hostname.match(breezeWikiRegex)) {
|
|
|
|
origin = String(currentURL.pathname).split('/')[1] + '.fandom.com/wiki/';
|
|
|
|
if (currentURL.search.includes('?q=')) {
|
|
|
|
origin = origin + currentURL.search.substring(3).split('&')[0];
|
|
|
|
} else {
|
|
|
|
origin = origin + currentURL.pathname.split('/')[3];
|
|
|
|
}
|
2023-02-06 00:48:53 +00:00
|
|
|
}
|
2023-04-28 05:17:29 +00:00
|
|
|
getData().then(sites => {
|
2023-11-05 05:52:22 +00:00
|
|
|
let crossLanguageSetting = storage.crossLanguage || 'off';
|
2023-04-28 05:17:29 +00:00
|
|
|
// Check if site is in our list of wikis:
|
2023-11-05 05:52:22 +00:00
|
|
|
// let matchingSites = sites.filter(el => String(origin).replace(/^https?:\/\//, '').startsWith(el.origin_base_url));
|
|
|
|
let matchingSites = [];
|
|
|
|
if (crossLanguageSetting === 'on') {
|
|
|
|
matchingSites = sites.filter(el => String(origin).replace(/^https?:\/\//, '').startsWith(el.origin_base_url));
|
|
|
|
} else {
|
|
|
|
matchingSites = sites.filter(el => String(origin).replace(/^https?:\/\//, '').startsWith(el.origin_base_url + el.origin_content_path));
|
|
|
|
}
|
2023-04-28 05:17:29 +00:00
|
|
|
if (matchingSites.length > 0) {
|
|
|
|
// Select match with longest base URL
|
2023-11-05 05:52:22 +00:00
|
|
|
let closestMatch = '';
|
2023-04-28 05:17:29 +00:00
|
|
|
matchingSites.forEach(site => {
|
|
|
|
if (site.origin_base_url.length > closestMatch.length) {
|
|
|
|
closestMatch = site.origin_base_url;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let site = matchingSites.find(site => site.origin_base_url === closestMatch);
|
|
|
|
if (site) {
|
|
|
|
// Get user's settings for the wiki
|
|
|
|
let id = site['id'];
|
2023-11-06 07:20:33 +00:00
|
|
|
let siteSetting = 'alert';
|
|
|
|
if (storage.wikiSettings && storage.wikiSettings[id]) {
|
|
|
|
siteSetting = storage.wikiSettings[id];
|
|
|
|
} else if (storage.defaultWikiAction) {
|
|
|
|
siteSetting = storage.defaultWikiAction;
|
|
|
|
}
|
2023-04-28 05:17:29 +00:00
|
|
|
// 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:
|
2023-11-05 05:52:22 +00:00
|
|
|
let article = String(origin).split(site['origin_content_path'])[1];
|
2023-04-28 05:17:29 +00:00
|
|
|
// Set up URL to redirect user to based on wiki platform:
|
2023-11-05 05:52:22 +00:00
|
|
|
let newURL = '';
|
|
|
|
if (article) {
|
|
|
|
let searchParams = '';
|
|
|
|
switch (site['destination_platform']) {
|
|
|
|
case 'mediawiki':
|
|
|
|
searchParams = 'Special:Search/' + site['destination_content_prefix'] + article;
|
|
|
|
break;
|
|
|
|
case 'doku':
|
|
|
|
searchParams = 'start?do=search&q=' + article;
|
|
|
|
break;
|
2023-02-06 00:48:53 +00:00
|
|
|
}
|
2023-11-05 05:52:22 +00:00
|
|
|
newURL = 'https://' + site["destination_base_url"] + site["destination_content_path"] + searchParams.replaceAll('+', '_');
|
|
|
|
// We replace plus signs with underscores since Fextralife uses pluses instead of spaces/underscores
|
|
|
|
} else {
|
|
|
|
newURL = 'https://' + site["destination_base_url"];
|
2023-02-06 00:48:53 +00:00
|
|
|
}
|
2023-11-05 05:52:22 +00:00
|
|
|
// When head elem is loaded, notify that another wiki is available
|
|
|
|
const docObserver = new MutationObserver(function (mutations, mutationInstance) {
|
|
|
|
const headElement = document.querySelector('head');
|
|
|
|
if (headElement) {
|
|
|
|
displayRedirectBanner(origin, newURL, site['id'], site['destination'], site['lang'], storage);
|
|
|
|
mutationInstance.disconnect();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
docObserver.observe(document, {
|
|
|
|
childList: true,
|
|
|
|
subtree: true
|
|
|
|
});
|
2023-02-06 00:48:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-28 05:17:29 +00:00
|
|
|
});
|
2023-02-06 00:48:53 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-28 05:17:29 +00:00
|
|
|
});
|
2023-02-06 00:48:53 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-25 06:17:46 +00:00
|
|
|
main();
|