Cleaning and updating the updateInstance func

This commit is contained in:
ManeraKai 2022-02-04 02:13:37 +03:00
parent 933cd7b908
commit eb6e58af52
No known key found for this signature in database
GPG Key ID: 5ABC31FFD562E337
12 changed files with 270 additions and 114 deletions

View File

@ -11,12 +11,6 @@ import mapsHelper from "./maps.js";
import medium from "./medium.js";
function filterInstances(instances) {
let onionScan = instances.filter((instance) => !instance.includes(".onion"))
let i2pScan = onionScan.filter((instance) => !instance.includes(".i2p"))
return i2pScan;
}
function addHttps(instances) {
return instances.map((item, i) => "https://" + item)
}
@ -33,15 +27,24 @@ function updateInstances() {
if (request.status === 200) {
const instances = JSON.parse(request.responseText);
youtubeHelper.redirects = addHttps(filterInstances(instances.invidious));
twitterHelper.redirects = addHttps(filterInstances(instances.nitter));
instagramHelper.redirects = addHttps(filterInstances(instances.bibliogram));
redditHelper.redirects.libreddit = addHttps(filterInstances(instances.simplyTranslate))
redditHelper.redirects.teddit = addHttps(filterInstances(instances.teddit));
searchHelper.redirects.searx = addHttps(filterInstances(instances.simplyTranslate));
searchHelper.redirects.whoogle = addHttps(filterInstances(instances.whoogle));
wikipediaHelper.redirects = addHttps(filterInstances(instances.wikiless));
mediumHelper.redirects = addHttps(filterInstances(instances.scribe));
youtubeHelper.setRedirects(instances.invidious);
twitterHelper.setRedirects(instances.nitter);
instagramHelper.setRedirects(instances.bibliogram);
redditHelper.setTedditRedirects(instances.teddit);
translateHelper.setSimplyTranslateRedirects(instances.simplyTranslate);
searchHelper.setSearxRedirects(instances.searx);
searchHelper.setWhoogleRedirects(instances.whoogle);
wikipediaHelper.setRedirects(instances.wikiless);
mediumHelper.setRedirects(instances.scribe);
console.info("Successfully updated Instances");
return true;
}
@ -107,7 +110,6 @@ function isException(url, initiator) {
}
export default {
filterInstances,
getRandomInstance,
updateInstances,
addHttps,

View File

@ -18,6 +18,13 @@ let redirects = {
"https://bib.actionsack.com"
],
};
const getRedirects = () => redirects;
function setRedirects(val) {
redirects = val;
browser.storage.sync.set({ instagramRedirects: val })
console.log("instagramRedirects: ", val)
}
const reservedPaths = [
"about",
"explore",
@ -59,8 +66,7 @@ function setBibliogramInstance(val) {
browser.storage.sync.set({ bibliogramInstance })
};
async function redirect(url, initiator, type) {
await init();
function redirect(url, initiator, type) {
if (disableInstagram)
return null;
@ -86,25 +92,30 @@ async function redirect(url, initiator, type) {
return `${link}/u${url.pathname}${url.search}`;
}
function isInstagram(url) {
return targets.includes(url.host)
}
async function init() {
let result = await browser.storage.sync.get([
"disableInstagram",
"bibliogramInstance",
"instagramRedirects"
])
disableInstagram = result.disableInstagram ?? false;
bibliogramInstance = result.bibliogramInstance;
if (result.instagramRedirects)
redirects = result.instagramRedirects
}
export default {
targets,
redirects,
reservedPaths,
bypassPaths,
getRedirects,
setRedirects,
getDisableInstagram,
setDisableInstagram,
getBibliogramInstance,
setBibliogramInstance,
isInstagram,
redirect,
init,
};

View File

@ -21,6 +21,7 @@ const layers = {
traffic: "S", // not implemented on OSM, default to standard.
bicycling: "C",
};
function addressToLatLng(address, callback) {
const xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = () => {
@ -58,15 +59,12 @@ const getOsmInstance = () => osmInstance;
function setOsmInstance(val) {
osmInstance = val;
browser.storage.sync.set({ osmInstance })
};
}
async function redirect(url, initiator) {
await init()
if (disableMaps)
return null;
function redirect(url, initiator) {
if (disableMaps) return null;
if (initiator && initiator.host === "earth.google.com")
return null;
if (initiator && initiator.host === "earth.google.com") return null;
let redirect;
let link = commonHelper.getRandomInstance(redirects.normal);
@ -80,9 +78,9 @@ async function redirect(url, initiator) {
const [lat, lon] = url.searchParams.get("center").split(",");
mapCentre = `#map=${url.searchParams.get("zoom") || "17"}/${lat}/${lon}`;
// Set default zoom if mapCentre not present
} else {
} else
params = "&zoom=17";
}
// Set map layer
params = `${params}&layers=${layers[url.searchParams.get("layer")] || layers["none"]
}`;
@ -140,6 +138,9 @@ async function redirect(url, initiator) {
return redirect;
}
function isMaps(url) {
return url.href.match(targets)
}
async function init() {
let result = await browser.storage.sync.get([
@ -151,18 +152,12 @@ async function init() {
}
export default {
targets,
redirects,
mapCentreRegex,
dataLatLngRegex,
placeRegex,
travelModes,
layers,
addressToLatLng,
getDisableMaps,
setDisableMaps,
getOsmInstance,
setOsmInstance,
redirect,
isMaps,
init,
};

View File

@ -22,6 +22,12 @@ let redirects = {
"https://scribe.nixnet.services"
]
};
const getRedirects = () => redirects;
function setRedirects(val) {
redirects = val;
browser.storage.sync.set({ mediumRedirects: val })
console.log("mediumRedirects: ", val)
}
let disableMedium;
const getDisableMedium = () => disableMedium;
@ -38,8 +44,7 @@ function setScribeInstance(val) {
browser.storage.sync.set({ scribeInstance })
};
async function redirect(url, initiator) {
await init()
function redirect(url, initiator) {
if (disableMedium) return null;
if (url.pathname == "/") return null;
@ -59,22 +64,35 @@ async function redirect(url, initiator) {
return `${commonHelper.getRandomInstance(redirects.normal)}${url.pathname}${url.search}`;
}
function isMedium(url) {
return targets.some((rx) => rx.test(url.host));
}
async function init() {
let result = await browser.storage.sync.get([
"disableMedium",
"scribeInstance",
"mediumRedirects"
])
disableMedium = result.disableMedium ?? false;
scribeInstance = result.scribeInstance;
if (result.mediumRedirects)
redirects = result.mediumRedirects;
}
export default {
targets,
redirects,
getRedirects,
setRedirects,
getDisableMedium,
setDisableMedium,
getScribeInstance,
setScribeInstance,
redirect,
isMedium,
init,
};

View File

@ -49,6 +49,20 @@ let redirects = {
"desktop": "https://old.reddit.com", // desktop
"mobile": "https://i.reddit.com", // mobile
};
const getRedirects = () => redirects;
function setLibredditRedirects(val) {
redirects.libreddit = val;
browser.storage.sync.set({ redditRedirects: redirects })
console.log("libredditRedirects:", val)
}
function setTedditRedirects(val) {
redirects.teddit = val;
browser.storage.sync.set({ redditRedirects: redirects })
console.log("tedditRedirects:", val)
}
const bypassPaths = /\/(gallery\/poll\/rpan\/settings\/topics)/;
let disableReddit;
@ -72,8 +86,7 @@ function setRedditFrontend(val) {
};
async function redirect(url, initiator, type) {
await init()
function redirect(url, initiator, type) {
if (disableReddit)
return null;
@ -112,27 +125,40 @@ async function redirect(url, initiator, type) {
if (redditFrontend == 'teddit') return `${tedditLink}${url.pathname}${url.search}`;
}
function isReddit(url) {
return targets.includes(url.host)
}
async function init() {
let result = await browser.storage.sync.get([
"disableReddit",
"redditInstance",
"redditFrontend",
"redditRedirects"
])
disableReddit = result.disableReddit ?? false;
redditInstance = result.redditInstance;
redditFrontend = result.redditFrontend ?? 'libreddit';
if (result.redditRedirects)
redirects = result.redditRedirects;
}
export default {
targets,
redirects,
bypassPaths,
getRedirects,
setTedditRedirects,
setLibredditRedirects,
getDisableReddit,
setDisableReddit,
getRedditInstance,
setRedditInstance,
getRedditFrontend,
setRedditFrontend,
redirect,
isReddit,
init,
};

View File

@ -151,6 +151,21 @@ let redirects = {
]
}
};
const getRedirects = () => redirects;
function setSearxRedirects(val) {
redirects.searx = val;
browser.storage.sync.set({ searchRedirects: redirects })
console.log("searxRedirects:", val)
}
function setWhoogleRedirects(val) {
redirects.whoogle = val;
browser.storage.sync.set({ searchRedirects: redirects })
console.log("whoogleRedirects:", val)
}
let disableSearch;
const getDisableSearch = () => disableSearch;
@ -175,8 +190,7 @@ function setSearchFrontend(val) {
console.log("searchFrontend: ", searchFrontend)
};
async function redirect(url, initiator) {
await init();
function redirect(url, initiator) {
if (disableSearch)
return null;
@ -199,26 +213,41 @@ async function redirect(url, initiator) {
return `${instance}${path}?${searchQuery}`;
}
function isSearch(url) {
return targets.some((rx) => rx.test(url.href));
}
async function init() {
let result = await browser.storage.sync.get([
"disableSearch",
"searchInstance",
"searchFrontend",
"searchRedirects",
])
disableSearch = result.disableSearch ?? false;
searchInstance = result.searchInstance;
searchFrontend = result.searchFrontend ?? 'searx';
if (result.searchRedirects)
redirects = result.searchRedirects;
}
export default {
targets,
redirects,
isSearch,
getRedirects,
setSearxRedirects,
setWhoogleRedirects,
getDisableSearch,
setDisableSearch,
getSearchInstance,
setSearchInstance,
getSearchFrontend,
setSearchFrontend,
redirect,
init,
};

View File

@ -30,6 +30,19 @@ let redirects = {
}
};
const getRedirects = () => redirects;
function setSimplyTranslateRedirects(val) {
redirects.simplyTranslate = val;
browser.storage.sync.set({ translateRedirects: redirects })
console.log("simplyTranslateRedirects:", val)
}
function setLingvaRedirects(val) {
redirects.lingva = val;
browser.storage.sync.set({ translateRedirects: redirects })
console.log("lingvaRedirects:", val)
}
let disableTranslate;
const getDisableTranslate = () => disableTranslate;
@ -54,8 +67,7 @@ function setFrontend(val) {
console.log("Translate frontend: ", frontend)
}
async function redirect(url, initiator) {
await init()
function redirect(url, initiator) {
if (disableTranslate) {
console.log("SImplyTranslte disabled")
return null
@ -81,29 +93,43 @@ async function redirect(url, initiator) {
else
return link;
}
}
function isTranslate(url) {
return targets.includes(url.host)
}
async function init() {
let result = await browser.storage.sync.get([
"disableTranslate",
"simplyTranslateInstance",
"translateFrontend"
"translateFrontend",
"translateRedirects"
]);
disableTranslate = result.disableTranslate ?? false;
simplyTranslateInstance = result.simplyTranslateInstance;
translateFrontend = result.translateFrontend ?? "simplyTransalte";
if (result.translateRedirects)
redirects = result.translateRedirects
}
export default {
targets,
redirects,
getRedirects,
setSimplyTranslateRedirects,
setLingvaRedirects,
isTranslate,
getDisableTranslate,
setDisableTranslate,
getSimplyTranslateInstance,
setSimplyTranslateInstance,
getFrontend,
setFrontend,
redirect,
init,
};

View File

@ -43,6 +43,12 @@ let redirects = {
"http://npf37k3mtzwxreiw52ccs5ay4e6qt2fkcs2ndieurdyn2cuzzsfyfvid.onion",
]
};
const getRedirects = () => redirects;
function setRedirects(val) {
redirects = val;
browser.storage.sync.set({ twitterRedirects: val })
console.log("twitterRedirects:", val)
}
let disableTwitter;
const getDisableTwitter = () => disableTwitter;
@ -58,9 +64,7 @@ function setNitterInstance(val) {
browser.storage.sync.set({ nitterInstance })
}
async function redirect(url, initiator) {
await init();
function redirect(url, initiator) {
if (disableTwitter)
return null;
@ -91,22 +95,33 @@ async function redirect(url, initiator) {
}
function isTwitter(url) {
return targets.includes(url.host)
}
async function init() {
let result = await browser.storage.sync.get([
"disableTwitter",
"nitterInstance"
"nitterInstance",
"twitterRedirects"
]);
disableTwitter = result.disableTwitter ?? false;
nitterInstance = result.nitterInstance;
if (result.twitterRedirects)
redirects = result.twitterRedirects;
}
export default {
targets,
redirects,
getRedirects,
setRedirects,
getDisableTwitter,
setDisableTwitter,
getNitterInstance,
setNitterInstance,
redirect,
isTwitter,
init,
};

View File

@ -1,3 +1,5 @@
import commonHelper from './common.js'
const targets = /wikipedia.org/;
let redirects = {
@ -5,6 +7,12 @@ let redirects = {
"https://wikiless.org"
]
};
const getRedirects = () => redirects;
function setRedirects(val) {
redirects = val;
browser.storage.sync.set({ wikipediaRedirects: val })
console.log("wikipediaRedirects: ", val)
}
let disableWikipedia;
const getDisableWikipedia = () => disableWikipedia;
@ -20,8 +28,7 @@ function setWikipediaInstance(val) {
browser.storage.sync.set({ wikipediaInstance })
};
async function redirect(url, initiator) {
await init()
function redirect(url, initiator) {
if (disableWikipedia) return null;
let GETArguments = [];
@ -33,7 +40,8 @@ async function redirect(url, initiator) {
GETArguments.push([args[0], args[1]]);
}
}
let link = `${wikipediaInstance}${url.pathname}`;
let instance = wikipediaInstance ?? commonHelper.getRandomInstance(redirects.normal)
let link = `${instance}${url.pathname}`;
let urlSplit = url.host.split(".");
if (urlSplit[0] != "wikipedia" && urlSplit[0] != "www") {
if (urlSplit[0] == "m")
@ -55,22 +63,33 @@ async function redirect(url, initiator) {
else return null;
}
function isWikipedia(url) {
return url.host.match(targets);
}
async function init() {
let result = await browser.storage.sync.get([
"disableWikipedia",
"wikipediaInstance",
"wikipediaRedirects"
]);
disableWikipedia = result.disableWikipedia ?? false;
wikipediaInstance = result.wikipediaInstance;
if (result.wikipediaRedirects)
redirects = result.wikipediaRedirects;
}
export default {
targets,
redirects,
getRedirects,
setRedirects,
setDisableWikipedia,
getDisableWikipedia,
setWikipediaInstance,
getWikipediaInstance,
redirect,
isWikipedia,
init,
};

View File

@ -49,6 +49,12 @@ let redirects = {
"http://w6ijuptxiku4xpnnaetxvnkc5vqcdu7mgns2u77qefoixi63vbvnpnqd.onion",
]
};
const getRedirects = () => redirects;
function setRedirects(val) {
redirects = val;
browser.storage.sync.set({ youtubeRedirects: val })
console.log("youtubeRedirects: ", val)
}
let disableYoutube;
const getDisableYoutube = () => disableYoutube;
@ -106,7 +112,6 @@ function setInvidiousVolume(val) {
console.log("invidiousVolume: ", invidiousVolume)
}
let invidiousPlayerStyle;
const getInvidiousPlayerStyle = () => invidiousPlayerStyle;
function setInvidiousPlayerStyle(val) {
@ -139,7 +144,6 @@ function setUseFreeTube(val) {
}
const getUseFreeTube = () => useFreeTube;
let persistInvidiousPrefs;
function setPersistInvidiousPrefs(val) {
persistInvidiousPrefs = val;
@ -149,8 +153,7 @@ function setPersistInvidiousPrefs(val) {
}
const getPersistInvidiousPrefs = () => persistInvidiousPrefs;
async function redirect(url, initiator, type) {
await init();
function redirect(url, initiator, type) {
if (disableYoutube)
return null;
@ -193,6 +196,9 @@ async function redirect(url, initiator, type) {
return `${randomInstance}${url.pathname.replace("/shorts", "")}${url.search}`;
}
function isYoutube(url) {
return targets.includes(url.host);
}
function getCookie() {
let ca = document.cookie.split(";");
@ -233,6 +239,7 @@ async function init() {
"invidiousSubtitles",
"invidiousAutoplay",
"useFreeTube",
"youtubeRedirects"
]);
disableYoutube = result.disableYoutube ?? false;
invidiousInstance = result.invidiousInstance;
@ -246,13 +253,17 @@ async function init() {
invidiousAutoplay = result.invidiousAutoplay ?? true;
useFreeTube = result.useFreeTube ?? false;
if (result.youtubeRedirects)
redirects = result.youtubeRedirects
if (result.persistInvidiousPrefs) initInvidiousCookie();
}
export default {
targets,
redirects,
getRedirects,
setRedirects,
redirect,
isYoutube,
getDisableYoutube,
setDisableYoutube,

View File

@ -12,6 +12,7 @@ import mediumHelper from "../../assets/javascripts/helpers/medium.js";
window.browser = window.browser || window.chrome;
function wholeInit() {
mapsHelper.init()
searchHelper.init()
translateHelper.init()
@ -21,7 +22,11 @@ redditHelper.init()
twitterHelper.init()
wikipediaHelper.init()
youtubeHelper.init()
}
wholeInit();
browser.storage.onChanged.addListener(wholeInit);
browser.webRequest.onBeforeRequest.addListener(
async (details) => {
@ -34,23 +39,23 @@ browser.webRequest.onBeforeRequest.addListener(
var newUrl;
if (youtubeHelper.targets.includes(url.host)) newUrl = await youtubeHelper.redirect(url, initiator, details.type)
if (youtubeHelper.isYoutube(url)) newUrl = youtubeHelper.redirect(url, initiator, details.type)
else if (twitterHelper.targets.includes(url.host)) newUrl = await twitterHelper.redirect(url, initiator);
else if (twitterHelper.isTwitter(url)) newUrl = twitterHelper.redirect(url, initiator);
else if (instagramHelper.targets.includes(url.host)) newUrl = await instagramHelper.redirect(url, initiator, details.type);
else if (instagramHelper.isInstagram(url)) newUrl = instagramHelper.redirect(url, initiator, details.type);
else if (url.href.match(mapsHelper.targets)) newUrl = await mapsHelper.redirect(url, initiator);
else if (mapsHelper.isMaps(url)) newUrl = mapsHelper.redirect(url, initiator);
else if (redditHelper.targets.includes(url.host)) newUrl = await redditHelper.redirect(url, initiator, details.type);
else if (redditHelper.isReddit(url)) newUrl = redditHelper.redirect(url, initiator, details.type);
else if (mediumHelper.targets.some((rx) => rx.test(url.host))) newUrl = await mediumHelper.redirect(url, initiator);
else if (mediumHelper.isMedium(url)) newUrl = mediumHelper.redirect(url, initiator);
else if (translateHelper.targets.includes(url.host)) newUrl = await translateHelper.redirect(url, initiator);
else if (translateHelper.isTranslate(url)) newUrl = translateHelper.redirect(url, initiator);
else if (searchHelper.targets.some((rx) => rx.test(url.href))) newUrl = await searchHelper.redirect(url, initiator)
else if (searchHelper.isSearch(url)) newUrl = searchHelper.redirect(url, initiator)
else if (url.host.match(wikipediaHelper.targets)) newUrl = await wikipediaHelper.redirect(url, initiator);
else if (wikipediaHelper.isWikipedia(url)) newUrl = wikipediaHelper.redirect(url, initiator);
if (newUrl) {
console.info("Redirecting", url.href, "=>", newUrl);
@ -62,7 +67,6 @@ browser.webRequest.onBeforeRequest.addListener(
["blocking"]
);
browser.tabs.onUpdated.addListener((tabId, changeInfo, _) => {
let url;
try {
@ -73,19 +77,19 @@ browser.tabs.onUpdated.addListener((tabId, changeInfo, _) => {
var protocolHost = `${url.protocol}//${url.host}`;
var mightyList = [];
mightyList.push(
...youtubeHelper.redirects.normal,
...twitterHelper.redirects.normal,
...instagramHelper.redirects.normal,
...redditHelper.redirects.libreddit.normal,
...redditHelper.redirects.teddit.normal,
redditHelper.redirects.desktop,
redditHelper.redirects.mobile,
...searchHelper.redirects.searx.normal,
...searchHelper.redirects.whoogle.normal,
...translateHelper.redirects.simplyTranslate.normal,
...translateHelper.redirects.lingva.normal,
...mediumHelper.redirects.normal,
...wikipediaHelper.redirects.normal
...youtubeHelper.getRedirects().normal,
...twitterHelper.getRedirects().normal,
...instagramHelper.getRedirects().normal,
...redditHelper.getRedirects().libreddit.normal,
...redditHelper.getRedirects().teddit.normal,
redditHelper.getRedirects().desktop,
redditHelper.getRedirects().mobile,
...searchHelper.getRedirects().searx.normal,
...searchHelper.getRedirects().whoogle.normal,
...translateHelper.getRedirects().simplyTranslate.normal,
...translateHelper.getRedirects().lingva.normal,
...mediumHelper.getRedirects().normal,
...wikipediaHelper.getRedirects().normal
);
if (mightyList.includes(protocolHost)) browser.pageAction.show(tabId);
@ -96,14 +100,14 @@ browser.pageAction.onClicked.addListener((tab) => {
var tabUrl = new URL(tab.url);
var protocolHost = `${tabUrl.protocol}//${tabUrl.host}`;
var newUrl;
if (youtubeHelper.redirects.normal.includes(protocolHost)) newUrl = 'https://youtube.com';
if (twitterHelper.redirects.normal.includes(protocolHost)) newUrl = 'https://twitter.com';
if (youtubeHelper.getRedirects().normal.includes(protocolHost)) newUrl = 'https://youtube.com';
if (instagramHelper.redirects.normal.includes(protocolHost)) newUrl = 'https://instagram.com';
if (twitterHelper.getRedirects().normal.includes(protocolHost)) newUrl = 'https://twitter.com';
if (instagramHelper.getRedirects().normal.includes(protocolHost)) newUrl = 'https://instagram.com';
if (redditHelper.redirects.libreddit.normal.includes(protocolHost) || redditHelper.redirects.teddit.normal.includes(protocolHost)) {
if (redditHelper.getRedirects().libreddit.normal.includes(protocolHost) || redditHelper.getRedirects().teddit.normal.includes(protocolHost)) {
if (tabUrl.pathname.startsWith('/img')) {
newUrl = "https://i.redd.it"
tabUrl.href = tabUrl.href.replace("/img", "")
@ -113,18 +117,18 @@ browser.pageAction.onClicked.addListener((tab) => {
}
if (
searchHelper.redirects.searx.normal.includes(protocolHost) ||
searchHelper.redirects.whoogle.normal.includes(protocolHost)
searchHelper.getRedirects().searx.normal.includes(protocolHost) ||
searchHelper.getRedirects().whoogle.normal.includes(protocolHost)
) newUrl = 'https://google.com';
if (
translateHelper.redirects.simplyTranslate.normal.includes(protocolHost) ||
translateHelper.redirects.lingva.normal.includes(protocolHost)
translateHelper.getRedirects().simplyTranslate.normal.includes(protocolHost) ||
translateHelper.getRedirects().lingva.normal.includes(protocolHost)
) newUrl = 'https://translate.google.com';
if (mediumHelper.redirects.normal.includes(protocolHost)) newUrl = 'https://medium.com';
if (mediumHelper.getRedirects().normal.includes(protocolHost)) newUrl = 'https://medium.com';
if (wikipediaHelper.redirects.normal.includes(protocolHost)) newUrl = 'https://wikipedia.com';
if (wikipediaHelper.getRedirects().normal.includes(protocolHost)) newUrl = 'https://wikipedia.com';
if (newUrl) browser.tabs.update({ url: tabUrl.href.replace(protocolHost, newUrl) });
});

View File

@ -54,9 +54,9 @@ function addToExceptions() {
try {
let value = input.value;
new RegExp(input.value);
if (type === "URL") {
if (type === "URL")
value = value.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
}
exceptions.push(value);
browser.storage.sync.set({
exceptions: exceptions,