diff --git a/src/_locales/ar/messages.json b/src/_locales/ar/messages.json index 9382d0c..f407c96 100644 --- a/src/_locales/ar/messages.json +++ b/src/_locales/ar/messages.json @@ -201,7 +201,7 @@ "toggleAll": { "message": "تبديل الكل", - "description": "used in common.js" + "description": "used in utils.js" }, "frontend": { diff --git a/src/_locales/de/messages.json b/src/_locales/de/messages.json index b7a9d26..50e9f24 100644 --- a/src/_locales/de/messages.json +++ b/src/_locales/de/messages.json @@ -201,7 +201,7 @@ "toggleAll": { "message": "Alle umschalten", - "description": "used in common.js" + "description": "used in utils.js" }, "frontend": { diff --git a/src/_locales/en/messages.json b/src/_locales/en/messages.json index 23a36ee..daeff4b 100644 --- a/src/_locales/en/messages.json +++ b/src/_locales/en/messages.json @@ -196,7 +196,7 @@ "toggleAll": { "message": "Toggle all", - "description": "used in common.js" + "description": "used in utils.js" }, "frontend": { diff --git a/src/assets/javascripts/helpers/imgur.js b/src/assets/javascripts/helpers/imgur.js index 8f8c28e..4b34afc 100644 --- a/src/assets/javascripts/helpers/imgur.js +++ b/src/assets/javascripts/helpers/imgur.js @@ -1,6 +1,6 @@ window.browser = window.browser || window.chrome; -import commonHelper from './common.js' +import utils from './utils.js' const targets = /^https?:\/{2}([im]\.)?imgur\.com(\/|$)/ @@ -46,42 +46,55 @@ let let rimgoNormalRedirectsChecks, rimgoTorRedirectsChecks, - rimgoI2pRedirectsChecks, - rimgoNormalCustomRedirects, - rimgoTorCustomRedirects, - rimgoI2pCustomRedirects; + rimgoI2pRedirectsChecks; function redirect(url, type, initiator) { - if (disable) return; - if (url.pathname == "/") return; - if (![ - "main_frame", - "sub_frame", - "xmlhttprequest", - "other", - "image", - "media", - ].includes(type)) return; - if ( - initiator && - ([...redirects.rimgo.normal, ...rimgoNormalCustomRedirects].includes(initiator.origin) || targets.test(initiator.host)) - ) return; - if (!targets.test(url.href)) return; - if (url.pathname.includes("delete/")) return; + return new Promise(resolve => { + browser.storage.local.get( + [ + "disableImgur", + "imgurRedirects", + "imgurProtocol", - // https://imgur.com/gallery/s4WXQmn - // https://imgur.com/a/H8M4rcp - // https://imgur.com/gallery/gYiQLWy - // https://imgur.com/gallery/cTRwaJU - // https://i.imgur.com/CFSQArP.jpeg - let instancesList; - if (protocol == 'normal') instancesList = [...rimgoNormalRedirectsChecks, ...rimgoNormalCustomRedirects]; - if (protocol == 'tor') instancesList = [...rimgoTorRedirectsChecks, ...rimgoTorCustomRedirects]; - if (protocol == 'i2p') instancesList = [...rimgoI2pRedirectsChecks, ...rimgoI2pCustomRedirects]; - if (instancesList.length === 0) return null; - let randomInstance = commonHelper.getRandomInstance(instancesList) + "rimgoNormalRedirectsChecks", + "rimgoNormalCustomRedirects", + "rimgoTorRedirectsChecks", + "rimgoTorCustomRedirects", + "rimgoI2pRedirectsChecks", + "rimgoI2pCustomRedirects", + ], + r => { + if (r.disableImgur) { resolve(); return; } + if (url.pathname == "/") { resolve(); return; } + if (!["main_frame", "sub_frame", "xmlhttprequest", "other", "image", "media",].includes(type)) { resolve(); return; } + if ( + initiator && + ( + [ + ...r.imgurRedirects.rimgo.normal, + ...r.rimgoNormalCustomRedirects, + ...r.rimgoTorCustomRedirects, + ...r.rimgoI2pCustomRedirects, + ].includes(initiator.origin) || targets.test(initiator.host)) + ) { resolve(); return; } + if (!targets.test(url.href)) { resolve(); return; } + if (url.pathname.includes("delete/")) { resolve(); return; } + // https://imgur.com/gallery/s4WXQmn + // https://imgur.com/a/H8M4rcp + // https://imgur.com/gallery/gYiQLWy + // https://imgur.com/gallery/cTRwaJU + // https://i.imgur.com/CFSQArP.jpeg + let instancesList; + if (r.imgurProtocol == 'normal') instancesList = [...r.rimgoNormalRedirectsChecks, ...r.rimgoNormalCustomRedirects]; + if (r.imgurProtocol == 'tor') instancesList = [...r.rimgoTorRedirectsChecks, ...r.rimgoTorCustomRedirects]; + if (r.imgurProtocol == 'i2p') instancesList = [...r.rimgoI2pRedirectsChecks, ...r.rimgoI2pCustomRedirects]; + if (instancesList.length === 0) { resolve(); return; } - return `${randomInstance}${url.pathname}${url.search}`; + let randomInstance = utils.getRandomInstance(instancesList) + resolve(`${randomInstance}${url.pathname}${url.search}`); + } + ) + }) } async function reverse(url) { @@ -93,7 +106,7 @@ async function reverse(url) { "rimgoI2pCustomRedirects", ], r => { - let protocolHost = commonHelper.protocolHost(url); + let protocolHost = utils.protocolHost(url); if ( ![ ...r.imgurRedirects.rimgo.normal, @@ -110,32 +123,47 @@ async function reverse(url) { } function switchInstance(url) { - let protocolHost = commonHelper.protocolHost(url); + return new Promise(resolve => { + browser.storage.local.get( + [ + "imgurRedirects", + "imgurProtocol", - let imgurList = [ - ...redirects.rimgo.normal, - ...redirects.rimgo.tor, - ...redirects.rimgo.i2p, + "rimgoNormalRedirectsChecks", + "rimgoNormalCustomRedirects", - ...rimgoNormalCustomRedirects, - ...rimgoTorCustomRedirects, - ...rimgoI2pCustomRedirects, - ]; - if (!imgurList.includes(protocolHost)) return null; + "rimgoTorRedirectsChecks", + "rimgoTorCustomRedirects", - let instancesList; - if (protocol == 'normal') instancesList = [...rimgoNormalCustomRedirects, ...rimgoNormalRedirectsChecks]; - else if (protocol == 'tor') instancesList = [...rimgoTorCustomRedirects, ...rimgoTorRedirectsChecks]; - else if (protocol == 'i2p') instancesList = [...rimgoI2pCustomRedirects, ...rimgoI2pRedirectsChecks]; + "rimgoI2pRedirectsChecks", + "rimgoI2pCustomRedirects", + ], + r => { + let protocolHost = utils.protocolHost(url); + if (![ + ...r.imgurRedirects.rimgo.normal, + ...r.imgurRedirects.rimgo.tor, + ...r.imgurRedirects.rimgo.i2p, - console.log("instancesList", instancesList); - let index = instancesList.indexOf(protocolHost); - if (index > -1) instancesList.splice(index, 1); + ...r.rimgoNormalCustomRedirects, + ...r.rimgoTorCustomRedirects, + ...r.rimgoI2pCustomRedirects, + ].includes(protocolHost)) resolve(); - if (instancesList.length === 0) return null; + let instancesList; + if (r.imgurProtocol == 'normal') instancesList = [...r.rimgoNormalCustomRedirects, ...r.rimgoNormalRedirectsChecks]; + else if (r.imgurProtocol == 'tor') instancesList = [...r.rimgoTorCustomRedirects, ...r.rimgoTorRedirectsChecks]; + else if (r.imgurProtocol == 'i2p') instancesList = [...r.rimgoI2pCustomRedirects, ...r.rimgoI2pRedirectsChecks]; - let randomInstance = commonHelper.getRandomInstance(instancesList); - return `${randomInstance}${url.pathname}${url.search}`; + let index = instancesList.indexOf(protocolHost); + if (index > -1) instancesList.splice(index, 1); + if (instancesList.length === 0) resolve(); + + let randomInstance = utils.getRandomInstance(instancesList); + resolve(`${randomInstance}${url.pathname}${url.search}`); + } + ) + }) } async function initDefaults() { @@ -168,46 +196,12 @@ async function initDefaults() { }); } -async function init() { - return new Promise(resolve => { - browser.storage.local.get( - [ - "disableImgur", - "imgurRedirects", - "imgurProtocol", - - "rimgoNormalRedirectsChecks", - "rimgoNormalCustomRedirects", - "rimgoTorRedirectsChecks", - "rimgoTorCustomRedirects", - "rimgoI2pRedirectsChecks", - "rimgoI2pCustomRedirects", - ], - r => { - disable = r.disableImgur; - protocol = r.imgurProtocol; - redirects = r.imgurRedirects; - - rimgoNormalRedirectsChecks = r.rimgoNormalRedirectsChecks; - rimgoNormalCustomRedirects = r.rimgoNormalCustomRedirects; - rimgoTorRedirectsChecks = r.rimgoTorRedirectsChecks; - rimgoTorCustomRedirects = r.rimgoTorCustomRedirects; - rimgoI2pRedirectsChecks = r.rimgoI2pRedirectsChecks; - rimgoI2pCustomRedirects = r.rimgoI2pCustomRedirects; - - resolve(); - } - ) - }); -} - export default { getRedirects, setRedirects, redirect, reverse, - init, initDefaults, switchInstance, }; diff --git a/src/assets/javascripts/helpers/instagram.js b/src/assets/javascripts/helpers/instagram.js index 3d4af08..621de39 100644 --- a/src/assets/javascripts/helpers/instagram.js +++ b/src/assets/javascripts/helpers/instagram.js @@ -1,5 +1,5 @@ window.browser = window.browser || window.chrome; -import commonHelper from './common.js' +import utils from './utils.js' const targets = [ "instagram.com", @@ -11,7 +11,6 @@ let redirects = { "tor": [] } }; -const getRedirects = () => redirects; function setRedirects(val) { redirects.bibliogram = val; browser.storage.local.set({ instagramRedirects: redirects }) @@ -25,66 +24,58 @@ function setRedirects(val) { } let bibliogramNormalRedirectsChecks; -let bibliogramTorRedirectsChecks; -let bibliogramNormalCustomRedirects = []; -let bibliogramTorCustomRedirects = []; - -let disable; //disableInstagram -let protocol; //instagramProtocol function redirect(url, type, initiator) { - if (disable) return; - if ( - initiator && - ([...redirects.bibliogram.normal, ...bibliogramNormalCustomRedirects].includes(initiator.origin) || targets.includes(initiator.host)) - ) return; + return new Promise(resolve => { + browser.storage.local.get( + [ + "disableInstagram", + "instagramProtocol", - if (!targets.includes(url.host)) return; + "instagramRedirects", - if (![ - "main_frame", - "sub_frame", - "xmlhttprequest", - "other", - "image", - "media", - ].includes(type)) return null; + "bibliogramNormalRedirectsChecks", + "bibliogramTorRedirectsChecks", - const bypassPaths = [ - /about/, - /explore/, - /support/, - /press/, - /api/, - /privacy/, - /safety/, - /admin/, - /\/(accounts\/|embeds?.js)/ - ]; + "bibliogramNormalCustomRedirects", + "bibliogramTorCustomRedirects", + ], + r => { + if (r.disableInstagram) { resolve(); return; } + if ( + initiator && + ([ + ...r.instagramRedirects.bibliogram.normal, + ...r.instagramRedirects.bibliogram.tor, + ...r.bibliogramNormalCustomRedirects, + ...r.bibliogramTorCustomRedirects, + ].includes(initiator.origin) || targets.includes(initiator.host)) + ) { resolve('BYPASSTAB'); return; } - if (bypassPaths.some(rx => rx.test(url.pathname))) return; + if (!targets.includes(url.host)) { resolve(); return; } + if (!["main_frame", "sub_frame", "xmlhttprequest", "other", "image", "media"].includes(type)) { resolve(); return; } - let instancesList; - if (protocol == 'normal') instancesList = [...bibliogramNormalRedirectsChecks, ...bibliogramNormalCustomRedirects]; - else if (protocol == 'tor') instancesList = [...bibliogramTorRedirectsChecks, ...bibliogramTorCustomRedirects]; - if (instancesList.length === 0) return null; - let randomInstance = commonHelper.getRandomInstance(instancesList) + const bypassPaths = [/about/, /explore/, /support/, /press/, /api/, /privacy/, /safety/, /admin/, /\/(accounts\/|embeds?.js)/]; + if (bypassPaths.some(rx => rx.test(url.pathname))) { resolve(); return; } - const reservedPaths = [ - "u", - "p", - "privacy", - ]; + let instancesList; + if (r.instagramProtocol == 'normal') instancesList = [...r.bibliogramNormalRedirectsChecks, ...r.bibliogramNormalCustomRedirects]; + else if (r.instagramProtocol == 'tor') instancesList = [...r.bibliogramTorRedirectsChecks, ...r.bibliogramTorCustomRedirects]; + if (instancesList.length === 0) { resolve(); return; } + let randomInstance = utils.getRandomInstance(instancesList) - if (url.pathname === "/" || reservedPaths.includes(url.pathname.split("/")[1])) - return `${randomInstance}${url.pathname}${url.search}`; - if (url.pathname.startsWith("/reel") || url.pathname.startsWith("/tv")) - return `${randomInstance}/p${url.pathname.replace(/\/reel|\/tv/i, '')}${url.search}`; - else - return `${randomInstance}/u${url.pathname}${url.search}`; // Likely a user profile, redirect to '/u/...' + const reservedPaths = ["u", "p", "privacy",]; + if (url.pathname === "/" || reservedPaths.includes(url.pathname.split("/")[1])) + resolve(`${randomInstance}${url.pathname}${url.search}`); + if (url.pathname.startsWith("/reel") || url.pathname.startsWith("/tv")) + resolve(`${randomInstance}/p${url.pathname.replace(/\/reel|\/tv/i, '')}${url.search}`); + else + resolve(`${randomInstance}/u${url.pathname}${url.search}`); // Likely a user profile, redirect to '/u/...' + } + ) + }) } - -async function reverse(url) { +function reverse(url) { browser.storage.local.get( [ "instagramRedirects", @@ -92,7 +83,7 @@ async function reverse(url) { "bibliogramTorCustomRedirects", ], r => { - let protocolHost = commonHelper.protocolHost(url); + let protocolHost = utils.protocolHost(url); if ( ![ ...r.instagramRedirects.bibliogram.normal, @@ -114,29 +105,41 @@ async function reverse(url) { } function switchInstance(url) { - let protocolHost = commonHelper.protocolHost(url); + return new Promise(resolve => { + browser.storage.local.get( + [ + "instagramRedirects", + "instagramProtocol", - let instagramList = [ - ...redirects.bibliogram.normal, - ...redirects.bibliogram.tor, - ...bibliogramNormalCustomRedirects, - ...bibliogramTorCustomRedirects, - ]; + "bibliogramNormalRedirectsChecks", + "bibliogramTorRedirectsChecks", - if (!instagramList.includes(protocolHost)) return null; + "bibliogramNormalCustomRedirects", + "bibliogramTorCustomRedirects", + ], + r => { + let protocolHost = utils.protocolHost(url); - let instancesList; - if (protocol == 'normal') instancesList = [...bibliogramNormalCustomRedirects, ...bibliogramNormalRedirectsChecks]; - else if (protocol == 'tor') instancesList = [...bibliogramTorCustomRedirects, ...bibliogramTorRedirectsChecks]; + if (![ + ...r.instagramRedirects.bibliogram.normal, + ...r.instagramRedirects.bibliogram.tor, + ...r.bibliogramNormalCustomRedirects, + ...r.bibliogramTorCustomRedirects, + ].includes(protocolHost)) { resolve(); return; } - console.log("instancesList", instancesList); - let index = instancesList.indexOf(protocolHost); - if (index > -1) instancesList.splice(index, 1); + let instancesList; + if (r.instagramProtocol == 'normal') instancesList = [...r.bibliogramNormalCustomRedirects, ...r.bibliogramNormalRedirectsChecks]; + else if (r.instagramProtocol == 'tor') instancesList = [...r.bibliogramTorCustomRedirects, ...r.bibliogramTorRedirectsChecks]; - if (instancesList.length === 0) return null; + let index = instancesList.indexOf(protocolHost); + if (index > -1) instancesList.splice(index, 1); + if (instancesList.length === 0) { resolve(); return; } - let randomInstance = commonHelper.getRandomInstance(instancesList); - return `${randomInstance}${url.pathname}${url.search}`; + let randomInstance = utils.getRandomInstance(instancesList); + resolve(`${randomInstance}${url.pathname}${url.search}`); + } + ) + }) } function initDefaults() { @@ -168,46 +171,12 @@ function initDefaults() { }) } -async function init() { - return new Promise(resolve => { - browser.storage.local.get( - [ - "disableInstagram", - "instagramRedirects", - - "bibliogramNormalRedirectsChecks", - "bibliogramTorRedirectsChecks", - - "bibliogramNormalCustomRedirects", - "bibliogramTorCustomRedirects", - "instagramProtocol" - ], - r => { - disable = r.disableInstagram; - if (r.instagramRedirects) redirects = r.instagramRedirects - - bibliogramNormalRedirectsChecks = r.bibliogramNormalRedirectsChecks; - bibliogramNormalCustomRedirects = r.bibliogramNormalCustomRedirects; - - bibliogramTorRedirectsChecks = r.bibliogramTorRedirectsChecks; - bibliogramTorCustomRedirects = r.bibliogramTorCustomRedirects; - - protocol = r.instagramProtocol; - - resolve(); - } - ) - }) -} - export default { - getRedirects, setRedirects, reverse, redirect, - init, initDefaults, switchInstance, }; diff --git a/src/assets/javascripts/helpers/lbry.js b/src/assets/javascripts/helpers/lbry.js index 6010574..6c51cdd 100644 --- a/src/assets/javascripts/helpers/lbry.js +++ b/src/assets/javascripts/helpers/lbry.js @@ -1,6 +1,6 @@ window.browser = window.browser || window.chrome; -import commonHelper from './common.js' +import utils from './utils.js' let targets = ["odysee.com"]; @@ -21,8 +21,6 @@ let redirects = { } } -const getRedirects = () => redirects; - function setRedirects(val) { redirects.librarian = val; browser.storage.local.set({ lbryTargetsRedirects: redirects }) @@ -44,52 +42,82 @@ function setRedirects(val) { let librarianNormalRedirectsChecks; let librarianTorRedirectsChecks; -let librarianNormalCustomRedirects = []; -let librarianTorCustomRedirects = []; -let disable; // disableLbryTargets -let protocol; // lbryTargetsProtocol +async function switchInstance(url) { + return new Promise(resolve => { + browser.storage.local.get( + [ + "lbryTargetsRedirects", + "lbryTargetsProtocol", -function switchInstance(url) { - let protocolHost = commonHelper.protocolHost(url); + "librarianNormalRedirectsChecks", + "librarianNormalCustomRedirects", - let librarianList = [ - ...redirects.librarian.normal, - ...redirects.librarian.tor, - ...librarianNormalCustomRedirects, - ...librarianTorCustomRedirects, - ]; + "librarianTorRedirectsChecks", + "librarianTorCustomRedirects", + ], + r => { + let protocolHost = utils.protocolHost(url); + if (![ + ...redirects.librarian.normal, + ...redirects.librarian.tor, + ...r.librarianNormalCustomRedirects, + ...r.librarianTorCustomRedirects, + ].includes(protocolHost)) resolve(); - if (!librarianList.includes(protocolHost)) return; + let instancesList; + if (r.lbryTargetsProtocol == 'normal') instancesList = [...r.librarianNormalRedirectsChecks, ...r.librarianNormalCustomRedirects]; + else if (r.lbryTargetsProtocol == 'tor') instancesList = [...r.librarianTorRedirectsChecks, ...r.librarianTorCustomRedirects]; - let instancesList; - if (protocol == 'normal') instancesList = [...librarianNormalRedirectsChecks, ...librarianNormalCustomRedirects]; - else if (protocol == 'tor') instancesList = [...librarianTorRedirectsChecks, ...librarianTorCustomRedirects]; + let index = instancesList.indexOf(protocolHost); + if (index > -1) instancesList.splice(index, 1); + if (instancesList.length === 0) resolve(); - console.log("instancesList", instancesList); - let index = instancesList.indexOf(protocolHost); - if (index > -1) instancesList.splice(index, 1); - - if (instancesList.length === 0) return null; - - let randomInstance = commonHelper.getRandomInstance(instancesList); - return `${randomInstance}${url.pathname}${url.search}`; + let randomInstance = utils.getRandomInstance(instancesList); + resolve(`${randomInstance}${url.pathname}${url.search}`); + } + ) + }) } function redirect(url, type, initiator) { - if (disable) return null; - if (initiator && ([...redirects.librarian.normal, ...librarianNormalCustomRedirects].includes(initiator.origin) || targets.includes(initiator.host))) return null; - if (!targets.includes(url.host)) return null; + return new Promise(resolve => { + browser.storage.local.get( + [ + "disableLbryTargets", + "lbryTargetsProtocol", - if (type != "main_frame") return null; + "lbryTargetsRedirects", - let instancesList; - if (protocol == 'normal') instancesList = [...librarianNormalRedirectsChecks, ...librarianNormalCustomRedirects]; - if (protocol == 'tor') instancesList = [...librarianTorRedirectsChecks, ...librarianTorCustomRedirects]; - if (instancesList.length === 0) return null; - let randomInstance = commonHelper.getRandomInstance(instancesList); + "librarianNormalRedirectsChecks", + "librarianNormalCustomRedirects", - return `${randomInstance}${url.pathname}${url.search}`; + "librarianTorRedirectsChecks", + "librarianTorCustomRedirects", + ], + r => { + if (r.disableLbryTargets) { resolve(); return; } + if (initiator && ( + [ + ...r.lbryTargetsRedirects.librarian.normal, + ...r.librarianNormalCustomRedirects, + ...r.librarianTorCustomRedirects, + ].includes(initiator.origin) || + targets.includes(initiator.host)) + ) { resolve(); return; } + if (!targets.includes(url.host)) { resolve(); return; } + if (type != "main_frame") { resolve(); return; } + + let instancesList; + if (r.lbryTargetsProtocol == 'normal') instancesList = [...r.librarianNormalRedirectsChecks, ...r.librarianNormalCustomRedirects]; + if (r.lbryTargetsProtocol == 'tor') instancesList = [...r.librarianTorRedirectsChecks, ...r.librarianTorCustomRedirects]; + if (instancesList.length === 0) { resolve(); return; } + + let randomInstance = utils.getRandomInstance(instancesList); + resolve(`${randomInstance}${url.pathname}${url.search}`); + } + ) + }) } async function initDefaults() { @@ -118,47 +146,10 @@ async function initDefaults() { }) } -async function init() { - return new Promise(resolve => { - browser.storage.local.get( - [ - "disableLbryTargets", - "lbryTargetsRedirects", - - "librarianNormalRedirectsChecks", - "librarianNormalCustomRedirects", - - "librarianTorRedirectsChecks", - "librarianTorCustomRedirects", - - "lbryTargetsProtocol" - ], - r => { - disable = r.disableLbryTargets; - - protocol = r.lbryTargetsProtocol; - - redirects = r.lbryTargetsRedirects; - - librarianNormalRedirectsChecks = r.librarianNormalRedirectsChecks; - librarianNormalCustomRedirects = r.librarianNormalCustomRedirects; - - librarianTorRedirectsChecks = r.librarianTorRedirectsChecks; - librarianTorCustomRedirects = r.librarianTorCustomRedirects; - - resolve(); - } - ) - }); -} - export default { - - getRedirects, setRedirects, switchInstance, redirect, - init, initDefaults, }; diff --git a/src/assets/javascripts/helpers/maps.js b/src/assets/javascripts/helpers/maps.js index fa49896..357221a 100644 --- a/src/assets/javascripts/helpers/maps.js +++ b/src/assets/javascripts/helpers/maps.js @@ -1,7 +1,7 @@ "use strict"; window.browser = window.browser || window.chrome; -import commonHelper from './common.js' +import utils from './utils.js' const targets = /^https?:\/{2}(((www|maps)\.)?(google\.).*(\/maps)|maps\.(google\.).*)/; @@ -80,12 +80,11 @@ async function redirect(url, initiator) { if (!url.href.match(targets)) return; let randomInstance; - if (frontend == 'osm') randomInstance = commonHelper.getRandomInstance(redirects.osm.normal); - if (frontend == 'facil') randomInstance = commonHelper.getRandomInstance([...facilNormalRedirectsChecks, ...facilNormalCustomRedirects]); + if (frontend == 'osm') randomInstance = utils.getRandomInstance(redirects.osm.normal); + if (frontend == 'facil') randomInstance = utils.getRandomInstance([...facilNormalRedirectsChecks, ...facilNormalCustomRedirects]); let mapCentre = "#"; let prefs = {}; - let facilPrefs = {}; if (url.pathname.match(mapCentreRegex)) { // Set map centre if present var [, lat, lon, zoom] = url.pathname.match(mapCentreRegex); diff --git a/src/assets/javascripts/helpers/medium.js b/src/assets/javascripts/helpers/medium.js index 5306f47..a0892df 100644 --- a/src/assets/javascripts/helpers/medium.js +++ b/src/assets/javascripts/helpers/medium.js @@ -1,5 +1,5 @@ window.browser = window.browser || window.chrome; -import commonHelper from './common.js' +import utils from './utils.js' const targets = [ @@ -59,56 +59,78 @@ function setRedirects(val) { let scribeNormalRedirectsChecks; let scribeTorRedirectsChecks; -let scribeNormalCustomRedirects = []; -let scribeTorCustomRedirects = []; - -let disable; -let protocol; function redirect(url, type, initiator) { + return new Promise(resolve => { + browser.storage.local.get( + [ + "disableMedium", + "mediumRedirects", + "scribeNormalRedirectsChecks", + "scribeNormalCustomRedirects", + "scribeTorRedirectsChecks", + "scribeTorCustomRedirects", + "mediumProtocol" + ], + r => { + if (r.disableMedium) { resolve(); return; } + if (type != "main_frame" && "sub_frame" && "xmlhttprequest" && "other") { resolve(); return; } + if (initiator && ( + [ + ...r.mediumRedirects.scribe.normal, + ...r.mediumRedirects.scribe.tor, + ...r.scribeNormalCustomRedirects, + ...r.scribeTorCustomRedirects, + ].includes(initiator.origin))) { resolve(); return; } - if (disable) return; + if (!targets.some(rx => rx.test(url.host))) { resolve(); return; } + if (/^\/($|@[a-zA-Z.]{0,}(\/|)$)/.test(url.pathname)) { resolve(); return; } - if (type != "main_frame" && "sub_frame" && "xmlhttprequest" && "other") return; + let instancesList; + if (r.mediumProtocol == 'normal') instancesList = [...r.scribeNormalRedirectsChecks, ...r.scribeNormalCustomRedirects]; + else if (r.mediumProtocol == 'tor') instancesList = [...r.scribeTorRedirectsChecks, ...r.scribeTorCustomRedirects]; + if (instancesList.length === 0) { resolve(); return; } - if (initiator && ([...redirects.scribe.normal, ...scribeNormalCustomRedirects].includes(initiator.origin))) return; - - if (!targets.some(rx => rx.test(url.host))) return; - if (/^\/($|@[a-zA-Z.]{0,}(\/|)$)/.test(url.pathname)) return; - - let instancesList; - if (protocol == 'normal') instancesList = [...scribeNormalRedirectsChecks, ...scribeNormalCustomRedirects]; - else if (protocol == 'tor') instancesList = [...scribeTorRedirectsChecks, ...scribeTorCustomRedirects]; - if (instancesList.length === 0) return null; - let randomInstance = commonHelper.getRandomInstance(instancesList) - - return `${randomInstance}${url.pathname}${url.search}`; + let randomInstance = utils.getRandomInstance(instancesList) + resolve(`${randomInstance}${url.pathname}${url.search}`); + } + ) + }) } - function switchInstance(url) { - let protocolHost = commonHelper.protocolHost(url); + return new Promise(resolve => { + browser.storage.local.get( + [ + "mediumRedirects", + "mediumProtocol", - let mediumList = [ - ...redirects.scribe.tor, - ...redirects.scribe.normal, + "scribeNormalRedirectsChecks", + "scribeNormalCustomRedirects", + "scribeTorRedirectsChecks", + "scribeTorCustomRedirects", + ], + r => { + let protocolHost = utils.protocolHost(url); + if (![ + ...r.mediumRedirects.scribe.tor, + ...r.mediumRedirects.scribe.normal, - ...scribeNormalCustomRedirects, - ...scribeTorCustomRedirects, - ]; + ...r.scribeNormalCustomRedirects, + ...r.scribeTorCustomRedirects, + ].includes(protocolHost)) { resolve(); return; } - if (!mediumList.includes(protocolHost)) return null; + let instancesList; + if (r.mediumProtocol == 'normal') instancesList = [...r.scribeNormalCustomRedirects, ...r.scribeNormalRedirectsChecks]; + else if (r.mediumProtocol == 'tor') instancesList = [...r.scribeTorCustomRedirects, ...r.scribeTorRedirectsChecks]; - let instancesList; - if (protocol == 'normal') instancesList = [...scribeNormalCustomRedirects, ...scribeNormalRedirectsChecks]; - else if (protocol == 'tor') instancesList = [...scribeTorCustomRedirects, ...scribeTorRedirectsChecks]; + let index = instancesList.indexOf(protocolHost); + if (index > -1) instancesList.splice(index, 1); + if (instancesList.length === 0) { resolve(); return; } - let index = instancesList.indexOf(protocolHost); - if (index > -1) instancesList.splice(index, 1); - - if (instancesList.length === 0) return null; - - let randomInstance = commonHelper.getRandomInstance(instancesList); - return `${randomInstance}${url.pathname}${url.search}`; + let randomInstance = utils.getRandomInstance(instancesList); + resolve(`${randomInstance}${url.pathname}${url.search}`); + }) + }) } function initDefaults() { @@ -137,41 +159,11 @@ function initDefaults() { }) } -async function init() { - return new Promise(resolve => { - browser.storage.local.get( - [ - "disableMedium", - "mediumRedirects", - "scribeNormalRedirectsChecks", - "scribeNormalCustomRedirects", - "scribeTorRedirectsChecks", - "scribeTorCustomRedirects", - "mediumProtocol" - ], - r => { - disable = r.disableMedium; - protocol = r.mediumProtocol; - redirects = r.mediumRedirects; - - scribeNormalRedirectsChecks = r.scribeNormalRedirectsChecks; - scribeNormalCustomRedirects = r.scribeNormalCustomRedirects; - - scribeTorRedirectsChecks = r.scribeTorRedirectsChecks; - scribeTorCustomRedirects = r.scribeTorCustomRedirects; - - resolve(); - } - ) - }) -} - export default { getRedirects, setRedirects, redirect, - init, initDefaults, switchInstance, }; diff --git a/src/assets/javascripts/helpers/peertube.js b/src/assets/javascripts/helpers/peertube.js index 7fca2b6..c431d25 100644 --- a/src/assets/javascripts/helpers/peertube.js +++ b/src/assets/javascripts/helpers/peertube.js @@ -1,6 +1,6 @@ window.browser = window.browser || window.chrome; -import commonHelper from './common.js' +import utils from './utils.js' let targets = []; @@ -46,30 +46,41 @@ let simpleertubeTorCustomRedirects = []; let disable; let protocol; -function switchInstance(url) { - let protocolHost = commonHelper.protocolHost(url); +async function switchInstance(url) { + return new Promise(resolve => { + browser.storage.local.get( + [ + "peerTubeTargets", + "peertubeTargetsProtocol", - let simpleertubeList = [ - ...redirects.simpleertube.normal, - ...redirects.simpleertube.tor, - ...simpleertubeNormalCustomRedirects, - ...simpleertubeTorCustomRedirects, - ]; + "simpleertubeNormalRedirectsChecks", + "simpleertubeNormalCustomRedirects", - if (!simpleertubeList.includes(protocolHost)) return; + "simpleertubeTorRedirectsChecks", + "simpleertubeTorCustomRedirects", + ], + r => { + let protocolHost = utils.protocolHost(url); + if (![ + ...redirects.simpleertube.normal, + ...redirects.simpleertube.tor, + ...r.simpleertubeNormalCustomRedirects, + ...r.simpleertubeTorCustomRedirects, + ].includes(protocolHost)) resolve(); - let instancesList; - if (protocol == 'normal') instancesList = [...simpleertubeNormalRedirectsChecks, ...simpleertubeNormalCustomRedirects]; - else if (protocol == 'tor') instancesList = [...simpleertubeTorRedirectsChecks, ...simpleertubeTorCustomRedirects]; + let instancesList; + if (r.peertubeTargetsProtocol == 'normal') instancesList = [...r.simpleertubeNormalRedirectsChecks, ...r.simpleertubeNormalCustomRedirects]; + else if (r.peertubeTargetsProtocol == 'tor') instancesList = [...r.simpleertubeTorRedirectsChecks, ...r.simpleertubeTorCustomRedirects]; - console.log("instancesList", instancesList); - let index = instancesList.indexOf(protocolHost); - if (index > -1) instancesList.splice(index, 1); + let index = instancesList.indexOf(protocolHost); + if (index > -1) instancesList.splice(index, 1); + if (instancesList.length === 0) resolve() - if (instancesList.length === 0) return null; - - let randomInstance = commonHelper.getRandomInstance(instancesList); - return `${randomInstance}${url.pathname}${url.search}`; + let randomInstance = utils.getRandomInstance(instancesList); + resolve(`${randomInstance}${url.pathname}${url.search}`); + } + ) + }) } function redirect(url, type, initiator) { @@ -84,7 +95,7 @@ function redirect(url, type, initiator) { targets.includes(initiator.host) ) ) return null; - let protocolHost = commonHelper.protocolHost(url); + let protocolHost = utils.protocolHost(url); if (!targets.includes(protocolHost)) return null; if (type != "main_frame") return null; @@ -93,7 +104,7 @@ function redirect(url, type, initiator) { if (protocol == 'normal') instancesList = [...simpleertubeNormalRedirectsChecks, ...simpleertubeNormalCustomRedirects]; if (protocol == 'tor') instancesList = [...simpleertubeTorRedirectsChecks, ...simpleertubeTorCustomRedirects]; if (instancesList.length === 0) return null; - let randomInstance = commonHelper.getRandomInstance(instancesList); + let randomInstance = utils.getRandomInstance(instancesList); if (url.host == 'search.joinpeertube.org') return randomInstance; diff --git a/src/assets/javascripts/helpers/reddit.js b/src/assets/javascripts/helpers/reddit.js index def751a..1bb54b7 100644 --- a/src/assets/javascripts/helpers/reddit.js +++ b/src/assets/javascripts/helpers/reddit.js @@ -1,6 +1,6 @@ window.browser = window.browser || window.chrome; -import commonHelper from './common.js' +import utils from './utils.js' const targets = [ /^https?:\/{2}(www\.|old\.|np\.|new\.|amp\.|)reddit\.com/, @@ -66,13 +66,6 @@ let tedditTorRedirectsChecks; let tedditNormalCustomRedirects = []; let tedditTorCustomRedirects = []; -const bypassPaths = /\/(gallery\/poll\/rpan\/settings\/topics)/; - -let - disableReddit, - frontend, - protocol; - function initLibredditCookies(from) { return new Promise(resolve => { browser.storage.local.get( @@ -84,7 +77,7 @@ function initLibredditCookies(from) { "libredditTorCustomRedirects", ], r => { - let protocolHost = commonHelper.protocolHost(from); + let protocolHost = utils.protocolHost(from); if (![ ...r.libredditNormalRedirectsChecks, ...r.libredditTorRedirectsChecks, @@ -96,16 +89,16 @@ function initLibredditCookies(from) { if (r.redditProtocol == 'normal') checkedInstances = [...r.libredditNormalRedirectsChecks, ...r.libredditNormalCustomRedirects]; else if (r.redditProtocol == 'tor') checkedInstances = [...r.libredditTorRedirectsChecks, ...r.libredditTorCustomRedirects]; for (const to of checkedInstances) { - commonHelper.copyCookie('libreddit', from, to, "theme"); - commonHelper.copyCookie('libreddit', from, to, "front_page"); - commonHelper.copyCookie('libreddit', from, to, "layout"); - commonHelper.copyCookie('libreddit', from, to, "wide"); - commonHelper.copyCookie('libreddit', from, to, "post_sort"); - commonHelper.copyCookie('libreddit', from, to, "comment_sort"); - commonHelper.copyCookie('libreddit', from, to, "show_nsfw"); - commonHelper.copyCookie('libreddit', from, to, "autoplay_videos"); - commonHelper.copyCookie('libreddit', from, to, "use_hls"); - commonHelper.copyCookie('libreddit', from, to, "hide_hls_notification"); + utils.copyCookie('libreddit', from, to, "theme"); + utils.copyCookie('libreddit', from, to, "front_page"); + utils.copyCookie('libreddit', from, to, "layout"); + utils.copyCookie('libreddit', from, to, "wide"); + utils.copyCookie('libreddit', from, to, "post_sort"); + utils.copyCookie('libreddit', from, to, "comment_sort"); + utils.copyCookie('libreddit', from, to, "show_nsfw"); + utils.copyCookie('libreddit', from, to, "autoplay_videos"); + utils.copyCookie('libreddit', from, to, "use_hls"); + utils.copyCookie('libreddit', from, to, "hide_hls_notification"); } resolve(true); } @@ -131,16 +124,16 @@ function setLibredditCookies() { if (r.redditProtocol == 'normal') checkedInstances = [...r.libredditNormalRedirectsChecks, ...r.libredditNormalCustomRedirects] else if (r.redditProtocol == 'tor') checkedInstances = [...r.libredditTorRedirectsChecks, ...r.libredditTorCustomRedirects] for (const to of checkedInstances) { - commonHelper.getCookiesFromStorage('libreddit', to, "theme"); - commonHelper.getCookiesFromStorage('libreddit', to, "front_page"); - commonHelper.getCookiesFromStorage('libreddit', to, "layout"); - commonHelper.getCookiesFromStorage('libreddit', to, "wide"); - commonHelper.getCookiesFromStorage('libreddit', to, "post_sort"); - commonHelper.getCookiesFromStorage('libreddit', to, "comment_sort"); - commonHelper.getCookiesFromStorage('libreddit', to, "show_nsfw"); - commonHelper.getCookiesFromStorage('libreddit', to, "autoplay_videos"); - commonHelper.getCookiesFromStorage('libreddit', to, "use_hls"); - commonHelper.getCookiesFromStorage('libreddit', to, "hide_hls_notification"); + utils.getCookiesFromStorage('libreddit', to, "theme"); + utils.getCookiesFromStorage('libreddit', to, "front_page"); + utils.getCookiesFromStorage('libreddit', to, "layout"); + utils.getCookiesFromStorage('libreddit', to, "wide"); + utils.getCookiesFromStorage('libreddit', to, "post_sort"); + utils.getCookiesFromStorage('libreddit', to, "comment_sort"); + utils.getCookiesFromStorage('libreddit', to, "show_nsfw"); + utils.getCookiesFromStorage('libreddit', to, "autoplay_videos"); + utils.getCookiesFromStorage('libreddit', to, "use_hls"); + utils.getCookiesFromStorage('libreddit', to, "hide_hls_notification"); } } ) @@ -157,7 +150,7 @@ function initTedditCookies(from) { "tedditTorCustomRedirects", ], r => { - let protocolHost = commonHelper.protocolHost(from); + let protocolHost = utils.protocolHost(from); if (![ ...r.tedditNormalRedirectsChecks, ...r.tedditTorRedirectsChecks, @@ -169,18 +162,18 @@ function initTedditCookies(from) { if (r.redditProtocol == 'normal') checkedInstances = [...r.tedditNormalRedirectsChecks, ...r.tedditNormalCustomRedirects] else if (r.redditProtocol == 'tor') checkedInstances = [...r.tedditTorRedirectsChecks, ...r.tedditTorCustomRedirects] for (const to of checkedInstances) { - commonHelper.copyCookie('teddit', from, to, 'collapse_child_comments') - commonHelper.copyCookie('teddit', from, to, 'domain_instagram') - commonHelper.copyCookie('teddit', from, to, 'domain_twitter') - commonHelper.copyCookie('teddit', from, to, 'domain_youtube') - commonHelper.copyCookie('teddit', from, to, 'flairs') - commonHelper.copyCookie('teddit', from, to, 'highlight_controversial') - commonHelper.copyCookie('teddit', from, to, 'nsfw_enabled') - commonHelper.copyCookie('teddit', from, to, 'post_media_max_height') - commonHelper.copyCookie('teddit', from, to, 'show_upvoted_percentage') - commonHelper.copyCookie('teddit', from, to, 'show_upvotes') - commonHelper.copyCookie('teddit', from, to, 'theme') - commonHelper.copyCookie('teddit', from, to, 'videos_muted') + utils.copyCookie('teddit', from, to, 'collapse_child_comments') + utils.copyCookie('teddit', from, to, 'domain_instagram') + utils.copyCookie('teddit', from, to, 'domain_twitter') + utils.copyCookie('teddit', from, to, 'domain_youtube') + utils.copyCookie('teddit', from, to, 'flairs') + utils.copyCookie('teddit', from, to, 'highlight_controversial') + utils.copyCookie('teddit', from, to, 'nsfw_enabled') + utils.copyCookie('teddit', from, to, 'post_media_max_height') + utils.copyCookie('teddit', from, to, 'show_upvoted_percentage') + utils.copyCookie('teddit', from, to, 'show_upvotes') + utils.copyCookie('teddit', from, to, 'theme') + utils.copyCookie('teddit', from, to, 'videos_muted') } resolve(true); } @@ -205,114 +198,137 @@ function setTedditCookies() { if (r.redditProtocol == 'normal') checkedInstances = [...r.tedditNormalRedirectsChecks, ...r.tedditNormalCustomRedirects] else if (r.redditProtocol == 'tor') checkedInstances = [...r.tedditTorRedirectsChecks, ...r.tedditTorCustomRedirects] for (const to of checkedInstances) { - commonHelper.getCookiesFromStorage('teddit', to, 'collapse_child_comments') - commonHelper.getCookiesFromStorage('teddit', to, 'domain_instagram') - commonHelper.getCookiesFromStorage('teddit', to, 'domain_twitter') - commonHelper.getCookiesFromStorage('teddit', to, 'domain_youtube') - commonHelper.getCookiesFromStorage('teddit', to, 'flairs') - commonHelper.getCookiesFromStorage('teddit', to, 'highlight_controversial') - commonHelper.getCookiesFromStorage('teddit', to, 'nsfw_enabled') - commonHelper.getCookiesFromStorage('teddit', to, 'post_media_max_height') - commonHelper.getCookiesFromStorage('teddit', to, 'show_upvoted_percentage') - commonHelper.getCookiesFromStorage('teddit', to, 'show_upvotes') - commonHelper.getCookiesFromStorage('teddit', to, 'theme') - commonHelper.getCookiesFromStorage('teddit', to, 'videos_muted') + utils.getCookiesFromStorage('teddit', to, 'collapse_child_comments') + utils.getCookiesFromStorage('teddit', to, 'domain_instagram') + utils.getCookiesFromStorage('teddit', to, 'domain_twitter') + utils.getCookiesFromStorage('teddit', to, 'domain_youtube') + utils.getCookiesFromStorage('teddit', to, 'flairs') + utils.getCookiesFromStorage('teddit', to, 'highlight_controversial') + utils.getCookiesFromStorage('teddit', to, 'nsfw_enabled') + utils.getCookiesFromStorage('teddit', to, 'post_media_max_height') + utils.getCookiesFromStorage('teddit', to, 'show_upvoted_percentage') + utils.getCookiesFromStorage('teddit', to, 'show_upvotes') + utils.getCookiesFromStorage('teddit', to, 'theme') + utils.getCookiesFromStorage('teddit', to, 'videos_muted') } } ) } function redirect(url, type, initiator) { - // https://libreddit.exonip.de/vid/1mq8d0ma3yk81/720.mp4 - // https://libreddit.exonip.de/img/4v3t1vgvrzk81.png + return new Promise(resolve => { + browser.storage.local.get( + [ + "disableReddit", + "redditFrontend", + "redditRedirects", + "redditProtocol", - // https://teddit.net/vids/1mq8d0ma3yk81.mp4 - // https://teddit.net/pics/w:null_4v3t1vgvrzk81.png + "libredditNormalRedirectsChecks", + "libredditNormalCustomRedirects", + "libredditTorRedirectsChecks", + "libredditTorCustomRedirects", + + "tedditNormalRedirectsChecks", + "tedditNormalCustomRedirects", + "tedditTorRedirectsChecks", + "tedditTorCustomRedirects", + ], + r => { + // https://libreddit.exonip.de/vid/1mq8d0ma3yk81/720.mp4 + // https://libreddit.exonip.de/img/4v3t1vgvrzk81.png + + // https://teddit.net/vids/1mq8d0ma3yk81.mp4 + // https://teddit.net/pics/w:null_4v3t1vgvrzk81.png - // redd.it/t5379n - // https://v.redd.it/z08avb339n801/DASH_1_2_M - // https://i.redd.it/bfkhs659tzk81.jpg + // redd.it/t5379n + // https://v.redd.it/z08avb339n801/DASH_1_2_M + // https://i.redd.it/bfkhs659tzk81.jpg - if (disableReddit) return null; - if (!targets.some(rx => rx.test(url.href))) return null; + if (r.disableReddit) { resolve(); return; } + if (!targets.some(rx => rx.test(url.href))) { resolve(); return; } - if ( - initiator && - [...redirects.libreddit.normal, - ...redirects.libreddit.tor, - ...libredditNormalCustomRedirects, - ...libredditTorCustomRedirects, - ...redirects.teddit.normal, - ...redirects.teddit.tor, - ...tedditNormalCustomRedirects, - ...tedditTorCustomRedirects, - ].includes(initiator.origin) - ) return 'BYPASSTAB'; + if ( + initiator && + [ + ...r.redditRedirects.libreddit.normal, + ...r.redditRedirects.libreddit.tor, + ...r.redditRedirects.teddit.normal, + ...r.redditRedirects.teddit.tor, + ...r.libredditNormalCustomRedirects, + ...r.libredditTorCustomRedirects, + ...r.tedditNormalCustomRedirects, + ...r.tedditTorCustomRedirects, + ].includes(initiator.origin) + ) { resolve('BYPASSTAB'); return; } - if (type !== "main_frame" || url.pathname.match(bypassPaths)) return null; + const bypassPaths = /\/(gallery\/poll\/rpan\/settings\/topics)/; + if (type !== "main_frame" || url.pathname.match(bypassPaths)) { resolve(); return; } - let libredditInstancesList; - let tedditInstancesList; - if (protocol == 'normal') { - libredditInstancesList = [...libredditNormalRedirectsChecks, ...libredditNormalCustomRedirects]; - tedditInstancesList = [...tedditNormalRedirectsChecks, ...tedditNormalCustomRedirects]; - } else if (protocol == 'tor') { - libredditInstancesList = [...libredditTorRedirectsChecks, ...libredditTorCustomRedirects]; - tedditInstancesList = [...tedditTorRedirectsChecks, ...tedditTorCustomRedirects]; - } + let libredditInstancesList; + let tedditInstancesList; + if (r.redditProtocol == 'normal') { + libredditInstancesList = [...r.libredditNormalRedirectsChecks, ...r.libredditNormalCustomRedirects]; + tedditInstancesList = [...r.tedditNormalRedirectsChecks, ...r.tedditNormalCustomRedirects]; + } else if (r.redditProtocol == 'tor') { + libredditInstancesList = [...r.libredditTorRedirectsChecks, ...r.libredditTorCustomRedirects]; + tedditInstancesList = [...r.tedditTorRedirectsChecks, ...r.tedditTorCustomRedirects]; + } - if (url.host === "i.redd.it") { - if (frontend == 'teddit') { - if (tedditInstancesList.length === 0) return null; - let tedditRandomInstance = commonHelper.getRandomInstance(tedditInstancesList); - return `${tedditRandomInstance}/pics/w:null_${url.pathname.substring(1)}${url.reddit}`; - } - if (frontend == 'libreddit') { - if (libredditInstancesList.length === 0) return null; - let libredditRandomInstance = commonHelper.getRandomInstance(libredditInstancesList); - return `${libredditRandomInstance}/img${url.pathname}${url.reddit}`; - } - } - else if (url.host === "redd.it") { - if (frontend == 'libreddit' && !url.pathname.match(/^\/+[^\/]+\/+[^\/]/)) { - if (libredditInstancesList.length === 0) return null; - let libredditRandomInstance = commonHelper.getRandomInstance(libredditInstancesList); - // https://redd.it/foo => https://libredd.it/comments/foo - return `${libredditRandomInstance}/comments${url.pathname}${url.reddit}`; - } - if (frontend == 'teddit' && !url.pathname.match(/^\/+[^\/]+\/+[^\/]/)) { - if (tedditInstancesList.length === 0) return null; - let tedditRandomInstance = commonHelper.getRandomInstance(tedditInstancesList); - // https://redd.it/foo => https://teddit.net/comments/foo - return `${tedditRandomInstance}/comments${url.pathname}${url.reddit}`; - } - } - else if (url.host === 'preview.redd.it') { - if (frontend == 'teddit') { - return null; - } - if (frontend == 'libreddit') { - if (libredditInstancesList.length === 0) return null; - let libredditRandomInstance = commonHelper.getRandomInstance(libredditInstancesList); - return `${libredditRandomInstance}/preview/pre${url.pathname}${url.reddit}`; - } - } + if (url.host === "i.redd.it") { + if (r.redditFrontend == 'teddit') { + if (tedditInstancesList.length === 0) { resolve(); return; } + let tedditRandomInstance = utils.getRandomInstance(tedditInstancesList); + resolve(`${tedditRandomInstance}/pics/w:null_${url.pathname.substring(1)}${url.reddit}`); return; + } + if (r.redditFrontend == 'libreddit') { + if (libredditInstancesList.length === 0) { resolve(); return; } + let libredditRandomInstance = utils.getRandomInstance(libredditInstancesList); + resolve(`${libredditRandomInstance}/img${url.pathname}${url.reddit}`); return; + } + } + else if (url.host === "redd.it") { + if (r.redditFrontend == 'libreddit' && !url.pathname.match(/^\/+[^\/]+\/+[^\/]/)) { + if (libredditInstancesList.length === 0) { resolve(); return; } + let libredditRandomInstance = utils.getRandomInstance(libredditInstancesList); + // https://redd.it/foo => https://libredd.it/comments/foo + resolve(`${libredditRandomInstance}/comments${url.pathname}${url.reddit}`); return; + } + if (r.redditFrontend == 'teddit' && !url.pathname.match(/^\/+[^\/]+\/+[^\/]/)) { + if (tedditInstancesList.length === 0) { resolve(); return; } + let tedditRandomInstance = utils.getRandomInstance(tedditInstancesList); + // https://redd.it/foo => https://teddit.net/comments/foo + resolve(`${tedditRandomInstance}/comments${url.pathname}${url.reddit}`); return; + } + } + else if (url.host === 'preview.redd.it') { + if (r.redditFrontend == 'teddit') { + { resolve(); return; } + } + if (r.redditFrontend == 'libreddit') { + if (libredditInstancesList.length === 0) { resolve(); return; } + let libredditRandomInstance = utils.getRandomInstance(libredditInstancesList); + resolve(`${libredditRandomInstance}/preview/pre${url.pathname}${url.reddit}`); return; + } + } - if (frontend == 'libreddit') { - if (libredditInstancesList.length === 0) return null; - let libredditRandomInstance = commonHelper.getRandomInstance(libredditInstancesList); - return `${libredditRandomInstance}${url.pathname}${url.reddit}`; - } - if (frontend == 'teddit') { - if (tedditInstancesList.length === 0) return null; - let tedditRandomInstance = commonHelper.getRandomInstance(tedditInstancesList); - return `${tedditRandomInstance}${url.pathname}${url.reddit}`; - } + let randomInstance; + if (r.redditFrontend == 'libreddit') { + if (libredditInstancesList.length === 0) { resolve(); return; } + randomInstance = utils.getRandomInstance(libredditInstancesList); + } + if (r.redditFrontend == 'teddit') { + if (tedditInstancesList.length === 0) { resolve(); return; } + randomInstance = utils.getRandomInstance(tedditInstancesList); + } + resolve(`${randomInstance}${url.pathname}${url.search}`); + }) + }) } function reverse(url) { - let protocolHost = commonHelper.protocolHost(url); + let protocolHost = utils.protocolHost(url); if ( ![...redirects.nitter.normal, ...redirects.nitter.tor, @@ -323,7 +339,7 @@ function reverse(url) { return `https://reddit.com${url.pathname}${url.reddit}`; } -async function switchInstance(url) { +function switchInstance(url) { return new Promise(resolve => { browser.storage.local.get( [ @@ -342,18 +358,8 @@ async function switchInstance(url) { "tedditTorCustomRedirects", ], r => { - let protocolHost = commonHelper.protocolHost(url); - let isTeddit = [ - ...r.redditRedirects.teddit.normal, - ...r.redditRedirects.teddit.tor - ].includes(protocolHost); - - let isLibreddit = [ - ...r.redditRedirects.libreddit.normal, - ...r.redditRedirects.libreddit.tor - ].includes(protocolHost); - - let redditList = [ + let protocolHost = utils.protocolHost(url); + if (![ ...r.redditRedirects.libreddit.normal, ...r.redditRedirects.libreddit.tor, @@ -365,33 +371,39 @@ async function switchInstance(url) { ...r.tedditNormalCustomRedirects, ...r.tedditTorCustomRedirects, - ] - if (!redditList.includes(protocolHost)) return null; + ].includes(protocolHost)) { resolve(); return; } + let instancesList; if (r.redditFrontend == 'libreddit') { if (r.redditProtocol == 'normal') instancesList = [...r.libredditNormalRedirectsChecks, ...r.libredditNormalCustomRedirects]; else if (r.redditProtocol == 'tor') instancesList = [...r.libredditTorRedirectsChecks, ...r.libredditTorCustomRedirects]; - if (isTeddit) url.pathname = url.pathname.replace("/pics/w:null_", "/img/"); + if ([ + ...r.redditRedirects.teddit.normal, + ...r.redditRedirects.teddit.tor + ].includes(protocolHost)) url.pathname = url.pathname.replace("/pics/w:null_", "/img/"); } else if (r.redditFrontend == 'teddit') { if (r.redditProtocol == 'normal') instancesList = [...r.tedditNormalRedirectsChecks, ...r.tedditNormalCustomRedirects]; else if (r.redditProtocol == 'tor') instancesList = [...r.tedditTorRedirectsChecks, ...r.tedditTorCustomRedirects]; - if (isLibreddit) url.pathname = url.pathname.replace("/img/", "/pics/w:null_"); + if ([ + ...r.redditRedirects.libreddit.normal, + ...r.redditRedirects.libreddit.tor + ].includes(protocolHost) + ) url.pathname = url.pathname.replace("/img/", "/pics/w:null_"); } let index = instancesList.indexOf(protocolHost); if (index > -1) instancesList.splice(index, 1); - if (instancesList.length === 0) return; - - let randomInstance = commonHelper.getRandomInstance(instancesList); + if (instancesList.length === 0) { resolve(); return; } + let randomInstance = utils.getRandomInstance(instancesList); resolve(`${randomInstance}${url.pathname}${url.reddit}`) } ) }) } -async function initDefaults() { +function initDefaults() { fetch('/instances/data.json').then(response => response.text()).then(async data => { let dataJson = JSON.parse(data); redirects.teddit = dataJson.teddit; @@ -430,48 +442,6 @@ async function initDefaults() { }); } -async function init() { - return new Promise(resolve => { - browser.storage.local.get( - [ - "disableReddit", - "redditFrontend", - "redditRedirects", - "redditProtocol", - - "libredditNormalRedirectsChecks", - "libredditNormalCustomRedirects", - "libredditTorRedirectsChecks", - "libredditTorCustomRedirects", - - "tedditNormalRedirectsChecks", - "tedditNormalCustomRedirects", - "tedditTorRedirectsChecks", - "tedditTorCustomRedirects", - ], r => { - disableReddit = r.disableReddit; - protocol = r.redditProtocol; - frontend = r.redditFrontend; - redirects = r.redditRedirects; - - libredditNormalRedirectsChecks = r.libredditNormalRedirectsChecks; - libredditNormalCustomRedirects = r.libredditNormalCustomRedirects; - - libredditTorRedirectsChecks = r.libredditTorRedirectsChecks; - libredditTorCustomRedirects = r.libredditTorCustomRedirects; - - tedditNormalRedirectsChecks = r.tedditNormalRedirectsChecks; - tedditNormalCustomRedirects = r.tedditNormalCustomRedirects; - - tedditTorRedirectsChecks = r.tedditTorRedirectsChecks; - tedditTorCustomRedirects = r.tedditTorCustomRedirects; - - resolve(); - } - ); - }); -} - export default { getRedirects, setTedditRedirects, @@ -483,7 +453,6 @@ export default { setTedditCookies, redirect, - init, initDefaults, switchInstance, }; diff --git a/src/assets/javascripts/helpers/search.js b/src/assets/javascripts/helpers/search.js index 4e8441c..37bcdf7 100644 --- a/src/assets/javascripts/helpers/search.js +++ b/src/assets/javascripts/helpers/search.js @@ -1,6 +1,6 @@ window.browser = window.browser || window.chrome; -import commonHelper from './common.js' +import utils from './utils.js' const targets = [ /^https?:\/{2}(www\.|search\.|)google(\.[a-z]{2,3}){1,2}(\/search(\?.*|$)|\/$)/, @@ -139,7 +139,7 @@ function initSearxCookies(from) { "searxI2pCustomRedirects", ], r => { - let protocolHost = commonHelper.protocolHost(from); + let protocolHost = utils.protocolHost(from); if (![ ...r.searxNormalRedirectsChecks, ...r.searxNormalCustomRedirects, @@ -155,23 +155,23 @@ function initSearxCookies(from) { else if (protocol == 'tor') checkedInstances = [...r.searxTorRedirectsChecks, ...r.searxTorCustomRedirects]; else if (protocol == 'i2p') checkedInstances = [...r.searxI2pRedirectsChecks, ...r.searxI2pCustomRedirects]; for (const to of checkedInstances) { - commonHelper.copyCookie('searx', from, to, 'advanced_search'); - commonHelper.copyCookie('searx', from, to, 'autocomplete'); - commonHelper.copyCookie('searx', from, to, 'categories'); - commonHelper.copyCookie('searx', from, to, 'disabled_engines'); - commonHelper.copyCookie('searx', from, to, 'disabled_plugins'); - commonHelper.copyCookie('searx', from, to, 'doi_resolver'); - commonHelper.copyCookie('searx', from, to, 'enabled_engines'); - commonHelper.copyCookie('searx', from, to, 'enabled_plugins'); - commonHelper.copyCookie('searx', from, to, 'image_proxy'); - commonHelper.copyCookie('searx', from, to, 'language'); - commonHelper.copyCookie('searx', from, to, 'locale'); - commonHelper.copyCookie('searx', from, to, 'method'); - commonHelper.copyCookie('searx', from, to, 'oscar-style'); - commonHelper.copyCookie('searx', from, to, 'results_on_new_tab'); - commonHelper.copyCookie('searx', from, to, 'safesearch'); - commonHelper.copyCookie('searx', from, to, 'theme'); - commonHelper.copyCookie('searx', from, to, 'tokens'); + utils.copyCookie('searx', from, to, 'advanced_search'); + utils.copyCookie('searx', from, to, 'autocomplete'); + utils.copyCookie('searx', from, to, 'categories'); + utils.copyCookie('searx', from, to, 'disabled_engines'); + utils.copyCookie('searx', from, to, 'disabled_plugins'); + utils.copyCookie('searx', from, to, 'doi_resolver'); + utils.copyCookie('searx', from, to, 'enabled_engines'); + utils.copyCookie('searx', from, to, 'enabled_plugins'); + utils.copyCookie('searx', from, to, 'image_proxy'); + utils.copyCookie('searx', from, to, 'language'); + utils.copyCookie('searx', from, to, 'locale'); + utils.copyCookie('searx', from, to, 'method'); + utils.copyCookie('searx', from, to, 'oscar-style'); + utils.copyCookie('searx', from, to, 'results_on_new_tab'); + utils.copyCookie('searx', from, to, 'safesearch'); + utils.copyCookie('searx', from, to, 'theme'); + utils.copyCookie('searx', from, to, 'tokens'); } resolve(true); } @@ -196,23 +196,23 @@ function setSearxCookies() { if (r.searchProtocol == 'normal') checkedInstances = [...r.searxNormalRedirectsChecks, ...r.searxNormalCustomRedirects] else if (r.searchProtocol == 'tor') checkedInstances = [...r.searxTorRedirectsChecks, ...r.searxTorCustomRedirects] for (const to of checkedInstances) { - commonHelper.getCookiesFromStorage('searx', to, 'advanced_search'); - commonHelper.getCookiesFromStorage('searx', to, 'autocomplete'); - commonHelper.getCookiesFromStorage('searx', to, 'categories'); - commonHelper.getCookiesFromStorage('searx', to, 'disabled_engines'); - commonHelper.getCookiesFromStorage('searx', to, 'disabled_plugins'); - commonHelper.getCookiesFromStorage('searx', to, 'doi_resolver'); - commonHelper.getCookiesFromStorage('searx', to, 'enabled_engines'); - commonHelper.getCookiesFromStorage('searx', to, 'enabled_plugins'); - commonHelper.getCookiesFromStorage('searx', to, 'image_proxy'); - commonHelper.getCookiesFromStorage('searx', to, 'language'); - commonHelper.getCookiesFromStorage('searx', to, 'locale'); - commonHelper.getCookiesFromStorage('searx', to, 'method'); - commonHelper.getCookiesFromStorage('searx', to, 'oscar-style'); - commonHelper.getCookiesFromStorage('searx', to, 'results_on_new_tab'); - commonHelper.getCookiesFromStorage('searx', to, 'safesearch'); - commonHelper.getCookiesFromStorage('searx', to, 'theme'); - commonHelper.getCookiesFromStorage('searx', to, 'tokens'); + utils.getCookiesFromStorage('searx', to, 'advanced_search'); + utils.getCookiesFromStorage('searx', to, 'autocomplete'); + utils.getCookiesFromStorage('searx', to, 'categories'); + utils.getCookiesFromStorage('searx', to, 'disabled_engines'); + utils.getCookiesFromStorage('searx', to, 'disabled_plugins'); + utils.getCookiesFromStorage('searx', to, 'doi_resolver'); + utils.getCookiesFromStorage('searx', to, 'enabled_engines'); + utils.getCookiesFromStorage('searx', to, 'enabled_plugins'); + utils.getCookiesFromStorage('searx', to, 'image_proxy'); + utils.getCookiesFromStorage('searx', to, 'language'); + utils.getCookiesFromStorage('searx', to, 'locale'); + utils.getCookiesFromStorage('searx', to, 'method'); + utils.getCookiesFromStorage('searx', to, 'oscar-style'); + utils.getCookiesFromStorage('searx', to, 'results_on_new_tab'); + utils.getCookiesFromStorage('searx', to, 'safesearch'); + utils.getCookiesFromStorage('searx', to, 'theme'); + utils.getCookiesFromStorage('searx', to, 'tokens'); } } ) @@ -231,7 +231,7 @@ function initSearxngCookies(from) { "searxngI2pCustomRedirects", ], r => { - let protocolHost = commonHelper.protocolHost(from); + let protocolHost = utils.protocolHost(from); if (![ ...r.searxngNormalRedirectsChecks, ...r.searxngNormalCustomRedirects, @@ -246,25 +246,25 @@ function initSearxngCookies(from) { else if (r.searchProtocol == 'tor') checkedInstances = [...r.searxngTorRedirectsChecks, ...r.searxngTorCustomRedirects]; else if (r.searchProtocol == 'i2p') checkedInstances = [...r.searxngI2pRedirectsChecks, ...r.searxngI2pCustomRedirects]; for (const to of checkedInstances) { - commonHelper.copyCookie('searxng', from, to, 'autocomplete'); - commonHelper.copyCookie('searxng', from, to, 'categories'); - commonHelper.copyCookie('searxng', from, to, 'disabled_engines'); - commonHelper.copyCookie('searxng', from, to, 'disabled_plugins'); - commonHelper.copyCookie('searxng', from, to, 'doi_resolver'); - commonHelper.copyCookie('searxng', from, to, 'enabled_plugins'); - commonHelper.copyCookie('searxng', from, to, 'enabled_engines'); - commonHelper.copyCookie('searxng', from, to, 'image_proxy'); - commonHelper.copyCookie('searxng', from, to, 'infinite_scroll'); - commonHelper.copyCookie('searxng', from, to, 'language'); - commonHelper.copyCookie('searxng', from, to, 'locale'); - commonHelper.copyCookie('searxng', from, to, 'maintab'); - commonHelper.copyCookie('searxng', from, to, 'method'); - commonHelper.copyCookie('searxng', from, to, 'query_in_title'); - commonHelper.copyCookie('searxng', from, to, 'results_on_new_tab'); - commonHelper.copyCookie('searxng', from, to, 'safesearch'); - commonHelper.copyCookie('searxng', from, to, 'simple_style'); - commonHelper.copyCookie('searxng', from, to, 'theme'); - commonHelper.copyCookie('searxng', from, to, 'tokens'); + utils.copyCookie('searxng', from, to, 'autocomplete'); + utils.copyCookie('searxng', from, to, 'categories'); + utils.copyCookie('searxng', from, to, 'disabled_engines'); + utils.copyCookie('searxng', from, to, 'disabled_plugins'); + utils.copyCookie('searxng', from, to, 'doi_resolver'); + utils.copyCookie('searxng', from, to, 'enabled_plugins'); + utils.copyCookie('searxng', from, to, 'enabled_engines'); + utils.copyCookie('searxng', from, to, 'image_proxy'); + utils.copyCookie('searxng', from, to, 'infinite_scroll'); + utils.copyCookie('searxng', from, to, 'language'); + utils.copyCookie('searxng', from, to, 'locale'); + utils.copyCookie('searxng', from, to, 'maintab'); + utils.copyCookie('searxng', from, to, 'method'); + utils.copyCookie('searxng', from, to, 'query_in_title'); + utils.copyCookie('searxng', from, to, 'results_on_new_tab'); + utils.copyCookie('searxng', from, to, 'safesearch'); + utils.copyCookie('searxng', from, to, 'simple_style'); + utils.copyCookie('searxng', from, to, 'theme'); + utils.copyCookie('searxng', from, to, 'tokens'); } resolve(true); } @@ -289,140 +289,213 @@ function setSearxngCookies() { if (r.searchProtocol == 'normal') checkedInstances = [...r.searxngNormalRedirectsChecks, ...r.searxngNormalCustomRedirects] else if (r.searchProtocol == 'tor') checkedInstances = [...r.searxngTorRedirectsChecks, ...r.searxngTorCustomRedirects] for (const to of checkedInstances) { - commonHelper.getCookiesFromStorage('searxng', to, 'autocomplete'); - commonHelper.getCookiesFromStorage('searxng', to, 'categories'); - commonHelper.getCookiesFromStorage('searxng', to, 'disabled_engines'); - commonHelper.getCookiesFromStorage('searxng', to, 'disabled_plugins'); - commonHelper.getCookiesFromStorage('searxng', to, 'doi_resolver'); - commonHelper.getCookiesFromStorage('searxng', to, 'enabled_plugins'); - commonHelper.getCookiesFromStorage('searxng', to, 'enabled_engines'); - commonHelper.getCookiesFromStorage('searxng', to, 'image_proxy'); - commonHelper.getCookiesFromStorage('searxng', to, 'infinite_scroll'); - commonHelper.getCookiesFromStorage('searxng', to, 'language'); - commonHelper.getCookiesFromStorage('searxng', to, 'locale'); - commonHelper.getCookiesFromStorage('searxng', to, 'maintab'); - commonHelper.getCookiesFromStorage('searxng', to, 'method'); - commonHelper.getCookiesFromStorage('searxng', to, 'query_in_title'); - commonHelper.getCookiesFromStorage('searxng', to, 'results_on_new_tab'); - commonHelper.getCookiesFromStorage('searxng', to, 'safesearch'); - commonHelper.getCookiesFromStorage('searxng', to, 'simple_style'); - commonHelper.getCookiesFromStorage('searxng', to, 'theme'); - commonHelper.getCookiesFromStorage('searxng', to, 'tokens'); + utils.getCookiesFromStorage('searxng', to, 'autocomplete'); + utils.getCookiesFromStorage('searxng', to, 'categories'); + utils.getCookiesFromStorage('searxng', to, 'disabled_engines'); + utils.getCookiesFromStorage('searxng', to, 'disabled_plugins'); + utils.getCookiesFromStorage('searxng', to, 'doi_resolver'); + utils.getCookiesFromStorage('searxng', to, 'enabled_plugins'); + utils.getCookiesFromStorage('searxng', to, 'enabled_engines'); + utils.getCookiesFromStorage('searxng', to, 'image_proxy'); + utils.getCookiesFromStorage('searxng', to, 'infinite_scroll'); + utils.getCookiesFromStorage('searxng', to, 'language'); + utils.getCookiesFromStorage('searxng', to, 'locale'); + utils.getCookiesFromStorage('searxng', to, 'maintab'); + utils.getCookiesFromStorage('searxng', to, 'method'); + utils.getCookiesFromStorage('searxng', to, 'query_in_title'); + utils.getCookiesFromStorage('searxng', to, 'results_on_new_tab'); + utils.getCookiesFromStorage('searxng', to, 'safesearch'); + utils.getCookiesFromStorage('searxng', to, 'simple_style'); + utils.getCookiesFromStorage('searxng', to, 'theme'); + utils.getCookiesFromStorage('searxng', to, 'tokens'); } } ) } function redirect(url) { - if (disable) return; - if (!targets.some(rx => rx.test(url.href))) return; - if (url.searchParams.has('tbm')) return; - if (url.hostname.includes('google') && !url.searchParams.has('q') && url.pathname != '/') return; - let randomInstance; - let path; - if (frontend == 'searx') { - let instancesList; - if (protocol == 'normal') instancesList = [...searxNormalRedirectsChecks, ...searxNormalCustomRedirects]; - else if (protocol == 'tor') instancesList = [...searxTorRedirectsChecks, ...searxTorCustomRedirects]; - else if (protocol == 'i2p') instancesList = [...searxI2pRedirectsChecks, ...searxI2pCustomRedirects]; - if (instancesList.length === 0) return null; - randomInstance = commonHelper.getRandomInstance(instancesList) - path = "/"; - } - else if (frontend == 'searxng') { - let instancesList; - if (protocol == 'normal') instancesList = [...searxngNormalRedirectsChecks, ...searxngNormalCustomRedirects]; - else if (protocol == 'tor') instancesList = [...searxngTorRedirectsChecks, ...searxngTorCustomRedirects]; - else if (protocol == 'i2p') instancesList = [...searxngI2pRedirectsChecks, ...searxngI2pCustomRedirects]; - if (instancesList.length === 0) return null; - randomInstance = commonHelper.getRandomInstance(instancesList) - path = "/"; - } - else if (frontend == 'whoogle') { - let instancesList; - if (protocol == 'normal') instancesList = [...whoogleNormalRedirectsChecks, ...whoogleNormalCustomRedirects]; - if (protocol == 'tor') instancesList = [...whoogleTorRedirectsChecks, ...whoogleTorCustomRedirects]; - if (protocol == 'i2p') instancesList = [...whoogleI2pRedirectsChecks, ...whoogleI2pCustomRedirects]; - if (instancesList.length === 0) return null; - randomInstance = commonHelper.getRandomInstance(instancesList) - path = "/search"; - } + return new Promise(resolve => { + browser.storage.local.get( + [ + "disableSearch", + "searchFrontend", + "searchRedirects", + "searchProtocol", - if ( - ((url.hostname.includes('google') || url.hostname.includes('bing')) && !url.searchParams.has('q')) || - (url.hostname.includes('yandex') && !url.searchParams.has('text')) - ) path = '/'; + "whoogleNormalRedirectsChecks", + "whoogleNormalCustomRedirects", - let searchQuery = ""; + "whoogleTorRedirectsChecks", + "whoogleTorCustomRedirects", - if ( - ( - url.hostname.includes('google') || - url.hostname.includes('bing') || - url.hostname.includes('libredirect.invalid') - ) && - url.searchParams.has('q') - ) searchQuery = `?q=${url.searchParams.get('q')}`; - if (url.hostname.includes('yandex') && url.searchParams.has('text')) searchQuery = `?q=${url.searchParams.get('text')}`; + "whoogleI2pRedirectsChecks", + "whoogleI2pCustomRedirects", - return `${randomInstance}${path}${searchQuery}`; + "searxNormalRedirectsChecks", + "searxNormalCustomRedirects", + + "searxTorRedirectsChecks", + "searxTorCustomRedirects", + + "searxI2pRedirectsChecks", + "searxI2pCustomRedirects", + + "searxngNormalRedirectsChecks", + "searxngNormalCustomRedirects", + + "searxngTorRedirectsChecks", + "searxngTorCustomRedirects", + + "searxngI2pRedirectsChecks", + "searxngI2pCustomRedirects", + ], + r => { + if (disable) { resolve(); return; } + if (!targets.some(rx => rx.test(url.href))) { resolve(); return; } + if (url.searchParams.has('tbm')) { resolve(); return; } + if (url.hostname.includes('google') && !url.searchParams.has('q') && url.pathname != '/') { resolve(); return; } + let randomInstance; + let path; + if (r.searchFrontend == 'searx') { + let instancesList; + if (r.searchProtocol == 'normal') instancesList = [...r.searxNormalRedirectsChecks, ...r.searxNormalCustomRedirects]; + else if (r.searchProtocol == 'tor') instancesList = [...r.searxTorRedirectsChecks, ...r.searxTorCustomRedirects]; + else if (r.searchProtocol == 'i2p') instancesList = [...r.searxI2pRedirectsChecks, ...r.searxI2pCustomRedirects]; + if (instancesList.length === 0) { resolve(); return; } + randomInstance = utils.getRandomInstance(instancesList) + path = "/"; + } + else if (r.searchFrontend == 'searxng') { + let instancesList; + if (r.searchProtocol == 'normal') instancesList = [...r.searxngNormalRedirectsChecks, ...r.searxngNormalCustomRedirects]; + else if (r.searchProtocol == 'tor') instancesList = [...r.searxngTorRedirectsChecks, ...r.searxngTorCustomRedirects]; + else if (r.searchProtocol == 'i2p') instancesList = [...r.searxngI2pRedirectsChecks, ...r.searxngI2pCustomRedirects]; + if (instancesList.length === 0) { resolve(); return; } + randomInstance = utils.getRandomInstance(instancesList) + path = "/"; + } + else if (r.searchFrontend == 'whoogle') { + let instancesList; + if (r.searchProtocol == 'normal') instancesList = [...r.whoogleNormalRedirectsChecks, ...r.whoogleNormalCustomRedirects]; + if (r.searchProtocol == 'tor') instancesList = [...r.whoogleTorRedirectsChecks, ...r.whoogleTorCustomRedirects]; + if (r.searchProtocol == 'i2p') instancesList = [...r.whoogleI2pRedirectsChecks, ...r.whoogleI2pCustomRedirects]; + if (instancesList.length === 0) { resolve(); return; } + randomInstance = utils.getRandomInstance(instancesList) + path = "/search"; + } + + if ( + ((url.hostname.includes('google') || url.hostname.includes('bing')) && !url.searchParams.has('q')) || + (url.hostname.includes('yandex') && !url.searchParams.has('text')) + ) path = '/'; + + let searchQuery = ""; + if ( + ( + url.hostname.includes('google') || + url.hostname.includes('bing') || + url.hostname.includes('libredirect.invalid') + ) && + url.searchParams.has('q') + ) searchQuery = `?q=${url.searchParams.get('q')}`; + if (url.hostname.includes('yandex') && url.searchParams.has('text')) searchQuery = `?q=${url.searchParams.get('text')}`; + + resolve(`${randomInstance}${path}${searchQuery}`); + }) + }) } -function switchInstance(url) { - let protocolHost = commonHelper.protocolHost(url); +async function switchInstance(url) { + return new Promise(resolve => { + browser.storage.local.get( + [ + "searchFrontend", + "searchRedirects", + "searchProtocol", - let searchList = [ - ...redirects.searx.normal, - ...redirects.searx.tor, - ...redirects.searx.i2p, + "whoogleNormalRedirectsChecks", + "whoogleNormalCustomRedirects", - ...searxNormalCustomRedirects, - ...searxTorCustomRedirects, - ...searxI2pCustomRedirects, + "whoogleTorRedirectsChecks", + "whoogleTorCustomRedirects", - ...redirects.searx.normal, - ...redirects.searxng.tor, - ...redirects.searxng.i2p, + "whoogleI2pRedirectsChecks", + "whoogleI2pCustomRedirects", - ...searxngNormalCustomRedirects, - ...searxngTorCustomRedirects, - ...searxngI2pCustomRedirects, + "searxNormalRedirectsChecks", + "searxNormalCustomRedirects", - ...redirects.whoogle.normal, - ...redirects.whoogle.tor, - ...redirects.whoogle.i2p, + "searxTorRedirectsChecks", + "searxTorCustomRedirects", - ...whoogleNormalCustomRedirects, - ...whoogleTorCustomRedirects, - ...whoogleI2pCustomRedirects, - ] + "searxI2pRedirectsChecks", + "searxI2pCustomRedirects", - if (!searchList.includes(protocolHost)) return null; + "searxngNormalRedirectsChecks", + "searxngNormalCustomRedirects", - let instancesList; - if (frontend == 'searx') { - if (protocol == 'normal') instancesList = [...searxNormalRedirectsChecks, ...searxNormalCustomRedirects]; - else if (protocol == 'tor') instancesList = [...searxTorRedirectsChecks, ...searxTorCustomRedirects]; - else if (protocol == 'i2p') instancesList = [...searxI2pRedirectsChecks, ...searxI2pCustomRedirects]; - } - else if (frontend == 'searxng') { - if (protocol == 'normal') instancesList = [...searxngNormalRedirectsChecks, ...searxngNormalCustomRedirects]; - else if (protocol == 'tor') instancesList = [...searxngTorRedirectsChecks, ...searxngTorCustomRedirects]; - else if (protocol == 'i2p') instancesList = [...searxngI2pRedirectsChecks, ...searxngI2pCustomRedirects]; - } - else if (frontend == 'whoogle') { - if (protocol == 'normal') instancesList = [...whoogleNormalRedirectsChecks, ...whoogleNormalCustomRedirects]; - else if (protocol == 'tor') instancesList = [...whoogleTorRedirectsChecks, ...whoogleTorCustomRedirects]; - else if (protocol == 'i2p') instancesList = [...whoogleI2pRedirectsChecks, ...whoogleI2pCustomRedirects]; - } + "searxngTorRedirectsChecks", + "searxngTorCustomRedirects", - let index = instancesList.indexOf(protocolHost); - if (index > -1) instancesList.splice(index, 1); + "searxngI2pRedirectsChecks", + "searxngI2pCustomRedirects", + ], + r => { + let protocolHost = utils.protocolHost(url); + if (![ + ...r.searchRedirects.searx.normal, + ...r.searchRedirects.searx.tor, + ...r.searchRedirects.searx.i2p, - if (instancesList.length === 0) return null; + ...r.searchRedirects.searxng.normal, + ...r.searchRedirects.searxng.tor, + ...r.searchRedirects.searxng.i2p, - let randomInstance = commonHelper.getRandomInstance(instancesList); - return `${randomInstance}${url.pathname}${url.search}`; + ...r.searchRedirects.whoogle.normal, + ...r.searchRedirects.whoogle.tor, + ...r.searchRedirects.whoogle.i2p, + + ...r.searxNormalCustomRedirects, + ...r.searxTorCustomRedirects, + ...r.searxI2pCustomRedirects, + + ...r.searxngNormalCustomRedirects, + ...r.searxngTorCustomRedirects, + ...r.searxngI2pCustomRedirects, + + ...r.whoogleNormalCustomRedirects, + ...r.whoogleTorCustomRedirects, + ...r.whoogleI2pCustomRedirects, + ].includes(protocolHost)) { + + resolve(); + } + + let instancesList; + if (r.searchProtocol == 'normal') { + if (r.searchFrontend == 'searx') instancesList = [...r.searxNormalRedirectsChecks, ...r.searxNormalCustomRedirects]; + else if (r.searchFrontend == 'searxng') instancesList = [...r.searxngNormalRedirectsChecks, ...r.searxngNormalCustomRedirects]; + else if (r.searchFrontend == 'whoogle') instancesList = [...r.whoogleNormalRedirectsChecks, ...r.whoogleNormalCustomRedirects]; + } + else if (r.searchProtocol == 'tor') { + if (r.searchFrontend == 'searx') instancesList = [...r.searxTorRedirectsChecks, ...r.searxTorCustomRedirects]; + else if (r.searchFrontend == 'searxng') instancesList = [...r.searxngTorRedirectsChecks, ...r.searxngTorCustomRedirects]; + else if (r.searchFrontend == 'whoogle') instancesList = [...r.whoogleTorRedirectsChecks, ...r.whoogleTorCustomRedirects]; + } + else if (r.searchProtocol == 'i2p') { + if (r.searchFrontend == 'searx') instancesList = [...r.searxI2pRedirectsChecks, ...r.searxI2pCustomRedirects]; + else if (r.searchFrontend == 'searxng') instancesList = [...r.searxngI2pRedirectsChecks, ...r.searxngI2pCustomRedirects]; + else if (r.searchFrontend == 'whoogle') instancesList = [...r.whoogleI2pRedirectsChecks, ...r.whoogleI2pCustomRedirects]; + } + + let index = instancesList.indexOf(protocolHost); + if (index > -1) instancesList.splice(index, 1); + if (instancesList.length === 0) resolve(); + + let randomInstance = utils.getRandomInstance(instancesList); + resolve(`${randomInstance}${url.pathname}${url.search}`); + }) + }) } async function initDefaults() { @@ -486,77 +559,6 @@ async function initDefaults() { }) } -async function init() { - browser.storage.local.get( - [ - "disableSearch", - "searchFrontend", - "searchRedirects", - "searchProtocol", - - "whoogleNormalRedirectsChecks", - "whoogleNormalCustomRedirects", - - "whoogleTorRedirectsChecks", - "whoogleTorCustomRedirects", - - "whoogleI2pRedirectsChecks", - "whoogleI2pCustomRedirects", - - "searxNormalRedirectsChecks", - "searxNormalCustomRedirects", - - "searxTorRedirectsChecks", - "searxTorCustomRedirects", - - "searxI2pRedirectsChecks", - "searxI2pCustomRedirects", - - "searxngNormalRedirectsChecks", - "searxngNormalCustomRedirects", - - "searxngTorRedirectsChecks", - "searxngTorCustomRedirects", - - "searxngI2pRedirectsChecks", - "searxngI2pCustomRedirects", - ], - r => { - disable = r.disableSearch; - protocol = r.searchProtocol; - frontend = r.searchFrontend; - redirects = r.searchRedirects; - - whoogleNormalRedirectsChecks = r.whoogleNormalRedirectsChecks; - whoogleNormalCustomRedirects = r.whoogleNormalCustomRedirects; - - whoogleTorRedirectsChecks = r.whoogleTorRedirectsChecks; - whoogleTorCustomRedirects = r.whoogleTorCustomRedirects; - - whoogleI2pRedirectsChecks = r.whoogleI2pRedirectsChecks; - whoogleI2pCustomRedirects = r.whoogleI2pCustomRedirects; - - searxNormalRedirectsChecks = r.searxNormalRedirectsChecks; - searxNormalCustomRedirects = r.searxNormalCustomRedirects; - - searxTorRedirectsChecks = r.searxTorRedirectsChecks; - searxTorCustomRedirects = r.searxTorCustomRedirects; - - searxI2pRedirectsChecks = r.searxI2pRedirectsChecks; - searxI2pCustomRedirects = r.searxI2pCustomRedirects; - - searxngNormalRedirectsChecks = r.searxngNormalRedirectsChecks; - searxngNormalCustomRedirects = r.searxngNormalCustomRedirects; - - searxngTorRedirectsChecks = r.searxngTorRedirectsChecks; - searxngTorCustomRedirects = r.searxngTorCustomRedirects; - - searxngI2pRedirectsChecks = r.searxngI2pRedirectsChecks; - searxngI2pCustomRedirects = r.searxngI2pCustomRedirects; - } - ); -} - export default { setSearxRedirects, setSearxngRedirects, @@ -570,6 +572,5 @@ export default { redirect, initDefaults, - init, switchInstance, }; diff --git a/src/assets/javascripts/helpers/sendTargets.js b/src/assets/javascripts/helpers/sendTargets.js index 7a5db96..39cbf31 100644 --- a/src/assets/javascripts/helpers/sendTargets.js +++ b/src/assets/javascripts/helpers/sendTargets.js @@ -1,6 +1,6 @@ window.browser = window.browser || window.chrome; -import commonHelper from './common.js' +import utils from './utils.js' const targets = [ /^https?:\/{2}send.invalid\/$/, @@ -43,32 +43,44 @@ let sendTorCustomRedirects = []; let disable; // disableSendTarget let protocol; // sendTargetsProtocol -function switchInstance(url) { - let protocolHost = commonHelper.protocolHost(url); +async function switchInstance(url) { + return new Promise(resolve => { + browser.storage.local.get( + [ + "disableSendTarget", + "sendTargetsRedirects", + "sendTargetsProtocol", - let sendList = [ - ...redirects.send.normal, - ...redirects.send.tor, - ...sendNormalCustomRedirects, - ...sendTorCustomRedirects, - ]; + "sendNormalRedirectsChecks", + "sendNormalCustomRedirects", - if (!sendList.includes(protocolHost)) return; + "sendTorRedirectsChecks", + "sendTorCustomRedirects", + ], + r => { + let protocolHost = utils.protocolHost(url); + if (![ + ...r.sendTargetsRedirects.send.normal, + ...r.sendTargetsRedirects.send.tor, + ...r.sendNormalCustomRedirects, + ...r.sendTorCustomRedirects, + ].includes(protocolHost)) resolve(); - if (url.pathname != '/') return; + if (url.pathname != '/') resolve(); - let instancesList; - if (protocol == 'normal') instancesList = [...sendNormalRedirectsChecks, ...sendNormalCustomRedirects]; - else if (protocol == 'tor') instancesList = [...sendTorRedirectsChecks, ...sendTorCustomRedirects]; + let instancesList; + if (r.sendTargetsProtocol == 'normal') instancesList = [...r.sendNormalRedirectsChecks, ...r.sendNormalCustomRedirects]; + else if (r.sendTargetsProtocol == 'tor') instancesList = [...r.sendTorRedirectsChecks, ...r.sendTorCustomRedirects]; - console.log("instancesList", instancesList); - let index = instancesList.indexOf(protocolHost); - if (index > -1) instancesList.splice(index, 1); + let index = instancesList.indexOf(protocolHost); + if (index > -1) instancesList.splice(index, 1); + if (instancesList.length === 0) resolve(); - if (instancesList.length === 0) return null; - - let randomInstance = commonHelper.getRandomInstance(instancesList); - return `${randomInstance}${url.pathname}${url.search}`; + let randomInstance = utils.getRandomInstance(instancesList); + resolve(`${randomInstance}${url.pathname}${url.search}`); + } + ) + }) } function redirect(url, type, initiator) { @@ -86,7 +98,7 @@ function redirect(url, type, initiator) { if (protocol == 'normal') instancesList = [...sendNormalRedirectsChecks, ...sendNormalCustomRedirects]; if (protocol == 'tor') instancesList = [...sendTorRedirectsChecks, ...sendTorCustomRedirects]; if (instancesList.length === 0) return null; - let randomInstance = commonHelper.getRandomInstance(instancesList); + let randomInstance = utils.getRandomInstance(instancesList); return randomInstance; } @@ -132,7 +144,6 @@ async function init() { "sendTargetsProtocol" ], r => { - disable = r.disableSendTarget; protocol = r.sendTargetsProtocol; redirects = r.sendTargetsRedirects; diff --git a/src/assets/javascripts/helpers/tiktok.js b/src/assets/javascripts/helpers/tiktok.js index 501390d..90d8a8d 100644 --- a/src/assets/javascripts/helpers/tiktok.js +++ b/src/assets/javascripts/helpers/tiktok.js @@ -1,6 +1,6 @@ window.browser = window.browser || window.chrome; -import commonHelper from './common.js' +import utils from './utils.js' const targets = [ /^https?:\/{2}(www\.|)tiktok\.com.*/ @@ -52,7 +52,7 @@ function initProxiTokCookies(from) { "proxiTokTorCustomRedirects", ], r => { - let protocolHost = commonHelper.protocolHost(from); + let protocolHost = utils.protocolHost(from); if (![ ...r.proxiTokNormalRedirectsChecks, ...r.proxiTokNormalCustomRedirects, @@ -64,8 +64,8 @@ function initProxiTokCookies(from) { if (r.tiktokProtocol == 'normal') checkedInstances = [...r.proxiTokNormalRedirectsChecks, ...r.proxiTokNormalCustomRedirects] else if (r.tiktokProtocol == 'tor') checkedInstances = [...r.proxiTokTorRedirectsChecks, ...r.proxiTokTorCustomRedirects] for (const to of checkedInstances) { - commonHelper.copyCookie('proxitok', from, to, 'theme'); - commonHelper.copyCookie('proxitok', from, to, 'api-legacy'); + utils.copyCookie('proxitok', from, to, 'theme'); + utils.copyCookie('proxitok', from, to, 'api-legacy'); } resolve(true); } @@ -86,35 +86,56 @@ function setProxiTokCookies() { r => { if (r.disableTiktok || r.tiktokProtocol === undefined) return; let checkedInstances; - if (r.youtubeProtocol == 'normal') checkedInstances = [...r.proxiTokNormalRedirectsChecks, ...r.proxiTokNormalCustomRedirects] - else if (r.youtubeProtocol == 'tor') checkedInstances = [...r.proxiTokTorRedirectsChecks, ...r.proxiTokTorCustomRedirects] + if (r.tiktokProtocol == 'normal') checkedInstances = [...r.proxiTokNormalRedirectsChecks, ...r.proxiTokNormalCustomRedirects] + else if (r.tiktokProtocol == 'tor') checkedInstances = [...r.proxiTokTorRedirectsChecks, ...r.proxiTokTorCustomRedirects] for (const to of checkedInstances) { - commonHelper.getCookiesFromStorage('proxitok', from, to, 'theme'); - commonHelper.getCookiesFromStorage('proxitok', from, to, 'api-legacy'); + utils.getCookiesFromStorage('proxitok', to, 'theme'); + utils.getCookiesFromStorage('proxitok', to, 'api-legacy'); } } ) } function redirect(url, type, initiator) { - if (disable) return; - if (type != "main_frame") return null; - if (initiator && ( - [...redirects.proxiTok.normal, ...proxiTokNormalCustomRedirects].includes(initiator.origin) || - targets.includes(initiator.host) - ) - ) return; - if (!targets.some(rx => rx.test(url.href))) return; - // https://www.tiktok.com/@keysikaspol/video/7061265241887345946 - // https://www.tiktok.com/@keysikaspol + return new Promise(resolve => { + browser.storage.local.get( + [ + "disableTiktok", + "tiktokProtocol", - let instancesList; - if (protocol == 'normal') instancesList = [...proxiTokNormalRedirectsChecks, ...proxiTokNormalCustomRedirects]; - if (protocol == 'tor') instancesList = [...proxiTokTorRedirectsChecks, ...proxiTokTorCustomRedirects]; - if (instancesList.length === 0) return null; - let randomInstance = commonHelper.getRandomInstance(instancesList); + "tiktokRedirects", - return `${randomInstance}${url.pathname}`; + "proxiTokNormalRedirectsChecks", + "proxiTokNormalCustomRedirects", + + "proxiTokTorRedirectsChecks", + "proxiTokTorCustomRedirects", + ], + r => { + if (r.disableTiktok) { resolve(); return; }; + if (type != "main_frame") { resolve(); return; }; + if (initiator && ( + [ + ...r.tiktokRedirects.proxiTok.normal, + ...r.proxiTokNormalCustomRedirects + ].includes(initiator.origin) || + targets.includes(initiator.host) + ) + ) { resolve(); return; }; + if (!targets.some(rx => rx.test(url.href))) { resolve(); return; }; + // https://www.tiktok.com/@keysikaspol/video/7061265241887345946 + // https://www.tiktok.com/@keysikaspol + + let instancesList; + if (r.tiktokProtocol == 'normal') instancesList = [...r.proxiTokNormalRedirectsChecks, ...r.proxiTokNormalCustomRedirects]; + if (r.tiktokProtocol == 'tor') instancesList = [...r.proxiTokTorRedirectsChecks, ...r.proxiTokTorCustomRedirects]; + if (instancesList.length === 0) { resolve(); return; }; + + let randomInstance = utils.getRandomInstance(instancesList); + resolve(`${randomInstance}${url.pathname}`); + } + ) + }) } async function reverse(url) { @@ -125,7 +146,7 @@ async function reverse(url) { "proxiTokTorCustomRedirects", ], r => { - let protocolHost = commonHelper.protocolHost(url); + let protocolHost = utils.protocolHost(url); if ( ![ ...r.tiktokRedirects.proxiTok.normal, @@ -162,33 +183,6 @@ async function initDefaults() { }) } -async function init() { - browser.storage.local.get( - [ - "disableTiktok", - "tiktokProtocol", - "tiktokRedirects", - - "proxiTokNormalRedirectsChecks", - "proxiTokNormalCustomRedirects", - - "proxiTokTorRedirectsChecks", - "proxiTokTorCustomRedirects", - ], - r => { - disable = r.disableTiktok; - protocol = r.tiktokProtocol; - redirects = r.tiktokRedirects; - - proxiTokNormalRedirectsChecks = r.proxiTokNormalRedirectsChecks; - proxiTokNormalCustomRedirects = r.proxiTokNormalCustomRedirects; - - proxiTokTorRedirectsChecks = r.proxiTokTorRedirectsChecks; - proxiTokTorCustomRedirects = r.proxiTokTorCustomRedirects; - } - ) -} - export default { getRedirects, setRedirects, @@ -199,6 +193,5 @@ export default { initProxiTokCookies, setProxiTokCookies, - initDefaults, - init, + initDefaults }; diff --git a/src/assets/javascripts/helpers/translate/translate.js b/src/assets/javascripts/helpers/translate/translate.js index 3ca4dd9..d78a66b 100644 --- a/src/assets/javascripts/helpers/translate/translate.js +++ b/src/assets/javascripts/helpers/translate/translate.js @@ -1,6 +1,6 @@ window.browser = window.browser || window.chrome; -import commonHelper from '../common.js' +import utils from '../utils.js' const targets = [ /^https?:\/{2}translate\.google(\.[a-z]{2,3}){1,2}\//, @@ -17,94 +17,32 @@ let redirects = { } }; -const getRedirects = () => redirects; +function setRedirects(val) { + browser.storage.local.get('cloudflareList', async r => { + redirects = val; + simplyTranslateNormalRedirectsChecks = [...redirects.simplyTranslate.normal]; + lingvaNormalRedirectsChecks = [...redirects.lingva.normal] + for (const instance of r.cloudflareList) { + let i; + i = simplyTranslateNormalRedirectsChecks.indexOf(instance); + if (i > -1) simplyTranslateNormalRedirectsChecks.splice(i, 1); -function setSimplyTranslateRedirects(val) { - redirects.simplyTranslate = val; - browser.storage.local.set({ translateRedirects: redirects }) - console.log("simplyTranslateRedirects:", val) - for (const item of simplyTranslateNormalRedirectsChecks) - if (!redirects.simplyTranslate.normal.includes(item)) { - var index = simplyTranslateNormalRedirectsChecks.indexOf(item); - if (index !== -1) simplyTranslateNormalRedirectsChecks.splice(index, 1); + i = lingvaNormalRedirectsChecks.indexOf(instance); + if (i > -1) lingvaNormalRedirectsChecks.splice(i, 1); } - browser.storage.local.set({ simplyTranslateNormalRedirectsChecks }) - - for (const item of simplyTranslateTorRedirectsChecks) - if (!redirects.simplyTranslate.normal.includes(item)) { - var index = simplyTranslateTorRedirectsChecks.indexOf(item); - if (index !== -1) simplyTranslateTorRedirectsChecks.splice(index, 1); - } - browser.storage.local.set({ simplyTranslateTorRedirectsChecks }) -} - -function setLingvaRedirects(val) { - redirects.lingva = val; - browser.storage.local.set({ translateRedirects: redirects }) - console.log("lingvaRedirects:", val) - for (const item of lingvaNormalRedirectsChecks) - if (!redirects.lingva.normal.includes(item)) { - var index = lingvaNormalRedirectsChecks.indexOf(item); - if (index !== -1) lingvaNormalRedirectsChecks.splice(index, 1); - } - browser.storage.local.set({ lingvaNormalRedirectsChecks }) - - for (const item of lingvaTorRedirectsChecks) - if (!redirects.lingva.normal.includes(item)) { - var index = lingvaTorRedirectsChecks.indexOf(item); - if (index !== -1) lingvaTorRedirectsChecks.splice(index, 1); - } - browser.storage.local.set({ lingvaTorRedirectsChecks }) + browser.storage.local.set({ + translateRedirects: redirects, + simplyTranslateNormalRedirectsChecks, + simplyTranslateTorRedirectsChecks: redirects.simplyTranslate.tor, + lingvaNormalRedirectsChecks, + lingvaTorRedirectsChecks: redirects.lingva.tor, + }) + }) } let simplyTranslateNormalRedirectsChecks, - simplyTranslateTorRedirectsChecks, - simplyTranslateNormalCustomRedirects, - simplyTranslateTorCustomRedirects, - lingvaNormalRedirectsChecks, - lingvaTorRedirectsChecks, - lingvaNormalCustomRedirects, - lingvaTorCustomRedirects; - -let - disable, // translateDisable - frontend, // translateFrontend - protocol; // translateProtocol - -function isTranslateRedirects(url, type, frontend) { - let protocolHost = commonHelper.protocolHost(url); - - if (type !== "main_frame") return; - - if (frontend == 'simplyTranslate') - return [ - ...redirects.simplyTranslate.normal, - ...redirects.simplyTranslate.tor, - ...simplyTranslateNormalCustomRedirects, - ...simplyTranslateTorCustomRedirects, - ].includes(protocolHost); - - if (frontend == 'lingva') - return [ - ...redirects.lingva.normal, - ...redirects.lingva.tor, - ...lingvaNormalCustomRedirects, - ...lingvaTorCustomRedirects, - ].includes(protocolHost); - - return [ - ...redirects.simplyTranslate.normal, - ...redirects.simplyTranslate.tor, - ...simplyTranslateNormalCustomRedirects, - ...simplyTranslateTorCustomRedirects, - - ...redirects.lingva.normal, - ...redirects.lingva.tor, - ...lingvaNormalCustomRedirects, - ...lingvaTorCustomRedirects, - ].includes(protocolHost); -} + lingvaNormalRedirectsChecks; function initLingvaLocalStorage(url, tabId) { return new Promise(resolve => { @@ -116,13 +54,13 @@ function initLingvaLocalStorage(url, tabId) { "lingvaTorCustomRedirects", ], r => { - let protocolHost = commonHelper.protocolHost(url); + let protocolHost = utils.protocolHost(url); if (![ ...r.lingvaNormalRedirectsChecks, ...r.lingvaTorRedirectsChecks, ...r.lingvaNormalCustomRedirects, ...r.lingvaTorCustomRedirects, - ].includes(protocolHost)) return; + ].includes(protocolHost)) { resolve(); return; } browser.tabs.executeScript( tabId, { @@ -136,34 +74,36 @@ function initLingvaLocalStorage(url, tabId) { }) } -async function setLingvaLocalStorage(url, tabId) { - browser.storage.local.get( - [ - "disableYoutube", - "youtubeFrontend", - "lingvaNormalRedirectsChecks", - "lingvaNormalCustomRedirects", - "lingvaTorRedirectsChecks", - "lingvaTorCustomRedirects", - ], - r => { - if (r.disableYoutube || r.youtubeFrontend != 'lingva') return; - let protocolHost = commonHelper.protocolHost(url); - if (![ - ...r.lingvaNormalRedirectsChecks, - ...r.lingvaTorRedirectsChecks, - ...r.lingvaNormalCustomRedirects, - ...r.lingvaTorCustomRedirects, - ].includes(protocolHost)) return; - browser.tabs.executeScript( - tabId, - { - file: "/assets/javascripts/helpers/youtube/set_lingva_preferences.js", - runAt: "document_start" - } - ); - return true; - }) +function setLingvaLocalStorage(url, tabId) { + return new Promise(resolve => { + browser.storage.local.get( + [ + "disableYoutube", + "youtubeFrontend", + "lingvaNormalRedirectsChecks", + "lingvaNormalCustomRedirects", + "lingvaTorRedirectsChecks", + "lingvaTorCustomRedirects", + ], + r => { + if (r.disableYoutube || r.youtubeFrontend != 'lingva') { resolve(); return; } + let protocolHost = utils.protocolHost(url); + if (![ + ...r.lingvaNormalRedirectsChecks, + ...r.lingvaTorRedirectsChecks, + ...r.lingvaNormalCustomRedirects, + ...r.lingvaTorCustomRedirects, + ].includes(protocolHost)) { resolve(); return; } + browser.tabs.executeScript( + tabId, + { + file: "/assets/javascripts/helpers/youtube/set_lingva_preferences.js", + runAt: "document_start" + } + ); + resolve(true); + }) + }) } function initSimplyTranslateCookies(from) { @@ -179,7 +119,7 @@ function initSimplyTranslateCookies(from) { "simplyTranslateI2pCustomRedirects", ], r => { - let protocolHost = commonHelper.protocolHost(from); + let protocolHost = utils.protocolHost(from); if (![ ...r.simplyTranslateNormalRedirectsChecks, ...r.simplyTranslateNormalCustomRedirects, @@ -187,17 +127,17 @@ function initSimplyTranslateCookies(from) { ...r.simplyTranslateTorCustomRedirects, ...r.simplyTranslateI2pRedirectsChecks, ...r.simplyTranslateI2pCustomRedirects, - ].includes(protocolHost)) resolve(); + ].includes(protocolHost)) { resolve(); return; } let checkedInstances; if (r.translateProtocol == 'normal') checkedInstances = [...r.simplyTranslateNormalRedirectsChecks, ...r.simplyTranslateNormalCustomRedirects] else if (r.translateProtocol == 'tor') checkedInstances = [...r.simplyTranslateTorRedirectsChecks, ...r.simplyTranslateTorCustomRedirects] else if (r.translateProtocol == 'i2p') checkedInstances = [...r.simplyTranslateI2pRedirectsChecks, ...r.simplyTranslateI2pCustomRedirects] for (const to of checkedInstances) { - commonHelper.copyCookie('simplyTranslate', from, to, 'from_lang'); - commonHelper.copyCookie('simplyTranslate', from, to, 'to_lang'); - commonHelper.copyCookie('simplyTranslate', from, to, 'tts_enabled'); - commonHelper.copyCookie('simplyTranslate', from, to, 'use_text_fields'); + utils.copyCookie('simplyTranslate', from, to, 'from_lang'); + utils.copyCookie('simplyTranslate', from, to, 'to_lang'); + utils.copyCookie('simplyTranslate', from, to, 'tts_enabled'); + utils.copyCookie('simplyTranslate', from, to, 'use_text_fields'); } resolve(true); } @@ -222,87 +162,132 @@ function setSimplyTranslateCookies() { if (r.translateProtocol == 'normal') checkedInstances = [...r.simplyTranslateNormalRedirectsChecks, ...r.simplyTranslateNormalCustomRedirects] else if (r.translateProtocol == 'tor') checkedInstances = [...r.simplyTranslateTorRedirectsChecks, ...r.simplyTranslateTorCustomRedirects] for (const to of checkedInstances) { - commonHelper.getCookiesFromStorage('simplyTranslate', to, 'from_lang'); - commonHelper.getCookiesFromStorage('simplyTranslate', to, 'to_lang'); - commonHelper.getCookiesFromStorage('simplyTranslate', to, 'tts_enabled'); - commonHelper.getCookiesFromStorage('simplyTranslate', to, 'use_text_fields'); + utils.getCookiesFromStorage('simplyTranslate', to, 'from_lang'); + utils.getCookiesFromStorage('simplyTranslate', to, 'to_lang'); + utils.getCookiesFromStorage('simplyTranslate', to, 'tts_enabled'); + utils.getCookiesFromStorage('simplyTranslate', to, 'use_text_fields'); } } ) } function redirect(url) { - if (disable) return; - if (!targets.some(rx => rx.test(url.href))) return; + return new Promise(resolve => { + browser.storage.local.get( + [ + "translateDisable", + "translateFrontend", + "translateProtocol", + "translateRedirects", - if (frontend == 'simplyTranslate') { - let instancesList; - if (protocol == 'normal') instancesList = [...simplyTranslateNormalRedirectsChecks, ...simplyTranslateNormalCustomRedirects]; - if (protocol == 'tor') instancesList = [...simplyTranslateTorRedirectsChecks, ...simplyTranslateTorCustomRedirects]; - if (instancesList.length === 0) return; - let randomInstance = commonHelper.getRandomInstance(instancesList) + "simplyTranslateNormalRedirectsChecks", + "simplyTranslateNormalCustomRedirects", + "simplyTranslateTorRedirectsChecks", + "simplyTranslateTorCustomRedirects", - return `${randomInstance}/${url.search}`; - } - else if (frontend == 'lingva') { - let params_arr = url.search.split('&'); - params_arr[0] = params_arr[0].substring(1); - let myMap = {}; - for (let i = 0; i < params_arr.length; i++) { - let pair = params_arr[i].split('='); - myMap[pair[0]] = pair[1]; - } - let instancesList; - if (protocol == 'normal') instancesList = [...lingvaNormalRedirectsChecks, ...lingvaNormalCustomRedirects]; - if (protocol == 'tor') instancesList = [...lingvaTorRedirectsChecks, ...lingvaTorCustomRedirects]; - if (instancesList.length === 0) return; - let randomInstance = commonHelper.getRandomInstance(instancesList) + "lingvaNormalRedirectsChecks", + "lingvaNormalCustomRedirects", + "lingvaTorRedirectsChecks", + "lingvaTorCustomRedirects", + ], + r => { + if (r.translateDisable) { resolve(); return; }; + if (!targets.some(rx => rx.test(url.href))) { resolve(); return; }; - if (myMap.sl && myMap.tl && myMap.text) - return `${randomInstance}/${myMap.sl}/${myMap.tl}/${myMap.text}`; + if (r.translateFrontend == 'simplyTranslate') { + let instancesList; + if (r.translateProtocol == 'normal') instancesList = [...r.simplyTranslateNormalRedirectsChecks, ...r.simplyTranslateNormalCustomRedirects]; + if (r.translateProtocol == 'tor') instancesList = [...r.simplyTranslateTorRedirectsChecks, ...r.simplyTranslateTorCustomRedirects]; + if (instancesList.length === 0) { resolve(); return; }; - return randomInstance; - } + let randomInstance = utils.getRandomInstance(instancesList) + console.log(`${randomInstance}/${url.search}`); + resolve(`${randomInstance}/${url.search}`); + + } + else if (r.translateFrontend == 'lingva') { + let params_arr = url.search.split('&'); + params_arr[0] = params_arr[0].substring(1); + let myMap = {}; + for (let i = 0; i < params_arr.length; i++) { + let pair = params_arr[i].split('='); + myMap[pair[0]] = pair[1]; + } + let instancesList; + if (r.translateProtocol == 'normal') instancesList = [...r.lingvaNormalRedirectsChecks, ...r.lingvaNormalCustomRedirects]; + if (r.translateProtocol == 'tor') instancesList = [...r.lingvaTorRedirectsChecks, ...r.lingvaTorCustomRedirects]; + if (instancesList.length === 0) { resolve(); return; } + let randomInstance = utils.getRandomInstance(instancesList) + + if (myMap.sl && myMap.tl && myMap.text) { + resolve(`${randomInstance}/${myMap.sl}/${myMap.tl}/${myMap.text}`); return; + } + resolve(randomInstance); + } + } + ) + }) } function switchInstance(url) { - let protocolHost = commonHelper.protocolHost(url); + return new Promise(resolve => { + browser.storage.local.get( + [ + "translateFrontend", + "translateProtocol", + "translateRedirects", - let translateList = [ - ...redirects.simplyTranslate.normal, - ...redirects.simplyTranslate.tor, + "simplyTranslateNormalRedirectsChecks", + "simplyTranslateNormalCustomRedirects", + "simplyTranslateTorRedirectsChecks", + "simplyTranslateTorCustomRedirects", - ...simplyTranslateNormalCustomRedirects, - ...simplyTranslateTorCustomRedirects, + "lingvaNormalRedirectsChecks", + "lingvaNormalCustomRedirects", + "lingvaTorRedirectsChecks", + "lingvaTorCustomRedirects", + ], + r => { + let protocolHost = utils.protocolHost(url); - ...redirects.lingva.normal, - ...redirects.lingva.tor, + let translateList = [ + ...r.translateRedirects.simplyTranslate.normal, + ...r.translateRedirects.simplyTranslate.tor, - ...lingvaNormalCustomRedirects, - ...lingvaTorCustomRedirects, - ] + ...r.simplyTranslateNormalCustomRedirects, + ...r.simplyTranslateTorCustomRedirects, - if (!translateList.includes(protocolHost)) return null; + ...r.translateRedirects.lingva.normal, + ...r.translateRedirects.lingva.tor, - let instancesList; - if (frontend == 'simplyTranslate') { - if (protocol == 'normal') instancesList = [...simplyTranslateNormalRedirectsChecks, ...simplyTranslateNormalCustomRedirects]; - else if (protocol == 'tor') instancesList = [...simplyTranslateTorRedirectsChecks, ...simplyTranslateTorCustomRedirects]; - } - else if (frontend == 'lingva') { - if (protocol == 'normal') instancesList = [...lingvaNormalRedirectsChecks, ...lingvaNormalCustomRedirects]; - else if (protocol == 'tor') instancesList = [...lingvaTorRedirectsChecks, ...lingvaTorCustomRedirects]; - } + ...r.lingvaNormalCustomRedirects, + ...r.lingvaTorCustomRedirects, + ] - let index = instancesList.indexOf(protocolHost); - if (index > -1) instancesList.splice(index, 1); - if (instancesList.length === 0) return null; + if (!translateList.includes(protocolHost)) { resolve(); return; } - let randomInstance = commonHelper.getRandomInstance(instancesList); - return `${randomInstance}${url.pathname}${url.search}`; + let instancesList; + + if (r.translateProtocol == 'normal') { + if (r.translateFrontend == 'simplyTranslate') instancesList = [...r.simplyTranslateNormalRedirectsChecks, ...r.simplyTranslateNormalCustomRedirects]; + else if (r.translateFrontend == 'lingva') [...r.lingvaNormalRedirectsChecks, ...r.lingvaNormalCustomRedirects]; + } + else if (r.translateProtocol == 'tor') { + if (r.translateFrontend == 'simplyTranslate') instancesList = [...r.simplyTranslateTorRedirectsChecks, ...r.simplyTranslateTorCustomRedirects]; + else if (r.translateFrontend == 'lingva') instancesList = [...r.lingvaTorRedirectsChecks, ...r.lingvaTorCustomRedirects]; + } + + let index = instancesList.indexOf(protocolHost); + if (index > -1) instancesList.splice(index, 1); + if (instancesList.length === 0) { resolve(); return; } + + let randomInstance = utils.getRandomInstance(instancesList); + return `${randomInstance}${url.pathname}${url.search}`; + }) + }) } -async function initDefaults() { +function initDefaults() { fetch('/instances/data.json').then(response => response.text()).then(async data => { let dataJson = JSON.parse(data); redirects.simplyTranslate = dataJson.simplyTranslate; @@ -339,58 +324,15 @@ async function initDefaults() { }) } -async function init() { - browser.storage.local.get( - [ - "translateDisable", - "translateFrontend", - "translateProtocol", - "translateRedirects", - - "simplyTranslateNormalRedirectsChecks", - "simplyTranslateNormalCustomRedirects", - "simplyTranslateTorRedirectsChecks", - "simplyTranslateTorCustomRedirects", - - "lingvaNormalRedirectsChecks", - "lingvaNormalCustomRedirects", - "lingvaTorRedirectsChecks", - "lingvaTorCustomRedirects", - ], - r => { - disable = r.translateDisable; - frontend = r.translateFrontend; - protocol = r.translateProtocol; - redirects = r.translateRedirects; - - simplyTranslateNormalRedirectsChecks = r.simplyTranslateNormalRedirectsChecks; - simplyTranslateNormalCustomRedirects = r.simplyTranslateNormalCustomRedirects; - - simplyTranslateTorRedirectsChecks = r.simplyTranslateTorRedirectsChecks; - simplyTranslateTorCustomRedirects = r.simplyTranslateTorCustomRedirects; - - lingvaNormalRedirectsChecks = r.lingvaNormalRedirectsChecks; - lingvaNormalCustomRedirects = r.lingvaNormalCustomRedirects; - - lingvaTorRedirectsChecks = r.lingvaTorRedirectsChecks; - lingvaTorCustomRedirects = r.lingvaTorCustomRedirects; - }); -} - export default { - getRedirects, - - isTranslateRedirects, initSimplyTranslateCookies, setSimplyTranslateCookies, initLingvaLocalStorage, setLingvaLocalStorage, - setSimplyTranslateRedirects, - setLingvaRedirects, + setRedirects, redirect, initDefaults, - init, switchInstance, }; diff --git a/src/assets/javascripts/helpers/twitter.js b/src/assets/javascripts/helpers/twitter.js index b2469d7..2660299 100644 --- a/src/assets/javascripts/helpers/twitter.js +++ b/src/assets/javascripts/helpers/twitter.js @@ -1,6 +1,6 @@ window.browser = window.browser || window.chrome; -import commonHelper from './common.js' +import utils from './utils.js' const targets = [ /^https?:\/{2}(www\.|mobile\.|)twitter\.com/, @@ -16,125 +16,171 @@ let redirects = { }, }; -const getRedirects = () => redirects; function setRedirects(val) { - redirects.nitter = val; - browser.storage.local.set({ twitterRedirects: redirects }) - console.log("twitterRedirects:", val) - for (const item of nitterNormalRedirectsChecks) - if (!redirects.nitter.normal.includes(item)) { - var index = nitterNormalRedirectsChecks.indexOf(item); - if (index !== -1) nitterNormalRedirectsChecks.splice(index, 1); + browser.storage.local.get('cloudflareList', r => { + redirects.nitter = val; + nitterNormalRedirectsChecks = [...redirects.nitter.normal]; + for (const instance of r.cloudflareList) { + let i = nitterNormalRedirectsChecks.indexOf(instance); + if (i > -1) nitterNormalRedirectsChecks.splice(i, 1); } - browser.storage.local.set({ nitterNormalRedirectsChecks }) - - for (const item of nitterTorRedirectsChecks) - if (!redirects.nitter.tor.includes(item)) { - var index = nitterTorRedirectsChecks.indexOf(item); - if (index !== -1) nitterTorRedirectsChecks.splice(index, 1); - } - browser.storage.local.set({ nitterTorRedirectsChecks }) + browser.storage.local.set({ + twitterRedirects: redirects, + nitterNormalRedirectsChecks, + nitterTorRedirectsChecks: [...redirects.nitter.tor] + }) + }) } let nitterNormalRedirectsChecks; -let nitterNormalCustomRedirects = []; -let nitterTorRedirectsChecks; -let nitterTorCustomRedirects = []; -let disable; // disableTwitter -let protocol; // twitterProtocol +async function redirect(url, initiator) { + return new Promise(resolve => { + browser.storage.local.get( + [ + "disableTwitter", + "twitterProtocol", -function redirect(url, initiator) { - if (disable) return; - if (!targets.some(rx => rx.test(url.href))) return; - if (url.pathname.split("/").includes("home")) return; + "twitterRedirects", - if ( - initiator && - [...redirects.nitter.normal, - ...redirects.nitter.tor, - ...nitterTorCustomRedirects, - ...nitterNormalCustomRedirects - ].includes(initiator.origin) + "nitterNormalRedirectsChecks", + "nitterNormalCustomRedirects", - ) return 'BYPASSTAB'; + "nitterTorRedirectsChecks", + "nitterTorCustomRedirects", + ], + r => { + if (r.disableTwitter) { resolve(); return; } + if (!targets.some(rx => rx.test(url.href))) { resolve(); return; } + if (url.pathname.split("/").includes("home")) { resolve(); return; } - let instancesList; - if (protocol == 'normal') - instancesList = [...nitterNormalRedirectsChecks, ...nitterNormalCustomRedirects]; - else if (protocol == 'tor') - instancesList = [...nitterTorRedirectsChecks, ...nitterTorCustomRedirects]; + if ( + initiator && + [...r.twitterRedirects.nitter.normal, + ...r.twitterRedirects.nitter.tor, + ...r.nitterTorCustomRedirects, + ...r.nitterNormalCustomRedirects + ].includes(initiator.origin) + ) { resolve('BYPASSTAB'); return; } - if (instancesList.length === 0) return null; - let randomInstance = commonHelper.getRandomInstance(instancesList) + let instancesList; + if (r.twitterProtocol == 'normal') instancesList = [...r.nitterNormalRedirectsChecks, ...r.nitterNormalCustomRedirects]; + else if (r.twitterProtocol == 'tor') instancesList = [...r.nitterTorRedirectsChecks, ...r.nitterTorCustomRedirects]; + if (instancesList.length === 0) { resolve(); return; } - // https://pbs.twimg.com/profile_images/648888480974508032/66_cUYfj_400x400.jpg - if (url.host.split(".")[0] === "pbs" || url.host.split(".")[0] === "video") - return `${randomInstance}/pic/${encodeURIComponent(`${url.host}${url.pathname}`)}`; - else if (url.pathname.split("/").includes("tweets")) - return `${randomInstance}${url.pathname.replace("/tweets", "")}${url.search}`; - else if (url.host == 't.co') - return `${randomInstance}/t.co${url.pathname}`; - else - return `${randomInstance}${url.pathname}${url.search}`; + let randomInstance = utils.getRandomInstance(instancesList) + console.log('randomInstance', randomInstance); + // https://pbs.twimg.com/profile_images/648888480974508032/66_cUYfj_400x400.jpg + if (url.host.split(".")[0] === "pbs" || url.host.split(".")[0] === "video") + resolve(`${randomInstance}/pic/${encodeURIComponent(`${url.host}${url.pathname}`)}`); + else if (url.pathname.split("/").includes("tweets")) + resolve(`${randomInstance}${url.pathname.replace("/tweets", "")}${url.search}`); + else if (url.host == 't.co') + resolve(`${randomInstance}/t.co${url.pathname}`); + else + resolve(`${randomInstance}${url.pathname}${url.search}`); + } + ) + }) } function reverse(url) { - let protocolHost = commonHelper.protocolHost(url); - if ( - ![...redirects.nitter.normal, - ...redirects.nitter.tor, - ...nitterNormalCustomRedirects, - ...nitterTorCustomRedirects].includes(protocolHost) - ) return; - return `https://twitter.com${url.pathname}${url.search}`; + return new Promise(resolve => { + browser.storage.local.get( + [ + "twitterRedirects", + "nitterNormalCustomRedirects", + "nitterTorCustomRedirects", + ], + r => { + let protocolHost = utils.protocolHost(url); + if ( + ![ + ...r.twitterRedirects.nitter.normal, + ...r.twitterRedirects.nitter.tor, + ...r.nitterNormalCustomRedirects, + ...r.nitterTorCustomRedirects + ].includes(protocolHost) + ) { resolve(); return; } + resolve(`https://twitter.com${url.pathname}${url.search}`); + } + ) + }) } function switchInstance(url) { - let protocolHost = commonHelper.protocolHost(url); + return new Promise(resolve => { + browser.storage.local.get( + [ + "twitterRedirects", + "twitterProtocol", - let twitterList = [ - ...redirects.nitter.normal, - ...redirects.nitter.tor, - ...nitterNormalCustomRedirects, - ...nitterTorCustomRedirects, - ]; + "nitterNormalRedirectsChecks", + "nitterNormalCustomRedirects", - if (!twitterList.includes(protocolHost)) return null; + "nitterTorRedirectsChecks", + "nitterTorCustomRedirects", + ], + r => { + let protocolHost = utils.protocolHost(url); + if (![ + ...r.twitterRedirects.nitter.normal, + ...r.twitterRedirects.nitter.tor, + ...r.nitterNormalCustomRedirects, + ...r.nitterTorCustomRedirects, + ].includes(protocolHost)) { resolve(); return; } - let instancesList; - if (protocol == 'normal') instancesList = [...nitterNormalRedirectsChecks, ...nitterNormalCustomRedirects]; - else if (protocol == 'tor') instancesList = [...nitterTorRedirectsChecks, ...nitterTorCustomRedirects]; + let instancesList; + if (r.twitterProtocol == 'normal') instancesList = [...r.nitterNormalRedirectsChecks, ...r.nitterNormalCustomRedirects]; + else if (r.twitterProtocol == 'tor') instancesList = [...r.nitterTorRedirectsChecks, ...r.nitterTorCustomRedirects]; - console.log("instancesList", instancesList); - let index = instancesList.indexOf(protocolHost); - if (index > -1) instancesList.splice(index, 1); + let index = instancesList.indexOf(protocolHost); + if (index > -1) instancesList.splice(index, 1); + if (instancesList.length === 0) { resolve(); return; } - if (instancesList.length === 0) return null; - - let randomInstance = commonHelper.getRandomInstance(instancesList); - return `${randomInstance}${url.pathname}${url.search}`; + let randomInstance = utils.getRandomInstance(instancesList); + resolve(`${randomInstance}${url.pathname}${url.search}`); + }) + }) } function removeXFrameOptions(e) { - let url = new URL(e.url); - let protocolHost = commonHelper.protocolHost(url); - let twitterList = [ - ...redirects.nitter.normal, - ...redirects.nitter.tor, - ...nitterNormalCustomRedirects, - ...nitterTorCustomRedirects, - ]; - if (!twitterList.includes(protocolHost) || e.type != 'sub_frame') return; - let isChanged = false; - for (const i in e.responseHeaders) if (e.responseHeaders[i].name == 'x-frame-options') { - e.responseHeaders.splice(i, 1); - isChanged = true; - } - if (isChanged) return { responseHeaders: e.responseHeaders }; + return new Promise(resolve => { + browser.storage.local.get( + [ + "twitterRedirects", + "twitterProtocol", + + "nitterNormalRedirectsChecks", + "nitterNormalCustomRedirects", + + "nitterTorRedirectsChecks", + "nitterTorCustomRedirects", + ], + r => { + let url = new URL(e.url); + let protocolHost = utils.protocolHost(url); + if ( + ![ + ...r.twitterRedirects.nitter.normal, + ...r.twitterRedirects.nitter.tor, + ...r.nitterNormalCustomRedirects, + ...r.nitterTorCustomRedirects, + ].includes(protocolHost) || + e.type != 'sub_frame' + ) { resolve(); return; } + let isChanged = false; + for (const i in e.responseHeaders) if (e.responseHeaders[i].name == 'x-frame-options') { + e.responseHeaders.splice(i, 1); + isChanged = true; + } + if (isChanged) resolve({ responseHeaders: e.responseHeaders }); + } + ) + }) } -async function initNitterCookies(from) { +function initNitterCookies(from) { return new Promise(resolve => { browser.storage.local.get( [ @@ -145,33 +191,35 @@ async function initNitterCookies(from) { "nitterTorCustomRedirects", ], r => { - let protocolHost = commonHelper.protocolHost(from); - if (![ - ...r.nitterNormalRedirectsChecks, - ...r.nitterTorRedirectsChecks, - ...r.nitterNormalCustomRedirects, - ...r.nitterTorCustomRedirects, - ].includes(protocolHost)) resolve(); + let protocolHost = utils.protocolHost(from); + if ( + ![ + ...r.nitterNormalRedirectsChecks, + ...r.nitterTorRedirectsChecks, + ...r.nitterNormalCustomRedirects, + ...r.nitterTorCustomRedirects, + ].includes(protocolHost) + ) { resolve(); return; } let checkedInstances; if (r.twitterProtocol == 'normal') checkedInstances = [...r.nitterNormalRedirectsChecks, ...r.nitterNormalCustomRedirects] else if (r.twitterProtocol == 'tor') checkedInstances = [...r.nitterTorRedirectsChecks, ...r.nitterTorCustomRedirects] for (const to of checkedInstances) { - commonHelper.copyCookie('nitter', from, to, 'theme'); - commonHelper.copyCookie('nitter', from, to, 'infiniteScroll'); - commonHelper.copyCookie('nitter', from, to, 'stickyProfile'); - commonHelper.copyCookie('nitter', from, to, 'bidiSupport'); - commonHelper.copyCookie('nitter', from, to, 'hideTweetStats'); - commonHelper.copyCookie('nitter', from, to, 'hideBanner'); - commonHelper.copyCookie('nitter', from, to, 'hidePins'); - commonHelper.copyCookie('nitter', from, to, 'hideReplies'); - commonHelper.copyCookie('nitter', from, to, 'squareAvatars'); - commonHelper.copyCookie('nitter', from, to, 'mp4Playback'); - commonHelper.copyCookie('nitter', from, to, 'hlsPlayback'); - commonHelper.copyCookie('nitter', from, to, 'proxyVideos'); - commonHelper.copyCookie('nitter', from, to, 'muteVideos'); - commonHelper.copyCookie('nitter', from, to, 'autoplayGifs'); + utils.copyCookie('nitter', from, to, 'theme'); + utils.copyCookie('nitter', from, to, 'infiniteScroll'); + utils.copyCookie('nitter', from, to, 'stickyProfile'); + utils.copyCookie('nitter', from, to, 'bidiSupport'); + utils.copyCookie('nitter', from, to, 'hideTweetStats'); + utils.copyCookie('nitter', from, to, 'hideBanner'); + utils.copyCookie('nitter', from, to, 'hidePins'); + utils.copyCookie('nitter', from, to, 'hideReplies'); + utils.copyCookie('nitter', from, to, 'squareAvatars'); + utils.copyCookie('nitter', from, to, 'mp4Playback'); + utils.copyCookie('nitter', from, to, 'hlsPlayback'); + utils.copyCookie('nitter', from, to, 'proxyVideos'); + utils.copyCookie('nitter', from, to, 'muteVideos'); + utils.copyCookie('nitter', from, to, 'autoplayGifs'); } resolve(true); }) @@ -195,20 +243,20 @@ function setNitterCookies() { if (r.youtubeProtocol == 'normal') checkedInstances = [...r.nitterNormalRedirectsChecks, ...r.nitterNormalCustomRedirects] else if (r.youtubeProtocol == 'tor') checkedInstances = [...r.nitterTorRedirectsChecks, ...r.nitterTorCustomRedirects] for (const to of checkedInstances) { - commonHelper.getCookiesFromStorage('nitter', to, 'theme'); - commonHelper.getCookiesFromStorage('nitter', to, 'infiniteScroll'); - commonHelper.getCookiesFromStorage('nitter', to, 'stickyProfile'); - commonHelper.getCookiesFromStorage('nitter', to, 'bidiSupport'); - commonHelper.getCookiesFromStorage('nitter', to, 'hideTweetStats'); - commonHelper.getCookiesFromStorage('nitter', to, 'hideBanner'); - commonHelper.getCookiesFromStorage('nitter', to, 'hidePins'); - commonHelper.getCookiesFromStorage('nitter', to, 'hideReplies'); - commonHelper.getCookiesFromStorage('nitter', to, 'squareAvatars'); - commonHelper.getCookiesFromStorage('nitter', to, 'mp4Playback'); - commonHelper.getCookiesFromStorage('nitter', to, 'hlsPlayback'); - commonHelper.getCookiesFromStorage('nitter', to, 'proxyVideos'); - commonHelper.getCookiesFromStorage('nitter', to, 'muteVideos'); - commonHelper.getCookiesFromStorage('nitter', to, 'autoplayGifs'); + utils.getCookiesFromStorage('nitter', to, 'theme'); + utils.getCookiesFromStorage('nitter', to, 'infiniteScroll'); + utils.getCookiesFromStorage('nitter', to, 'stickyProfile'); + utils.getCookiesFromStorage('nitter', to, 'bidiSupport'); + utils.getCookiesFromStorage('nitter', to, 'hideTweetStats'); + utils.getCookiesFromStorage('nitter', to, 'hideBanner'); + utils.getCookiesFromStorage('nitter', to, 'hidePins'); + utils.getCookiesFromStorage('nitter', to, 'hideReplies'); + utils.getCookiesFromStorage('nitter', to, 'squareAvatars'); + utils.getCookiesFromStorage('nitter', to, 'mp4Playback'); + utils.getCookiesFromStorage('nitter', to, 'hlsPlayback'); + utils.getCookiesFromStorage('nitter', to, 'proxyVideos'); + utils.getCookiesFromStorage('nitter', to, 'muteVideos'); + utils.getCookiesFromStorage('nitter', to, 'autoplayGifs'); } } ) @@ -229,7 +277,7 @@ function initDefaults() { twitterRedirects: redirects, twitterProtocol: "normal", - nitterNormalRedirectsChecks: nitterNormalRedirectsChecks, + nitterNormalRedirectsChecks, nitterNormalCustomRedirects: [], nitterTorRedirectsChecks: [...redirects.nitter.tor], @@ -239,44 +287,16 @@ function initDefaults() { }) } -async function init() { - browser.storage.local.get( - [ - "disableTwitter", - "twitterRedirects", - "twitterProtocol", - - "nitterNormalRedirectsChecks", - "nitterNormalCustomRedirects", - - "nitterTorRedirectsChecks", - "nitterTorCustomRedirects", - ], - r => { - disable = r.disableTwitter; - protocol = r.twitterProtocol; - redirects = r.twitterRedirects; - - nitterNormalRedirectsChecks = r.nitterNormalRedirectsChecks; - nitterNormalCustomRedirects = r.nitterNormalCustomRedirects; - - nitterTorRedirectsChecks = r.nitterTorRedirectsChecks; - nitterTorCustomRedirects = r.nitterTorCustomRedirects; - } - ); -} - export default { - getRedirects, setRedirects, + + redirect, + switchInstance, reverse, removeXFrameOptions, initNitterCookies, setNitterCookies, - redirect, initDefaults, - init, - switchInstance, }; diff --git a/src/assets/javascripts/helpers/common.js b/src/assets/javascripts/helpers/utils.js similarity index 67% rename from src/assets/javascripts/helpers/common.js rename to src/assets/javascripts/helpers/utils.js index 5982fa0..eec9e6b 100644 --- a/src/assets/javascripts/helpers/common.js +++ b/src/assets/javascripts/helpers/utils.js @@ -7,9 +7,12 @@ import redditHelper from "./reddit.js"; import searchHelper from "./search.js"; import translateHelper from "./translate/translate.js"; import wikipediaHelper from "./wikipedia.js"; -import localise from '../localise.js' +import peertubeHelper from "./peertube.js"; +import lbryHelper from "./lbry.js"; import sendTargetsHelper from "./sendTargets.js"; import tikTokHelper from "./tiktok.js"; +import imgurHelper from "./imgur.js"; +import localise from '../localise.js' function getRandomInstance(instances) { return instances[~~(instances.length * Math.random())]; @@ -26,14 +29,6 @@ async function initCloudflareList() { } async function wholeInit() { - await youtubeHelper.init(); - await twitterHelper.init(); - await instagramHelper.init(); - await redditHelper.init(); - await translateHelper.init(); - await searchHelper.init(); - await wikipediaHelper.init(); - await mediumHelper.init(); await sendTargetsHelper.init(); await tikTokHelper.init(); await initCloudflareList(); @@ -160,8 +155,13 @@ async function processDefaultCustomInstances(target, name, protocol, document) { x => { let cloudflare = cloudflareList.includes(x) ? ' cloudflare' : ''; - let latencyColor = (instancesLatency[x] <= 1000 ? "green" : instancesLatency[x] <= 2000 ? "orange" : "red"); - let latencyLimit = (instancesLatency[x] == 5000 ? '5000ms+' : instancesLatency[x] + 'ms') + let ms = instancesLatency[x]; + let latencyColor = (ms <= 1000 ? "green" : ms <= 2000 ? "orange" : "red"); + let latencyLimit; + if (ms == 5000) latencyLimit = '5000ms+'; + else if (ms > 5000) latencyLimit = `ERROR: ${ms - 5000}`; + else latencyLimit = ms + 'ms'; + let latency = x in instancesLatency ? '' + latencyLimit + '' : ''; return `