Added redirecting feature #6, will improve it

This commit is contained in:
ManeraKai 2022-02-12 12:30:30 +03:00
parent 7ee2539969
commit 9b73898584
12 changed files with 178 additions and 45 deletions

View File

@ -14,6 +14,28 @@ function getRandomInstance(instances) {
return instances[~~(instances.length * Math.random())];
}
async function getRandomOnlineInstance(instances) {
const shuffledInstances = instances.sort((a, b) => 0.5 - Math.random())
for (let ins of shuffledInstances) {
try {
const res = await fetch(ins, { redirect: 'follow' })
console.log(res)
if (res.status >= 200 && res.status < 300)
return ins // instance seems healthy!
else
console.warn(`Instance ${ins} seems offline (status code: ${res.status}). we try another one`)
} catch (err) {
console.warn(`Instance ${ins} seems offline. we try another one`)
}
}
// everything offline? -> unlikely
// rather respond with any entry instead of breaking the functionality
return shuffledInstances[0]
}
async function wholeInit() {
await youtubeHelper.init();
await twitterHelper.init();
@ -150,10 +172,11 @@ function processDefaultCustomInstances(
document.getElementById(`custom-${name}-instance-form`).addEventListener("submit", (event) => {
event.preventDefault();
let nameCustomInstanceInput = document.getElementById(`${name}-custom-instance`);
let val = nameCustomInstanceInput.value
if (nameCustomInstanceInput.validity.valid && !nameHelper.getRedirects()[name].normal.includes(val) && val.trim() != '') {
if (!nameCustomInstances.includes(val)) {
nameCustomInstances.push(val)
let url = new URL(nameCustomInstanceInput.value);
let protocolHost = `${url.protocol}//${url.host}`;
if (nameCustomInstanceInput.validity.valid && !nameHelper.getRedirects()[name].normal.includes(protocolHost)) {
if (!nameCustomInstances.includes(protocolHost)) {
nameCustomInstances.push(protocolHost)
setNameCustomRedirects(nameCustomInstances);
nameCustomInstanceInput.value = '';
}
@ -164,6 +187,7 @@ function processDefaultCustomInstances(
export default {
getRandomInstance,
getRandomOnlineInstance,
updateInstances,
isFirefox,
processDefaultCustomInstances,

View File

@ -22,6 +22,15 @@ let redirects = {
}
const getRedirects = () => redirects;
const getCustomRedirects = function () {
return {
"rimgo": {
"normal": [...rimgoRedirectsChecks, ...rimgoCustomRedirects]
},
};
};
function setRedirects(val) {
redirects.rimgo = val;
browser.storage.sync.set({ imgurRedirects: redirects })
@ -102,6 +111,7 @@ export default {
targets,
getRedirects,
getCustomRedirects,
setRedirects,
getDisableImgur,

View File

@ -22,6 +22,13 @@ let redirects = {
}
};
const getRedirects = () => redirects;
const getCustomRedirects = function () {
return {
"bibliogram": {
"normal": [...bibliogramRedirectsChecks, ...bibliogramCustomRedirects]
},
};
};
function setRedirects(val) {
redirects.bibliogram = val;
browser.storage.sync.set({ instagramRedirects: redirects })
@ -128,6 +135,7 @@ async function init() {
export default {
getRedirects,
getCustomRedirects,
setRedirects,
getDisableInstagram,

View File

@ -28,6 +28,13 @@ let redirects = {
}
};
const getRedirects = () => redirects;
const getCustomRedirects = function () {
return {
"scribe": {
"normal": [...scribeRedirectsChecks, ...scribeCustomRedirects]
},
};
};
function setRedirects(val) {
redirects.scribe = val;
browser.storage.sync.set({ mediumRedirects: redirects })
@ -111,6 +118,7 @@ export default {
targets,
getRedirects,
getCustomRedirects,
setRedirects,
getDisableMedium,

View File

@ -58,6 +58,16 @@ let redirects = {
"mobile": "https://i.reddit.com", // mobile
};
const getRedirects = () => redirects;
const getCustomRedirects = function () {
return {
"libreddit": {
"normal": [...libredditRedirectsChecks, ...libredditCustomRedirects]
},
"teddit": {
"normal": [...tedditRedirectsChecks, ...tedditCustomRedirects]
}
};
};
function setLibredditRedirects(val) {
redirects.libreddit = val;
@ -206,6 +216,7 @@ async function init() {
export default {
targets,
getRedirects,
getCustomRedirects,
setTedditRedirects,
setLibredditRedirects,

View File

@ -134,6 +134,16 @@ let redirects = {
},
};
const getRedirects = () => redirects;
const getCustomRedirects = function () {
return {
"searx": {
"normal": [...searxRedirectsChecks, ...searxCustomRedirects]
},
"whoogle": {
"normal": [...whoogleRedirectsChecks, ...whoogleCustomRedirects]
}
};
};
function setSearxRedirects(val) {
redirects.searx = val;
@ -269,6 +279,7 @@ export default {
isSearch,
getRedirects,
getCustomRedirects,
setSearxRedirects,
setWhoogleRedirects,

View File

@ -13,6 +13,14 @@ let redirects = {
}
const getRedirects = () => redirects;
const getCustomRedirects = function () {
return {
"proxiTok": {
"normal": [...proxiTokRedirectsChecks, ...proxiTokCustomRedirects]
},
};
};
function setRedirects(val) {
redirects.proxiTok = val;
browser.storage.sync.set({ tiktokRedirects: redirects })
@ -90,6 +98,7 @@ export default {
targets,
getRedirects,
getCustomRedirects,
setRedirects,
getDisableTiktok,

View File

@ -37,6 +37,16 @@ let redirects = {
};
const getRedirects = () => redirects;
const getCustomRedirects = function () {
return {
"simplyTranslate": {
"normal": [...simplyTranslateRedirectsChecks, ...simplyTranslateCustomRedirects]
},
"lingva": {
"normal": [...lingvaRedirectsChecks, ...lingvaCustomRedirects]
}
};
};
function setSimplyTranslateRedirects(val) {
redirects.simplyTranslate = val;
@ -174,6 +184,7 @@ async function init() {
export default {
getRedirects,
getCustomRedirects,
setSimplyTranslateRedirects,
setLingvaRedirects,

View File

@ -78,6 +78,13 @@ let redirects = {
}
};
const getRedirects = () => redirects;
const getCustomRedirects = function () {
return {
"nitter": {
"normal": [...nitterRedirectsChecks, ...nitterCustomRedirects]
},
};
};
function setRedirects(val) {
redirects.nitter = val;
browser.storage.sync.set({ twitterRedirects: redirects })
@ -166,6 +173,7 @@ async function init() {
export default {
getRedirects,
getCustomRedirects,
setRedirects,
getDisableTwitter,

View File

@ -16,6 +16,13 @@ let redirects = {
}
};
const getRedirects = () => redirects;
const getCustomRedirects = function () {
return {
"wikiless": {
"normal": [...wikilessRedirectsChecks, ...wikilessCustomRedirects]
},
};
};
function setRedirects(val) {
redirects.wikiless = val;
browser.storage.sync.set({ wikipediaRedirects: redirects })
@ -115,6 +122,7 @@ async function init() {
export default {
getRedirects,
getCustomRedirects,
setRedirects,
setDisableWikipedia,

View File

@ -54,6 +54,17 @@ let redirects = {
const getRedirects = () => redirects;
const getCustomRedirects = function () {
return {
"invidious": {
"normal": [...invidiousRedirectsChecks, ...invidiousCustomRedirects]
},
"piped": {
"normal": [...pipedRedirectsChecks, ...pipedCustomRedirects]
}
};
};
function setInvidiousRedirects(val) {
redirects.invidious = val;
browser.storage.sync.set({ youtubeRedirects: redirects })
@ -320,6 +331,7 @@ export default {
setFrontend,
getRedirects,
getCustomRedirects,
setInvidiousRedirects,
setPipedRedirects,

View File

@ -81,6 +81,22 @@ browser.webRequest.onBeforeRequest.addListener(
["blocking"]
);
browser.webRequest.onResponseStarted.addListener(
(responseDetails) => {
let url = new URL(responseDetails.url);
let protocolHost = `${url.protocol}//${url.host}`;
var mightyList = getMightyList();
if (mightyList.includes(protocolHost)); {
if (responseDetails.statusCode < 200 || responseDetails.statusCode >= 300) {
console.log("Instance is corrupted, redirecting", responseDetails.url);
changeInstance(responseDetails.url);
}
}
},
{ urls: ["<all_urls>"] }
);
browser.tabs.onUpdated.addListener((tabId, changeInfo, _) => {
let url;
try {
@ -90,50 +106,25 @@ browser.tabs.onUpdated.addListener((tabId, changeInfo, _) => {
}
var protocolHost = `${url.protocol}//${url.host}`;
if (youtubeHelper.getRedirects().invidious.normal.includes(protocolHost)) {
if (youtubeHelper.getPersistInvidiousPrefs())
youtubeHelper.invidiousInitCookies(tabId);
}
var mightyList = [];
mightyList.push(
...youtubeHelper.getRedirects().invidious.normal,
...youtubeHelper.getRedirects().piped.normal,
...twitterHelper.getRedirects().nitter.normal,
...instagramHelper.getRedirects().bibliogram.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().scribe.normal,
...imgurHelper.getRedirects().rimgo.normal,
...wikipediaHelper.getRedirects().wikiless.normal
);
var mightyList = getMightyList();
if (mightyList.includes(protocolHost)) browser.pageAction.show(tabId);
});
browser.pageAction.onClicked.addListener((tab) => {
var tabUrl = new URL(tab.url);
function changeInstance(url) {
var tabUrl = new URL(url);
var protocolHost = `${tabUrl.protocol}//${tabUrl.host}`;
var newUrl;
if (
youtubeHelper.getRedirects().invidious.normal.includes(protocolHost) ||
youtubeHelper.getRedirects().piped.normal.includes(protocolHost)
youtubeHelper.getCustomRedirects().invidious.normal.includes(protocolHost) ||
youtubeHelper.getCustomRedirects().piped.normal.includes(protocolHost)
)
newUrl = 'https://youtube.com';
@ -167,5 +158,27 @@ browser.pageAction.onClicked.addListener((tab) => {
if (wikipediaHelper.getRedirects().wikiless.normal.includes(protocolHost)) newUrl = 'https://wikipedia.com';
if (newUrl) browser.tabs.update({ url: tabUrl.href.replace(protocolHost, newUrl) });
});
}
browser.pageAction.onClicked.addListener((tab) => changeInstance(tab.url));
function getMightyList() {
return [
...youtubeHelper.getCustomRedirects().invidious.normal,
...youtubeHelper.getCustomRedirects().piped.normal,
...twitterHelper.getCustomRedirects().nitter.normal,
...instagramHelper.getCustomRedirects().bibliogram.normal,
...redditHelper.getCustomRedirects().libreddit.normal,
...redditHelper.getCustomRedirects().teddit.normal,
redditHelper.getCustomRedirects().desktop,
redditHelper.getCustomRedirects().mobile,
...searchHelper.getCustomRedirects().searx.normal,
...searchHelper.getCustomRedirects().whoogle.normal,
...translateHelper.getCustomRedirects().simplyTranslate.normal,
...translateHelper.getCustomRedirects().lingva.normal,
...mediumHelper.getCustomRedirects().scribe.normal,
...imgurHelper.getCustomRedirects().rimgo.normal,
...wikipediaHelper.getCustomRedirects().wikiless.normal
];
}