diff --git a/src/assets/javascripts/helpers/common.js b/src/assets/javascripts/helpers/common.js index 82d70e2..74f8edf 100644 --- a/src/assets/javascripts/helpers/common.js +++ b/src/assets/javascripts/helpers/common.js @@ -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, diff --git a/src/assets/javascripts/helpers/imgur.js b/src/assets/javascripts/helpers/imgur.js index 3f5f945..437739d 100644 --- a/src/assets/javascripts/helpers/imgur.js +++ b/src/assets/javascripts/helpers/imgur.js @@ -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, diff --git a/src/assets/javascripts/helpers/instagram.js b/src/assets/javascripts/helpers/instagram.js index dc28990..51495dc 100644 --- a/src/assets/javascripts/helpers/instagram.js +++ b/src/assets/javascripts/helpers/instagram.js @@ -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, diff --git a/src/assets/javascripts/helpers/medium.js b/src/assets/javascripts/helpers/medium.js index 348313b..60aa6b6 100644 --- a/src/assets/javascripts/helpers/medium.js +++ b/src/assets/javascripts/helpers/medium.js @@ -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, diff --git a/src/assets/javascripts/helpers/reddit.js b/src/assets/javascripts/helpers/reddit.js index 0c8d635..740bb99 100644 --- a/src/assets/javascripts/helpers/reddit.js +++ b/src/assets/javascripts/helpers/reddit.js @@ -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, diff --git a/src/assets/javascripts/helpers/search.js b/src/assets/javascripts/helpers/search.js index c48a0ed..ed1063c 100644 --- a/src/assets/javascripts/helpers/search.js +++ b/src/assets/javascripts/helpers/search.js @@ -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, diff --git a/src/assets/javascripts/helpers/tiktok.js b/src/assets/javascripts/helpers/tiktok.js index c2eb2fb..5022db8 100644 --- a/src/assets/javascripts/helpers/tiktok.js +++ b/src/assets/javascripts/helpers/tiktok.js @@ -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, diff --git a/src/assets/javascripts/helpers/translate.js b/src/assets/javascripts/helpers/translate.js index 2f9b61d..074e0a6 100644 --- a/src/assets/javascripts/helpers/translate.js +++ b/src/assets/javascripts/helpers/translate.js @@ -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, diff --git a/src/assets/javascripts/helpers/twitter.js b/src/assets/javascripts/helpers/twitter.js index a56ceeb..782c41a 100644 --- a/src/assets/javascripts/helpers/twitter.js +++ b/src/assets/javascripts/helpers/twitter.js @@ -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 }) @@ -90,11 +97,11 @@ function setNitterRedirectsChecks(val) { nitterRedirectsChecks = val; browser.storage.sync.set({ nitterRedirectsChecks }) for (const item of nitterRedirectsChecks) - if (!redirects.nitter.normal.includes(item)) { - var index = nitterRedirectsChecks.indexOf(item); - if (index !== -1) nitterRedirectsChecks.splice(index, 1); - } -setNitterRedirectsChecks(nitterRedirectsChecks); + if (!redirects.nitter.normal.includes(item)) { + var index = nitterRedirectsChecks.indexOf(item); + if (index !== -1) nitterRedirectsChecks.splice(index, 1); + } + setNitterRedirectsChecks(nitterRedirectsChecks); console.log("nitterRedirectsChecks: ", val) } @@ -166,6 +173,7 @@ async function init() { export default { getRedirects, + getCustomRedirects, setRedirects, getDisableTwitter, diff --git a/src/assets/javascripts/helpers/wikipedia.js b/src/assets/javascripts/helpers/wikipedia.js index a2d84d0..62a09e1 100644 --- a/src/assets/javascripts/helpers/wikipedia.js +++ b/src/assets/javascripts/helpers/wikipedia.js @@ -16,16 +16,23 @@ 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 }) console.log("wikipediaRedirects: ", val) for (const item of wikilessRedirectsChecks) - if (!redirects.wikiless.normal.includes(item)) { - var index = wikilessRedirectsChecks.indexOf(item); - if (index !== -1) wikilessRedirectsChecks.splice(index, 1); - } -setWikilessRedirectsChecks(wikilessRedirectsChecks); + if (!redirects.wikiless.normal.includes(item)) { + var index = wikilessRedirectsChecks.indexOf(item); + if (index !== -1) wikilessRedirectsChecks.splice(index, 1); + } + setWikilessRedirectsChecks(wikilessRedirectsChecks); } let disableWikipedia; @@ -115,6 +122,7 @@ async function init() { export default { getRedirects, + getCustomRedirects, setRedirects, setDisableWikipedia, diff --git a/src/assets/javascripts/helpers/youtube/youtube.js b/src/assets/javascripts/helpers/youtube/youtube.js index 8e5f2ea..be20b72 100644 --- a/src/assets/javascripts/helpers/youtube/youtube.js +++ b/src/assets/javascripts/helpers/youtube/youtube.js @@ -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, diff --git a/src/pages/background/background.js b/src/pages/background/background.js index 140f171..aa97e2f 100644 --- a/src/pages/background/background.js +++ b/src/pages/background/background.js @@ -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: [""] } +); + 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 + ]; +}