Adding non-EN wikis from wiki.gg
Includes wikis in DE, ES, FR, HU, IT, JA, KO, PL, PT, RU, TH, UK, and ZH. HU, JA, and TH are new language categories.
2024-02-12 07:23:47 +00:00
|
|
|
|
var LANGS = ["DE", "EN", "ES", "FI", "FR", "HU", "IT", "JA", "KO", "PL", "PT", "RU", "TH", "TOK", "UK", "ZH"];
|
2024-02-08 08:34:53 +00:00
|
|
|
|
const BASE64REGEX = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
|
|
|
|
|
|
|
|
|
|
function b64decode(str) {
|
|
|
|
|
const binary_string = atob(str);
|
|
|
|
|
const len = binary_string.length;
|
|
|
|
|
const bytes = new Uint8Array(new ArrayBuffer(len));
|
|
|
|
|
for (let i = 0; i < len; i++) {
|
|
|
|
|
bytes[i] = binary_string.charCodeAt(i);
|
|
|
|
|
}
|
|
|
|
|
return bytes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function commonFunctionDecompressJSON(value) {
|
|
|
|
|
// Check if value is base64 encoded:
|
|
|
|
|
if (BASE64REGEX.test(value)) {
|
|
|
|
|
// Decode into blob
|
|
|
|
|
const stream = new Blob([b64decode(value)], {
|
|
|
|
|
type: "application/json",
|
|
|
|
|
}).stream();
|
|
|
|
|
|
|
|
|
|
// Decompress value
|
|
|
|
|
const decompressedReadableStream = stream.pipeThrough(
|
|
|
|
|
new DecompressionStream("gzip")
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const resp = new Response(decompressedReadableStream);
|
|
|
|
|
const blob = await resp.blob();
|
|
|
|
|
const blobText = JSON.parse(await blob.text());
|
|
|
|
|
return blobText;
|
|
|
|
|
} else {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function commonFunctionCompressJSON(value) {
|
|
|
|
|
const stream = new Blob([JSON.stringify(value)], {
|
|
|
|
|
type: 'application/json',
|
|
|
|
|
}).stream();
|
|
|
|
|
|
|
|
|
|
// Compress stream with gzip
|
|
|
|
|
const compressedReadableStream = stream.pipeThrough(
|
|
|
|
|
new CompressionStream("gzip")
|
|
|
|
|
);
|
|
|
|
|
const compressedResponse = new Response(compressedReadableStream);
|
|
|
|
|
|
|
|
|
|
// Convert response to blob and buffer
|
|
|
|
|
const blob = await compressedResponse.blob();
|
|
|
|
|
const buffer = await blob.arrayBuffer();
|
|
|
|
|
|
|
|
|
|
// Encode and return string
|
|
|
|
|
return btoa(
|
|
|
|
|
String.fromCharCode(
|
|
|
|
|
...new Uint8Array(buffer)
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-01-11 09:01:15 +00:00
|
|
|
|
|
2024-01-10 09:21:47 +00:00
|
|
|
|
// Load wiki data objects, with each destination having its own object
|
|
|
|
|
async function commonFunctionGetSiteDataByDestination() {
|
|
|
|
|
var 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((jsonData) => {
|
|
|
|
|
jsonData.forEach((site) => site.language = LANGS[i]);
|
|
|
|
|
sites = sites.concat(jsonData);
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
await Promise.all(promises);
|
|
|
|
|
return sites;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Load wiki data objects, with each origin having its own object
|
|
|
|
|
async function commonFunctionGetSiteDataByOrigin() {
|
|
|
|
|
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((jsonData) => {
|
|
|
|
|
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,
|
|
|
|
|
"origin_main_page": origin.origin_main_page,
|
|
|
|
|
"destination": site.destination,
|
|
|
|
|
"destination_base_url": site.destination_base_url,
|
|
|
|
|
"destination_search_path": site.destination_search_path,
|
|
|
|
|
"destination_content_prefix": origin.destination_content_prefix || site.destination_content_prefix || "",
|
|
|
|
|
"destination_platform": site.destination_platform,
|
|
|
|
|
"destination_icon": site.destination_icon,
|
|
|
|
|
"destination_main_page": site.destination_main_page,
|
|
|
|
|
"tags": site.tags || [],
|
|
|
|
|
"language": LANGS[i]
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
await Promise.all(promises);
|
|
|
|
|
return sites;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Given a URL, find closest match in our dataset
|
|
|
|
|
async function commonFunctionFindMatchingSite(site, crossLanguageSetting) {
|
|
|
|
|
let matchingSite = commonFunctionGetSiteDataByOrigin().then(sites => {
|
|
|
|
|
let matchingSites = [];
|
|
|
|
|
if (crossLanguageSetting === 'on') {
|
2024-02-04 10:19:09 +00:00
|
|
|
|
matchingSites = sites.filter(el => site.replace(/.*https?:\/\//, '').startsWith(el.origin_base_url));
|
2024-01-10 09:21:47 +00:00
|
|
|
|
} else {
|
2024-02-18 06:41:06 +00:00
|
|
|
|
matchingSites = sites.filter(el => {
|
|
|
|
|
return site.replace(/.*https?:\/\//, '').startsWith(el.origin_base_url + el.origin_content_path)
|
|
|
|
|
|| site.replace(/.*https?:\/\//, '').replace(/\/$/, '') === el.origin_base_url
|
|
|
|
|
}
|
2024-01-24 10:18:19 +00:00
|
|
|
|
);
|
2024-01-10 09:21:47 +00:00
|
|
|
|
}
|
|
|
|
|
if (matchingSites.length > 0) {
|
|
|
|
|
// Select match with longest base URL
|
|
|
|
|
let closestMatch = '';
|
|
|
|
|
matchingSites.forEach(site => {
|
|
|
|
|
if (site.origin_base_url.length > closestMatch.length) {
|
|
|
|
|
closestMatch = site.origin_base_url;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return matchingSites.find(site => site.origin_base_url === closestMatch);
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return matchingSite;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-11 08:53:30 +00:00
|
|
|
|
function commonFunctionGetOriginArticle(originURL, matchingSite) {
|
2024-02-04 10:19:09 +00:00
|
|
|
|
let url = new URL(originURL);
|
|
|
|
|
return decodeURIComponent(String(url.pathname).split(matchingSite['origin_content_path'])[1] || '');
|
2024-01-11 05:40:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-11 08:53:30 +00:00
|
|
|
|
function commonFunctionGetDestinationArticle(matchingSite, article) {
|
2024-01-11 05:40:59 +00:00
|
|
|
|
return matchingSite['destination_content_prefix'] + article;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-11 08:53:30 +00:00
|
|
|
|
function commonFunctionGetNewURL(originURL, matchingSite) {
|
2024-01-11 05:40:59 +00:00
|
|
|
|
// 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
|
2024-01-11 08:53:30 +00:00
|
|
|
|
let originArticle = commonFunctionGetOriginArticle(originURL, matchingSite);
|
|
|
|
|
let destinationArticle = commonFunctionGetDestinationArticle(matchingSite, originArticle);
|
2024-01-11 05:40:59 +00:00
|
|
|
|
// 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']) {
|
2024-02-08 06:53:22 +00:00
|
|
|
|
case 'dokuwiki':
|
2024-01-11 05:40:59 +00:00
|
|
|
|
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':
|
2024-02-04 02:20:23 +00:00
|
|
|
|
searchParams = '?title=Special:Search&search=' + destinationArticle;
|
2024-01-11 05:40:59 +00:00
|
|
|
|
break;
|
2024-02-08 06:53:22 +00:00
|
|
|
|
case 'dokuwiki':
|
|
|
|
|
searchParams = '?do=search&q=' + destinationArticle;
|
2024-01-11 05:40:59 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
newURL = 'https://' + matchingSite["destination_base_url"] + matchingSite["destination_search_path"] + searchParams;
|
|
|
|
|
} else {
|
|
|
|
|
newURL = 'https://' + matchingSite["destination_base_url"];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return newURL;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-10 09:21:47 +00:00
|
|
|
|
// Temporary function to migrate user data to IWB version 3.0+
|
|
|
|
|
async function commonFunctionMigrateToV3() {
|
|
|
|
|
await chrome.storage.sync.get(async (storage) => {
|
|
|
|
|
if (!storage.v3migration) {
|
|
|
|
|
let defaultWikiAction = storage.defaultWikiAction || 'alert';
|
|
|
|
|
let defaultSearchAction = storage.defaultSearchAction || 'replace';
|
|
|
|
|
|
|
|
|
|
// Set new default action settings:
|
|
|
|
|
if (!storage.defaultWikiAction) {
|
|
|
|
|
if (storage.defaultActionSettings && storage.defaultActionSettings['EN']) {
|
|
|
|
|
defaultWikiAction = storage.defaultActionSettings['EN'];
|
|
|
|
|
}
|
|
|
|
|
chrome.storage.sync.set({ 'defaultWikiAction': defaultWikiAction });
|
|
|
|
|
}
|
|
|
|
|
if (!storage.defaultSearchAction) {
|
|
|
|
|
if (storage.defaultSearchFilterSettings && storage.defaultSearchFilterSettings['EN']) {
|
|
|
|
|
if (storage.defaultSearchFilterSettings['EN'] === 'false') {
|
|
|
|
|
defaultSearchAction = 'disabled';
|
|
|
|
|
} else {
|
|
|
|
|
defaultSearchAction = 'replace';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
chrome.storage.sync.set({ 'defaultSearchAction': defaultSearchAction });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove old objects:
|
|
|
|
|
chrome.storage.sync.remove('defaultActionSettings');
|
|
|
|
|
chrome.storage.sync.remove('defaultSearchFilterSettings');
|
|
|
|
|
|
|
|
|
|
// Migrate wiki settings to new searchEngineSettings and wikiSettings objects
|
|
|
|
|
sites = await commonFunctionGetSiteDataByOrigin();
|
|
|
|
|
let siteSettings = storage.siteSettings || {};
|
2024-02-08 08:34:53 +00:00
|
|
|
|
let searchEngineSettings = await commonFunctionDecompressJSON(storage.searchEngineSettings || {});
|
|
|
|
|
let wikiSettings = await commonFunctionDecompressJSON(storage.wikiSettings) || {};
|
2024-01-10 09:21:47 +00:00
|
|
|
|
|
|
|
|
|
sites.forEach((site) => {
|
|
|
|
|
if (!searchEngineSettings[site.id]) {
|
|
|
|
|
if (siteSettings[site.id] && siteSettings[site.id].searchFilter) {
|
|
|
|
|
if (siteSettings[site.id].searchFilter === 'false') {
|
|
|
|
|
searchEngineSettings[site.id] = 'disabled';
|
|
|
|
|
} else {
|
|
|
|
|
searchEngineSettings[site.id] = 'replace';
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
searchEngineSettings[site.id] = defaultSearchAction;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!wikiSettings[site.id]) {
|
|
|
|
|
wikiSettings[site.id] = siteSettings[site.id]?.action || defaultWikiAction;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2024-02-08 08:34:53 +00:00
|
|
|
|
chrome.storage.sync.set({ 'searchEngineSettings': await commonFunctionCompressJSON(searchEngineSettings) });
|
|
|
|
|
chrome.storage.sync.set({ 'wikiSettings': await commonFunctionCompressJSON(wikiSettings) });
|
2024-01-10 09:21:47 +00:00
|
|
|
|
|
|
|
|
|
// Remove old object:
|
|
|
|
|
chrome.storage.sync.remove('siteSettings');
|
|
|
|
|
|
|
|
|
|
// Mark v3 migration as complete:
|
|
|
|
|
chrome.storage.sync.set({ 'v3migration': 'done' });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|