From ebc9bf35a60325330c6403fbd26987a0f864422b Mon Sep 17 00:00:00 2001 From: ManeraKai Date: Sat, 21 May 2022 01:46:47 +0300 Subject: [PATCH] Cleaned code. Added shortcuts for the CopyRaw btn #268 --- src/_locales/ar/messages.json | 2 +- src/_locales/de/messages.json | 2 +- src/_locales/en/messages.json | 2 +- src/assets/javascripts/helpers/imgur.js | 170 ++--- src/assets/javascripts/helpers/instagram.js | 177 ++--- src/assets/javascripts/helpers/lbry.js | 139 ++-- src/assets/javascripts/helpers/maps.js | 7 +- src/assets/javascripts/helpers/medium.js | 130 ++-- src/assets/javascripts/helpers/peertube.js | 55 +- src/assets/javascripts/helpers/reddit.js | 361 +++++---- src/assets/javascripts/helpers/search.js | 479 ++++++------ src/assets/javascripts/helpers/sendTargets.js | 57 +- src/assets/javascripts/helpers/tiktok.js | 101 ++- .../helpers/translate/translate.js | 384 ++++------ src/assets/javascripts/helpers/twitter.js | 338 +++++---- .../helpers/{common.js => utils.js} | 157 +++- src/assets/javascripts/helpers/wikipedia.js | 180 ++--- .../javascripts/helpers/youtube/youtube.js | 699 +++++++++--------- .../javascripts/helpers/youtubeMusic.js | 118 ++- src/manifest.json | 14 +- src/pages/background/background.js | 100 +-- src/pages/background/incognito.js | 11 - src/pages/options/general/general.js | 4 +- src/pages/options/imgur/imgur.js | 28 +- src/pages/options/init.js | 4 +- src/pages/options/instagram/instagram.js | 25 +- src/pages/options/lbry/lbry.js | 25 +- src/pages/options/maps/maps.js | 23 +- src/pages/options/medium/medium.js | 25 +- src/pages/options/peertube/peertube.js | 26 +- src/pages/options/reddit/reddit.js | 50 +- src/pages/options/search/search.js | 79 +- src/pages/options/sendTargets/sendTargets.js | 25 +- src/pages/options/tiktok/tiktok.js | 25 +- src/pages/options/translate/translate.js | 49 +- src/pages/options/twitter/twitter.html | 21 - src/pages/options/twitter/twitter.js | 25 +- src/pages/options/twitter/twitter.pug | 3 - src/pages/options/wikipedia/wikipedia.js | 27 +- src/pages/options/youtube/youtube.html | 3 - src/pages/options/youtube/youtube.js | 74 +- src/pages/options/youtube/youtube.pug | 3 - .../options/youtubeMusic/youtubeMusic.js | 23 +- src/pages/popup/popup.html | 5 +- src/pages/popup/popup.js | 124 +--- src/pages/popup/popup.pug | 6 +- src/pages/popup/style.css | 9 +- 47 files changed, 1925 insertions(+), 2469 deletions(-) rename src/assets/javascripts/helpers/{common.js => utils.js} (67%) 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 `
@@ -305,7 +305,7 @@ async function testLatency(element, instances) { let text; if (m == 5000) text = '5000ms+' - else if (m > 5000) text = m - 5000 + else if (m > 5000) text = `ERROR: ${m - 5000}`; else text = `${m}ms`; element.innerHTML = `${href}: ${text}`; } @@ -336,13 +336,144 @@ function getCookiesFromStorage(frontend, to, name) { ) } +function copyRaw() { + browser.tabs.query( + { active: true, currentWindow: true }, tabs => { + let currTab = tabs[0]; + if (currTab) { + let url = new URL(currTab.url); + let newUrl; + newUrl = youtubeHelper.reverse(url); + + if (!newUrl) newUrl = twitterHelper.reverse(url); + if (!newUrl) newUrl = instagramHelper.reverse(url); + if (!newUrl) newUrl = tiktokHelper.reverse(url); + if (!newUrl) newUrl = imgurHelper.reverse(url); + + if (newUrl) { + navigator.clipboard.writeText(newUrl); + const textElement = copyRawElement.getElementsByTagName('h4')[0] + const oldHtml = textElement.innerHTML; + textElement.innerHTML = 'Copied'; + setTimeout(() => textElement.innerHTML = oldHtml, 1000); + } + } + } + ) +} + + +function unify() { + browser.tabs.query( + { active: true, currentWindow: true }, + async tabs => { + let currTab = tabs[0] + if (currTab) { + let url = new URL(currTab.url); + + let result = await youtubeHelper.initInvidiousCookies(url); + if (!result) result = await youtubeHelper.initPipedLocalStorage(url, currTab.id); + if (!result) result = await youtubeHelper.initPipedMaterialLocalStorage(url, currTab.id); + + if (!result) result = await twitterHelper.initNitterCookies(url); + + if (!result) result = await redditHelper.initLibredditCookies(url); + if (!result) result = await redditHelper.initTedditCookies(url); + + if (!result) result = await searchHelper.initSearxCookies(url); + if (!result) result = await searchHelper.initSearxngCookies(url); + + if (!result) result = await tiktokHelper.initProxiTokCookies(url); + + if (!result) result = await wikipediaHelper.initWikilessCookies(url); + + if (!result) result = await translateHelper.initSimplyTranslateCookies(url); + if (!result) result = await translateHelper.initLingvaLocalStorage(url); + + if (result) { + const textElement = unifyElement.getElementsByTagName('h4')[0] + const oldHtml = textElement.innerHTML; + textElement.innerHTML = 'Unified'; + setTimeout(() => textElement.innerHTML = oldHtml, 1000); + } + } + } + ) +} + +function switchInstance() { + browser.tabs.query({ active: true, currentWindow: true }, async tabs => { + let currTab = tabs[0]; + if (currTab) { + let url = new URL(currTab.url); + let newUrl; + newUrl = await youtubeHelper.switchInstance(url); + if (!newUrl) newUrl = await twitterHelper.switchInstance(url); + if (!newUrl) newUrl = await instagramHelper.switchInstance(url); + if (!newUrl) newUrl = await redditHelper.switchInstance(url); + if (!newUrl) newUrl = await searchHelper.switchInstance(url); + if (!newUrl) newUrl = await translateHelper.switchInstance(url); + if (!newUrl) newUrl = await mediumHelper.switchInstance(url); + if (!newUrl) newUrl = await sendTargetsHelper.switchInstance(url); + if (!newUrl) newUrl = await peertubeHelper.switchInstance(url); + if (!newUrl) newUrl = await lbryHelper.switchInstance(url); + if (!newUrl) newUrl = await imgurHelper.switchInstance(url); + if (!newUrl) newUrl = await wikipediaHelper.switchInstance(url); + + if (newUrl) { + browser.tabs.update({ url: newUrl }); + return true; + } + } + }) + return false; +} + +function latency(name, frontend, document, location, splitNames) { + let latencyElement; + let latencyLabel; + if (splitNames == true) { + latencyElement = document.getElementById(`latency-${frontend}`); + latencyLabel = document.getElementById(`latency-${frontend}-label`); + } else { + latencyElement = document.getElementById("latency"); + latencyLabel = document.getElementById("latency-label"); + } + latencyElement.addEventListener("click", + async () => { + let reloadWindow = () => location.reload(); + latencyElement.addEventListener("click", reloadWindow); + let key = `${name}Redirects` + browser.storage.local.get( + key, + r => { + let redirects = r[key]; + const oldHtml = latencyLabel.innerHTML; + latencyLabel.innerHTML = '...'; + testLatency(latencyLabel, redirects[frontend].normal).then(r => { + browser.storage.local.set({ [`${frontend}Latency`]: r }); + latencyLabel.innerHTML = oldHtml; + processDefaultCustomInstances(name, frontend, 'normal', document); + latencyElement.removeEventListener("click", reloadWindow) + }); + } + ) + } + ); +} + + + export default { getRandomInstance, updateInstances, protocolHost, processDefaultCustomInstances, isRtl, - testLatency, + latency, copyCookie, getCookiesFromStorage, + switchInstance, + copyRaw, + unify, } diff --git a/src/assets/javascripts/helpers/wikipedia.js b/src/assets/javascripts/helpers/wikipedia.js index a8acb09..c870bff 100644 --- a/src/assets/javascripts/helpers/wikipedia.js +++ b/src/assets/javascripts/helpers/wikipedia.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}(([a-z]{1,}\.){0,})wikipedia\.org/ @@ -63,7 +63,7 @@ function initWikilessCookies(from) { "wikilessI2pCustomRedirects", ], r => { - let protocolHost = commonHelper.protocolHost(from); + let protocolHost = utils.protocolHost(from); if (![ ...r.wikilessNormalRedirectsChecks, ...r.wikilessNormalCustomRedirects, @@ -79,8 +79,8 @@ function initWikilessCookies(from) { else if (r.wikipediaProtocol == 'i2p') checkedInstances = [...r.wikilessI2pRedirectsChecks, ...r.wikilessI2pCustomRedirects] for (const to of checkedInstances) { - commonHelper.copyCookie('wikiless', from, to, 'theme'); - commonHelper.copyCookie('wikiless', from, to, 'default_lang'); + utils.copyCookie('wikiless', from, to, 'theme'); + utils.copyCookie('wikiless', from, to, 'default_lang'); } resolve(true); } @@ -104,76 +104,112 @@ function setWikilessCookies() { if (r.wikipediaProtocol == 'normal') checkedInstances = [...r.wikilessNormalRedirectsChecks, ...r.wikilessNormalCustomRedirects] else if (r.wikipediaProtocol == 'tor') checkedInstances = [...r.wikilessTorRedirectsChecks, ...r.wikilessTorCustomRedirects] for (const to of checkedInstances) { - commonHelper.getCookiesFromStorage('wikiless', to, 'theme'); - commonHelper.getCookiesFromStorage('wikiless', to, 'default_lang'); + utils.getCookiesFromStorage('wikiless', to, 'theme'); + utils.getCookiesFromStorage('wikiless', to, 'default_lang'); } } ) } function redirect(url) { - if (disable) return; - if (!targets.test(url.href)) return; + return new Promise(resolve => { + browser.storage.local.get( + [ + "disableWikipedia", + "wikipediaRedirects", + "wikipediaProtocol", - let GETArguments = []; - if (url.search.length > 0) { - let search = url.search.substring(1); //get rid of '?' - let argstrings = search.split("&"); - for (let i = 0; i < argstrings.length; i++) { - let args = argstrings[i].split("="); - GETArguments.push([args[0], args[1]]); - } - } - let instancesList; - if (protocol == 'normal') instancesList = [...wikilessNormalRedirectsChecks, ...wikilessNormalCustomRedirects]; - else if (protocol == 'tor') instancesList = [...wikilessTorRedirectsChecks, ...wikilessTorCustomRedirects]; - else if (protocol == 'i2p') instancesList = [...wikilessI2pRedirectsChecks, ...wikilessI2pCustomRedirects]; - if (instancesList.length === 0) return null; - let randomInstance = commonHelper.getRandomInstance(instancesList) + "wikilessNormalRedirectsChecks", + "wikilessTorRedirectsChecks", + "wikilessI2pRedirectsChecks", + "wikilessNormalCustomRedirects", + "wikilessTorCustomRedirects", + "wikilessI2pCustomRedirects", + ], + r => { + if (r.disableWikipedia) { resolve(); return; } + if (!targets.test(url.href)) { resolve(); return; } - let link = `${randomInstance}${url.pathname}`; - let urlSplit = url.host.split("."); - if (urlSplit[0] != "wikipedia" && urlSplit[0] != "www") { - if (urlSplit[0] == "m") - GETArguments.push(["mobileaction", "toggle_view_mobile"]); - else - GETArguments.push(["lang", urlSplit[0]]); - if (urlSplit[1] == "m") - GETArguments.push(["mobileaction", "toggle_view_mobile"]); - // wikiless doesn't have mobile view support yet - } - for (let i = 0; i < GETArguments.length; i++) - link += (i == 0 ? "?" : "&") + GETArguments[i][0] + "=" + GETArguments[i][1]; + let GETArguments = []; + if (url.search.length > 0) { + let search = url.search.substring(1); //get rid of '?' + let argstrings = search.split("&"); + for (let i = 0; i < argstrings.length; i++) { + let args = argstrings[i].split("="); + GETArguments.push([args[0], args[1]]); + } + } + let instancesList; + if (r.wikipediaProtocol == 'normal') instancesList = [...r.wikilessNormalRedirectsChecks, ...r.wikilessNormalCustomRedirects]; + else if (r.wikipediaProtocol == 'tor') instancesList = [...r.wikilessTorRedirectsChecks, ...r.wikilessTorCustomRedirects]; + else if (r.wikipediaProtocol == 'i2p') instancesList = [...r.wikilessI2pRedirectsChecks, ...r.wikilessI2pCustomRedirects]; + if (instancesList.length === 0) { resolve(); return; } + let randomInstance = utils.getRandomInstance(instancesList) - return link; + let link = `${randomInstance}${url.pathname}`; + let urlSplit = url.host.split("."); + if (urlSplit[0] != "wikipedia" && urlSplit[0] != "www") { + if (urlSplit[0] == "m") + GETArguments.push(["mobileaction", "toggle_view_mobile"]); + else + GETArguments.push(["lang", urlSplit[0]]); + if (urlSplit[1] == "m") + GETArguments.push(["mobileaction", "toggle_view_mobile"]); + // wikiless doesn't have mobile view support yet + } + for (let i = 0; i < GETArguments.length; i++) + link += (i == 0 ? "?" : "&") + GETArguments[i][0] + "=" + GETArguments[i][1]; + + resolve(link); + } + ) + }) } function switchInstance(url) { - let protocolHost = commonHelper.protocolHost(url); + return new Promise(resolve => { + browser.storage.local.get( + [ + "wikipediaRedirects", + "wikipediaProtocol", - let wikipediaList = [ - ...redirects.wikiless.normal, - ...redirects.wikiless.tor, - ...redirects.wikiless.i2p, + "wikilessNormalRedirectsChecks", + "wikilessTorRedirectsChecks", - ...wikilessNormalCustomRedirects, - ...wikilessTorCustomRedirects, - ...wikilessI2pCustomRedirects - ] - if (!wikipediaList.includes(protocolHost)) return null; + "wikilessI2pRedirectsChecks", + "wikilessNormalCustomRedirects", - let instancesList; - if (protocol == 'normal') instancesList = [...wikilessNormalCustomRedirects, ...wikilessNormalRedirectsChecks]; - else if (protocol == 'tor') instancesList = [...wikilessTorCustomRedirects, ...wikilessTorRedirectsChecks]; - else if (protocol == 'i2p') instancesList = [...wikilessI2pCustomRedirects, ...wikilessI2pRedirectsChecks]; + "wikilessTorCustomRedirects", + "wikilessI2pCustomRedirects", + ], + r => { + let protocolHost = utils.protocolHost(url); - let index = instancesList.indexOf(protocolHost); - if (index > -1) instancesList.splice(index, 1); + let wikipediaList = [ + ...r.wikipediaRedirects.wikiless.normal, + ...r.wikipediaRedirects.wikiless.tor, + ...r.wikipediaRedirects.wikiless.i2p, - if (instancesList.length === 0) return null; + ...r.wikilessNormalCustomRedirects, + ...r.wikilessTorCustomRedirects, + ...r.wikilessI2pCustomRedirects + ] + if (!wikipediaList.includes(protocolHost)) resolve(); - let randomInstance = commonHelper.getRandomInstance(instancesList); - return `${randomInstance}${url.pathname}${url.search}`; + let instancesList; + if (r.wikipediaProtocol == 'normal') instancesList = [...r.wikilessNormalCustomRedirects, ...r.wikilessNormalRedirectsChecks]; + else if (r.wikipediaProtocol == 'tor') instancesList = [...r.wikilessTorCustomRedirects, ...r.wikilessTorRedirectsChecks]; + else if (r.wikipediaProtocol == 'i2p') instancesList = [...r.wikilessI2pCustomRedirects, ...r.wikilessI2pRedirectsChecks]; + + 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() { @@ -201,37 +237,6 @@ async function initDefaults() { }) } -async function init() { - browser.storage.local.get( - [ - "disableWikipedia", - "wikipediaRedirects", - "wikipediaProtocol", - - "wikilessNormalRedirectsChecks", - "wikilessTorRedirectsChecks", - "wikilessI2pRedirectsChecks", - "wikilessNormalCustomRedirects", - "wikilessTorCustomRedirects", - "wikilessI2pCustomRedirects", - ], - r => { - disable = r.disableWikipedia; - protocol = r.wikipediaProtocol; - redirects = r.wikipediaRedirects; - - wikilessNormalRedirectsChecks = r.wikilessNormalRedirectsChecks; - wikilessNormalCustomRedirects = r.wikilessNormalCustomRedirects; - - wikilessTorRedirectsChecks = r.wikilessTorRedirectsChecks; - wikilessTorCustomRedirects = r.wikilessTorCustomRedirects; - - wikilessI2pRedirectsChecks = r.wikilessI2pRedirectsChecks; - wikilessI2pCustomRedirects = r.wikilessI2pCustomRedirects; - } - ); -} - export default { getRedirects, setRedirects, @@ -241,6 +246,5 @@ export default { redirect, initDefaults, - init, switchInstance, }; diff --git a/src/assets/javascripts/helpers/youtube/youtube.js b/src/assets/javascripts/helpers/youtube/youtube.js index 31cc444..27cc539 100644 --- a/src/assets/javascripts/helpers/youtube/youtube.js +++ b/src/assets/javascripts/helpers/youtube/youtube.js @@ -2,7 +2,7 @@ window.browser = window.browser || window.chrome; -import commonHelper from '../common.js' +import utils from '../utils.js' const targets = [ /^https?:\/{2}(www\.|music\.|m\.|)youtube\.com(\/.*|$)/, @@ -34,7 +34,32 @@ let redirects = { } }; -const getRedirects = () => redirects; +function setRedirects(val) { + browser.storage.local.get('cloudflareList', async r => { + redirects.invidious = val.invidious; + redirects.piped = val.piped; + invidiousNormalRedirectsChecks = [...redirects.invidious.normal]; + pipedNormalRedirectsChecks = [...redirects.piped.normal]; + + for (const instance of r.cloudflareList) { + let i; + + i = invidiousNormalRedirectsChecks.indexOf(instance); + if (i > -1) invidiousNormalRedirectsChecks.splice(i, 1); + + i = pipedNormalRedirectsChecks.indexOf(instance); + if (i > -1) pipedNormalRedirectsChecks.splice(i, 1); + } + + browser.storage.local.set({ + youtubeRedirects: redirects, + invidiousNormalRedirectsChecks, + invidiousTorRedirectsChecks: redirects.invidious.tor, + pipedNormalRedirectsChecks, + pipedTorRedirectsChecks: redirects.piped.tor, + }) + }) +} let invidiousNormalRedirectsChecks, @@ -54,226 +79,238 @@ let pipedMaterialTorRedirectsChecks, pipedMaterialTorCustomRedirects; -let - disable, - protocol, - OnlyEmbeddedVideo, - frontend, - youtubeEmbedFrontend; - function redirect(url, details, initiator) { - if (disable) return null; - - let protocolHost = commonHelper.protocolHost(url); - - if (!targets.some(rx => rx.test(url.href))) return null; - - if ( - initiator && ( + return new Promise(resolve => { + browser.storage.local.get( [ - ...redirects.invidious.normal, - ...invidiousNormalCustomRedirects, - ...redirects.invidious.tor, - ...invidiousTorCustomRedirects, + "disableYoutube", + "OnlyEmbeddedVideo", + "youtubeFrontend", + "youtubeProtocol", + "youtubeEmbedFrontend", - ...redirects.piped.normal, - ...redirects.piped.tor, - ...pipedNormalCustomRedirects, - ...pipedTorCustomRedirects - ].includes(initiator.origin) + "youtubeRedirects", + + "invidiousNormalRedirectsChecks", + "invidiousNormalCustomRedirects", + + "invidiousTorRedirectsChecks", + "invidiousTorCustomRedirects", + + "pipedNormalRedirectsChecks", + "pipedNormalCustomRedirects", + + "pipedTorRedirectsChecks", + "pipedTorCustomRedirects", + + "pipedMaterialNormalRedirectsChecks", + "pipedMaterialNormalCustomRedirects", + + "pipedMaterialTorRedirectsChecks", + "pipedMaterialTorCustomRedirects", + ], + r => { + if (r.disableYoutube) { resolve(); return; } + if (!targets.some(rx => rx.test(url.href))) { resolve(); return; } + + if ( + initiator && ( + [ + ...r.youtubeRedirects.invidious.normal, + ...r.invidiousNormalCustomRedirects, + ...r.youtubeRedirects.invidious.tor, + ...r.invidiousTorCustomRedirects, + + ...r.youtubeRedirects.piped.normal, + ...r.youtubeRedirects.piped.tor, + ...r.pipedNormalCustomRedirects, + ...r.pipedTorCustomRedirects + ].includes(initiator.origin) + ) + ) { resolve('BYPASSTAB'); return; } + + if (url.pathname.match(/iframe_api/) || url.pathname.match(/www-widgetapi/)) { resolve(); return; } // Don't redirect YouTube Player API. + + if (r.youtubeFrontend == 'yatte' && details.type === "main_frame") + resolve(url.href.replace(/^https?:\/{2}/, 'yattee://')); + + else if (r.youtubeFrontend == 'freetube' && details.type === "main_frame") + resolve(`freetube://https:${url.pathname}${url.search}`); + + else if (r.youtubeFrontend == 'freetube' && details.type !== "main_frame" && r.youtubeEmbedFrontend == "youtube") + resolve(); + + else if ( + r.youtubeFrontend == 'invidious' || + ((r.youtubeFrontend == 'freetube' || r.youtubeFrontend == 'yatte') && r.youtubeEmbedFrontend == 'invidious' && details.type == "sub_frame") + ) { + + if (r.OnlyEmbeddedVideo == 'onlyEmbedded' && details.type !== "sub_frame") { resolve(); return; } + if ( + r.OnlyEmbeddedVideo == 'onlyNotEmbedded' && details.type !== "main_frame" && + !((r.youtubeFrontend == 'freetube' || r.youtubeFrontend == 'yatte') && r.youtubeEmbedFrontend == 'invidious' && details.type === "sub_frame") + ) { resolve(); return; } + + let instancesList; + if (r.youtubeProtocol == 'normal') instancesList = [...r.invidiousNormalRedirectsChecks, ...r.invidiousNormalCustomRedirects]; + else if (r.youtubeProtocol == 'tor') instancesList = [...r.invidiousTorRedirectsChecks, ...r.invidiousTorCustomRedirects]; + if (instancesList.length === 0) { resolve(); return; } + let randomInstance = utils.getRandomInstance(instancesList); + + resolve(`${randomInstance}${url.pathname}${url.search}`); + + } else if ( + r.youtubeFrontend == 'piped' || + ((r.youtubeFrontend == 'freetube' || r.youtubeFrontend == 'yatte') && r.youtubeEmbedFrontend == 'piped' && details.type === "sub_frame") + ) { + + if (r.OnlyEmbeddedVideo == 'onlyEmbedded' && details.type !== "sub_frame") { resolve(); return; } + if ( + r.OnlyEmbeddedVideo == 'onlyNotEmbedded' && details.type !== "main_frame" && + !((r.youtubeFrontend == 'freetube' || r.youtubeFrontend == 'yatte') && r.youtubeEmbedFrontend == 'piped' && details.type == "sub_frame") + ) { resolve(); return; } + + let instancesList; + if (r.youtubeProtocol == 'normal') instancesList = [...r.pipedNormalRedirectsChecks, ...r.pipedNormalCustomRedirects]; + else if (r.youtubeProtocol == 'tor') instancesList = [...r.pipedTorRedirectsChecks, ...r.pipedTorCustomRedirects]; + if (instancesList.length === 0) { resolve(); return; } + let randomInstance = utils.getRandomInstance(instancesList); + + resolve(`${randomInstance}${url.pathname}${url.search}`) + } + else if (r.youtubeFrontend == 'pipedMaterial' || + ((r.youtubeFrontend == 'freetube' || r.youtubeFrontend == 'yatte') && r.youtubeEmbedFrontend == 'pipedMaterial' && details.type === "sub_frame")) { + if (r.OnlyEmbeddedVideo == 'onlyEmbedded' && details.type !== "sub_frame") { resolve(); return; } + if ( + r.OnlyEmbeddedVideo == 'onlyNotEmbedded' && details.type !== "main_frame" && + !((r.youtubeFrontend == 'freetube' || r.youtubeFrontend == 'yatte') && r.youtubeEmbedFrontend == 'pipedMaterial' && details.type == "sub_frame") + ) { resolve(); return; } + + let instancesList; + if (r.youtubeProtocol == 'normal') instancesList = [...r.pipedMaterialNormalRedirectsChecks, ...r.pipedMaterialNormalCustomRedirects]; + else if (r.youtubeProtocol == 'tor') instancesList = [...r.pipedMaterialTorRedirectsChecks, ...r.pipedMaterialTorCustomRedirects]; + let randomInstance = utils.getRandomInstance(instancesList); + + resolve(`${randomInstance}${url.pathname}${url.search}`); + } + else resolve('CANCEL'); + } ) - ) return 'BYPASSTAB'; - - if (url.pathname.match(/iframe_api/) || url.pathname.match(/www-widgetapi/)) return null; // Don't redirect YouTube Player API. - - if (frontend == 'yatte' && details.type === "main_frame") - return url.href.replace(/^https?:\/{2}/, 'yattee://'); - - else if (frontend == 'freetube' && details.type === "main_frame") { - return `freetube://https:${url.pathname}${url.search}`; - } - - else if (frontend == 'freetube' && details.type !== "main_frame" && youtubeEmbedFrontend == "youtube") - return null; - - else if ( - frontend == 'invidious' || - ((frontend == 'freetube' || frontend == 'yatte') && youtubeEmbedFrontend == 'invidious' && details.type == "sub_frame") - ) { - - if (OnlyEmbeddedVideo == 'onlyEmbedded' && details.type !== "sub_frame") return null; - if ( - OnlyEmbeddedVideo == 'onlyNotEmbedded' && details.type !== "main_frame" && - !((frontend == 'freetube' || frontend == 'yatte') && youtubeEmbedFrontend == 'invidious' && details.type === "sub_frame") - ) return null; - - let instancesList; - if (protocol == 'normal') instancesList = [...invidiousNormalRedirectsChecks, ...invidiousNormalCustomRedirects]; - else if (protocol == 'tor') instancesList = [...invidiousTorRedirectsChecks, ...invidiousTorCustomRedirects]; - if (instancesList.length === 0) return null; - let randomInstance = commonHelper.getRandomInstance(instancesList); - - return `${randomInstance}${url.pathname}${url.search}`; - - } else if ( - frontend == 'piped' || - ((frontend == 'freetube' || frontend == 'yatte') && youtubeEmbedFrontend == 'piped' && details.type === "sub_frame") - ) { - - if (OnlyEmbeddedVideo == 'onlyEmbedded' && details.type !== "sub_frame") return null; - if ( - OnlyEmbeddedVideo == 'onlyNotEmbedded' && details.type !== "main_frame" && - !((frontend == 'freetube' || frontend == 'yatte') && youtubeEmbedFrontend == 'piped' && details.type == "sub_frame") - ) return null; - - let instancesList; - if (protocol == 'normal') instancesList = [...pipedNormalRedirectsChecks, ...pipedNormalCustomRedirects]; - else if (protocol == 'tor') instancesList = [...pipedTorRedirectsChecks, ...pipedTorCustomRedirects]; - if (instancesList.length === 0) return null; - let randomInstance = commonHelper.getRandomInstance(instancesList); - - return `${randomInstance}${url.pathname}${url.search}`; - } - else if (frontend == 'pipedMaterial' || - ((frontend == 'freetube' || frontend == 'yatte') && youtubeEmbedFrontend == 'pipedMaterial' && details.type === "sub_frame")) { - if (OnlyEmbeddedVideo == 'onlyEmbedded' && details.type !== "sub_frame") return null; - if ( - OnlyEmbeddedVideo == 'onlyNotEmbedded' && details.type !== "main_frame" && - !((frontend == 'freetube' || frontend == 'yatte') && youtubeEmbedFrontend == 'pipedMaterial' && details.type == "sub_frame") - ) return null; - - let instancesList; - if (protocol == 'normal') instancesList = [...pipedMaterialNormalRedirectsChecks, ...pipedMaterialNormalCustomRedirects]; - else if (protocol == 'tor') instancesList = [...pipedMaterialTorRedirectsChecks, ...pipedMaterialTorCustomRedirects]; - let randomInstance = commonHelper.getRandomInstance(instancesList); - - return `${randomInstance}${url.pathname}${url.search}`; - } - return 'CANCEL'; + }) } function reverse(url) { - browser.storage.local.get( - [ - "youtubeRedirects", - "invidiousNormalCustomRedirects", - "invidiousTorCustomRedirects", - "pipedNormalCustomRedirects", - "pipedTorCustomRedirects", - ], - r => { - let protocolHost = commonHelper.protocolHost(url); - if (![ - ...r.youtubeRedirects.invidious.normal, - ...r.youtubeRedirects.invidious.tor, + return new Promise(resolve => { + browser.storage.local.get( + [ + "youtubeRedirects", + "invidiousNormalCustomRedirects", + "invidiousTorCustomRedirects", + "pipedNormalCustomRedirects", + "pipedTorCustomRedirects", + "pipedMaterialNormalCustomRedirects", + "pipedMaterialTorCustomRedirects", + ], + r => { + let protocolHost = utils.protocolHost(url); + if (![ + ...r.youtubeRedirects.invidious.normal, + ...r.youtubeRedirects.invidious.tor, - ...r.youtubeRedirects.piped.normal, - ...r.youtubeRedirects.piped.tor, + ...r.youtubeRedirects.piped.normal, + ...r.youtubeRedirects.piped.tor, - ...r.invidiousNormalCustomRedirects, - ...r.invidiousTorCustomRedirects, + ...r.youtubeRedirects.pipedMaterial.normal, + ...r.youtubeRedirects.pipedMaterial.tor, - ...r.pipedNormalCustomRedirects, - ...r.pipedTorCustomRedirects, - ].includes(protocolHost)) return; + ...r.invidiousNormalCustomRedirects, + ...r.invidiousTorCustomRedirects, - return `https://youtube.com${url.pathname}${url.search}`; - }) + ...r.pipedNormalCustomRedirects, + ...r.pipedTorCustomRedirects, + + ...r.pipedMaterialNormalCustomRedirects, + ...r.pipedMaterialTorCustomRedirects, + ].includes(protocolHost)) { resolve(); return; } + + resolve(`https://youtube.com${url.pathname}${url.search}`); + }) + }) } function switchInstance(url) { - let protocolHost = commonHelper.protocolHost(url); - if ( - protocol == 'normal' && - ![ - ...redirects.invidious.normal, - ...redirects.piped.normal, - ...redirects.pipedMaterial.normal, + return new Promise(resolve => { + browser.storage.local.get( + [ + "youtubeRedirects", + "youtubeFrontend", + "youtubeProtocol", - ...invidiousNormalCustomRedirects, - ...pipedNormalCustomRedirects, - ...pipedMaterialNormalCustomRedirects - ].includes(protocolHost) - ) return null; + "invidiousNormalRedirectsChecks", + "invidiousNormalCustomRedirects", - if (protocol == 'tor' && - ![ - ...redirects.invidious.tor, - ...redirects.piped.tor, - ...redirects.pipedMaterial.tor, + "invidiousTorRedirectsChecks", + "invidiousTorCustomRedirects", - ...invidiousTorCustomRedirects, - ...pipedTorCustomRedirects, - ...pipedMaterialTorCustomRedirects - ].includes(protocolHost) - ) return null; + "pipedNormalRedirectsChecks", + "pipedNormalCustomRedirects", - let instancesList; - if (frontend == 'invidious') { - if (protocol == 'normal') instancesList = [...invidiousNormalRedirectsChecks, ...invidiousNormalCustomRedirects]; - else if (protocol == 'tor') instancesList = [...invidiousTorRedirectsChecks, ...invidiousTorCustomRedirects]; - } - else if (frontend == 'piped') { - if (protocol == 'normal') instancesList = [...pipedNormalRedirectsChecks, ...pipedNormalCustomRedirects]; - else if (protocol == 'tor') instancesList = [...pipedTorRedirectsChecks, ...pipedTorCustomRedirects]; - } - else if (frontend == 'pipedMaterial') { - if (protocol == 'normal') instancesList = [...pipedMaterialNormalRedirectsChecks, ...pipedMaterialNormalCustomRedirects]; - else if (protocol == 'tor') instancesList = [...pipedMaterialTorRedirectsChecks, ...pipedMaterialTorCustomRedirects]; - } + "pipedTorRedirectsChecks", + "pipedTorCustomRedirects", - let index = instancesList.indexOf(protocolHost); - if (index > -1) instancesList.splice(index, 1); + "pipedMaterialNormalRedirectsChecks", + "pipedMaterialNormalCustomRedirects", - if (instancesList.length == 0) return null; - let randomInstance = commonHelper.getRandomInstance(instancesList); - return `${randomInstance}${url.pathname}${url.search}`; + "pipedMaterialTorRedirectsChecks", + "pipedMaterialTorCustomRedirects", + ], + r => { + let protocolHost = utils.protocolHost(url); + if (![ + ...r.youtubeRedirects.invidious.normal, + ...r.youtubeRedirects.invidious.tor, + + ...r.youtubeRedirects.piped.normal, + ...r.youtubeRedirects.piped.tor, + + ...r.youtubeRedirects.pipedMaterial.normal, + ...r.youtubeRedirects.pipedMaterial.tor, + + ...r.invidiousNormalCustomRedirects, + ...r.invidiousTorCustomRedirects, + + ...r.pipedNormalCustomRedirects, + ...r.pipedTorCustomRedirects, + + ...r.pipedMaterialNormalCustomRedirects, + ...r.pipedMaterialTorCustomRedirects + ].includes(protocolHost)) { resolve(); return; } + + + let instancesList; + if (r.youtubeProtocol == 'normal') { + if (r.youtubeFrontend == 'invidious') instancesList = [...r.invidiousNormalRedirectsChecks, ...r.invidiousNormalCustomRedirects]; + else if (r.youtubeFrontend == 'piped') instancesList = [...r.pipedNormalRedirectsChecks, ...r.pipedNormalCustomRedirects]; + else if (r.youtubeFrontend == 'pipedMaterial') instancesList = [...r.pipedMaterialNormalRedirectsChecks, ...r.pipedMaterialNormalCustomRedirects]; + } + else if (r.youtubeProtocol == 'tor') { + if (r.youtubeFrontend == 'invidious') instancesList = [...r.invidiousTorRedirectsChecks, ...r.invidiousTorCustomRedirects]; + else if (r.youtubeFrontend == 'piped') instancesList = [...r.pipedTorRedirectsChecks, ...r.pipedTorCustomRedirects]; + else if (r.youtubeFrontend == 'pipedMaterial') instancesList = [...r.pipedMaterialTorRedirectsChecks, ...r.pipedMaterialTorCustomRedirects]; + } + + let index = instancesList.indexOf(protocolHost); + if (index > -1) instancesList.splice(index, 1); + if (instancesList.length == 0) { resolve(); return; } + + let randomInstance = utils.getRandomInstance(instancesList); + resolve(`${randomInstance}${url.pathname}${url.search}`); + } + ) + }) } -function isPipedorInvidious(url, type, frontend) { - init(); - let protocolHost = commonHelper.protocolHost(url); - - if (type !== "main_frame" && type !== "sub_frame") return false; - - if (frontend == 'invidious') - return [ - ...redirects.invidious.normal, - ...redirects.invidious.tor, - ...invidiousNormalCustomRedirects, - ...invidiousTorCustomRedirects, - ].includes(protocolHost); - - if (frontend == 'piped') - return [ - ...redirects.piped.normal, - ...redirects.piped.tor, - ...pipedNormalCustomRedirects, - ...pipedTorCustomRedirects, - ].includes(protocolHost); - - if (frontend == 'pipedMaterial') - return [ - ...redirects.pipedMaterial.normal, - ...redirects.pipedMaterial.tor, - ...pipedMaterialNormalCustomRedirects, - ...pipedMaterialTorCustomRedirects, - ].includes(protocolHost); - - return [ - ...redirects.invidious.normal, - ...redirects.invidious.tor, - ...invidiousNormalCustomRedirects, - ...invidiousTorCustomRedirects, - - ...redirects.piped.normal, - ...redirects.piped.tor, - ...pipedNormalCustomRedirects, - ...pipedTorCustomRedirects, - ].includes(protocolHost); -} - -async function initDefaults() { +function initDefaults() { return new Promise(async resolve => { fetch('/instances/data.json').then(response => response.text()).then(async data => { let dataJson = JSON.parse(data); @@ -334,71 +371,7 @@ async function initDefaults() { }) } -async function init() { - return new Promise( - resolve => { - browser.storage.local.get( - [ - "disableYoutube", - "OnlyEmbeddedVideo", - "youtubeRedirects", - "youtubeFrontend", - - "invidiousNormalRedirectsChecks", - "invidiousNormalCustomRedirects", - - "invidiousTorRedirectsChecks", - "invidiousTorCustomRedirects", - - "pipedNormalRedirectsChecks", - "pipedNormalCustomRedirects", - - "pipedTorRedirectsChecks", - "pipedTorCustomRedirects", - - "pipedMaterialNormalRedirectsChecks", - "pipedMaterialNormalCustomRedirects", - - "pipedMaterialTorRedirectsChecks", - "pipedMaterialTorCustomRedirects", - - "youtubeEmbedFrontend", - "youtubeProtocol", - ], - r => { - redirects = r.youtubeRedirects; - - disable = r.disableYoutube; - protocol = r.youtubeProtocol; - frontend = r.youtubeFrontend; - - invidiousNormalRedirectsChecks = r.invidiousNormalRedirectsChecks; - invidiousNormalCustomRedirects = r.invidiousNormalCustomRedirects; - - invidiousTorRedirectsChecks = r.invidiousTorRedirectsChecks; - invidiousTorCustomRedirects = r.invidiousTorCustomRedirects; - - pipedNormalRedirectsChecks = r.pipedNormalRedirectsChecks; - pipedNormalCustomRedirects = r.pipedNormalCustomRedirects; - - pipedTorRedirectsChecks = r.pipedTorRedirectsChecks; - pipedTorCustomRedirects = r.pipedTorCustomRedirects; - - pipedMaterialNormalRedirectsChecks = r.pipedMaterialNormalRedirectsChecks; - pipedMaterialNormalCustomRedirects = r.pipedMaterialNormalCustomRedirects; - - pipedMaterialTorRedirectsChecks = r.pipedMaterialTorRedirectsChecks; - pipedMaterialTorCustomRedirects = r.pipedMaterialTorCustomRedirects; - - youtubeEmbedFrontend = r.youtubeEmbedFrontend; - OnlyEmbeddedVideo = r.OnlyEmbeddedVideo; - - resolve(); - }); - }) -} - -async function initInvidiousCookies(from) { +function initInvidiousCookies(from) { return new Promise(resolve => { browser.storage.local.get( [ @@ -409,18 +382,18 @@ async function initInvidiousCookies(from) { "invidiousTorCustomRedirects", ], r => { - let protocolHost = commonHelper.protocolHost(from); + let protocolHost = utils.protocolHost(from); if (![ ...r.invidiousNormalRedirectsChecks, ...r.invidiousTorRedirectsChecks, ...r.invidiousNormalCustomRedirects, ...r.invidiousTorCustomRedirects, - ].includes(protocolHost)) return; + ].includes(protocolHost)) { resolve(); return; } let checkedInstances; if (r.youtubeProtocol == 'normal') checkedInstances = [...r.invidiousNormalRedirectsChecks, ...r.invidiousNormalCustomRedirects] else if (r.youtubeProtocol == 'tor') checkedInstances = [...r.invidiousTorRedirectsChecks, ...r.invidiousTorCustomRedirects] for (const to of checkedInstances) - commonHelper.copyCookie('invidious', from, to, 'PREFS'); + utils.copyCookie('invidious', from, to, 'PREFS'); resolve(true); } ) @@ -444,12 +417,12 @@ function setInvidiousCookies() { if (r.youtubeProtocol == 'normal') checkedInstances = [...r.invidiousNormalRedirectsChecks, ...r.invidiousNormalCustomRedirects] else if (r.youtubeProtocol == 'tor') checkedInstances = [...r.invidiousTorRedirectsChecks, ...r.invidiousTorCustomRedirects] for (const to of checkedInstances) - commonHelper.getCookiesFromStorage('invidious', to, 'PREFS'); + utils.getCookiesFromStorage('invidious', to, 'PREFS'); } ) } -async function initPipedLocalStorage(url, tabId) { +function initPipedLocalStorage(url, tabId) { return new Promise(resolve => { browser.storage.local.get( [ @@ -459,13 +432,13 @@ async function initPipedLocalStorage(url, tabId) { "pipedTorCustomRedirects", ], r => { - let protocolHost = commonHelper.protocolHost(url); + let protocolHost = utils.protocolHost(url); if (![ ...r.pipedNormalCustomRedirects, ...r.pipedNormalRedirectsChecks, ...r.pipedTorRedirectsChecks, ...r.pipedTorCustomRedirects, - ].includes(protocolHost)) resolve(); + ].includes(protocolHost)) { resolve(); return; } browser.tabs.executeScript( tabId, { @@ -479,38 +452,40 @@ async function initPipedLocalStorage(url, tabId) { }) } -async function setPipedLocalStorage(url, tabId) { - browser.storage.local.get( - [ - "disableYoutube", - "youtubeFrontend", - "pipedNormalRedirectsChecks", - "pipedNormalCustomRedirects", - "pipedTorRedirectsChecks", - "pipedTorCustomRedirects", - ], - r => { - if (!r.disableYoutube && r.youtubeFrontend == 'pipedMaterial') return; - let protocolHost = commonHelper.protocolHost(url); - if (![ - ...r.pipedNormalRedirectsChecks, - ...r.pipedTorRedirectsChecks, - ...r.pipedNormalCustomRedirects, - ...r.pipedTorCustomRedirects, - ].includes(protocolHost)) return; - browser.tabs.executeScript( - tabId, - { - file: "/assets/javascripts/helpers/youtube/set_piped_preferences.js", - runAt: "document_start" - } - ); - return true; - } - ) +function setPipedLocalStorage(url, tabId) { + return new Promise(resolve => { + browser.storage.local.get( + [ + "disableYoutube", + "youtubeFrontend", + "pipedNormalRedirectsChecks", + "pipedNormalCustomRedirects", + "pipedTorRedirectsChecks", + "pipedTorCustomRedirects", + ], + r => { + if (!r.disableYoutube && r.youtubeFrontend == 'pipedMaterial') { resolve(); return; } + let protocolHost = utils.protocolHost(url); + if (![ + ...r.pipedNormalRedirectsChecks, + ...r.pipedTorRedirectsChecks, + ...r.pipedNormalCustomRedirects, + ...r.pipedTorCustomRedirects, + ].includes(protocolHost)) { resolve(); return; } + browser.tabs.executeScript( + tabId, + { + file: "/assets/javascripts/helpers/youtube/set_piped_preferences.js", + runAt: "document_start" + } + ); + resolve(true); + } + ) + }) } -async function initPipedMaterialLocalStorage(tabId) { +function initPipedMaterialLocalStorage(tabId) { return new Promise(resolve => { browser.storage.local.get( [ @@ -520,13 +495,13 @@ async function initPipedMaterialLocalStorage(tabId) { "pipedMaterialTorCustomRedirects", ], r => { - const protocolHost = commonHelper.protocolHost(url); + const protocolHost = utils.protocolHost(url); if (![ ...r.pipedMaterialNormalCustomRedirects, ...r.pipedMaterialNormalRedirectsChecks, ...r.pipedMaterialTorRedirectsChecks, ...r.pipedMaterialTorCustomRedirects, - ].includes(protocolHost)) return; + ].includes(protocolHost)) { resolve(); return; } browser.tabs.executeScript( tabId, { @@ -540,79 +515,91 @@ async function initPipedMaterialLocalStorage(tabId) { }) } -async function setPipedMaterialLocalStorage(url, tabId) { - browser.storage.local.get( - [ - "disableYoutube", - "youtubeFrontend", - "pipedMaterialNormalRedirectsChecks", - "pipedMaterialTorRedirectsChecks", - "pipedMaterialNormalCustomRedirects", - "pipedMaterialTorCustomRedirects", - ], - r => { - if (r.disableYoutube || r.youtubeFrontend != 'pipedMaterial') return; - const protocolHost = commonHelper.protocolHost(url); - if (![ - ...r.pipedMaterialNormalRedirectsChecks, - ...r.pipedMaterialTorRedirectsChecks, - ...r.pipedMaterialNormalCustomRedirects, - ...r.pipedMaterialTorCustomRedirects, - ].includes(protocolHost)) return; - browser.tabs.executeScript( - tabId, - { - file: "/assets/javascripts/helpers/youtube/set_pipedMaterial_preferences.js", - runAt: "document_start" - } - ); - return true; - } - ) +function setPipedMaterialLocalStorage(url, tabId) { + return new Promise(resolve => { + browser.storage.local.get( + [ + "disableYoutube", + "youtubeFrontend", + "pipedMaterialNormalRedirectsChecks", + "pipedMaterialTorRedirectsChecks", + "pipedMaterialNormalCustomRedirects", + "pipedMaterialTorCustomRedirects", + ], + r => { + if (r.disableYoutube || r.youtubeFrontend != 'pipedMaterial') { resolve(); return; } + const protocolHost = utils.protocolHost(url); + if (![ + ...r.pipedMaterialNormalRedirectsChecks, + ...r.pipedMaterialTorRedirectsChecks, + ...r.pipedMaterialNormalCustomRedirects, + ...r.pipedMaterialTorCustomRedirects, + ].includes(protocolHost)) { resolve(); return; } + browser.tabs.executeScript( + tabId, + { + file: "/assets/javascripts/helpers/youtube/set_pipedMaterial_preferences.js", + runAt: "document_start" + } + ); + resolve(true); + } + ) + }) } function removeXFrameOptions(e) { - const url = new URL(e.url); - let protocolHost = commonHelper.protocolHost(url); - const list = [ - ...redirects.invidious.normal, - ...invidiousNormalCustomRedirects, - ...redirects.invidious.tor, - ...invidiousTorCustomRedirects, + return new Promise(resolve => { + browser.storage.local.get( + [ + "youtubeRedirects", + "pipedMaterialNormalRedirectsChecks", + "pipedMaterialTorRedirectsChecks", + "invidiousNormalCustomRedirects", + "invidiousTorCustomRedirects", + ], + r => { + const url = new URL(e.url); + let protocolHost = utils.protocolHost(url); + const list = [ + ...r.youtubeRedirects.invidious.normal, + ...r.youtubeRedirects.invidious.tor, + ...r.youtubeRedirects.piped.normal, + ...r.youtubeRedirects.piped.tor, - ...redirects.piped.normal, - ...redirects.piped.tor, - ...pipedNormalCustomRedirects, - ...pipedTorCustomRedirects - ]; - if (!list.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 }; + ...r.invidiousNormalCustomRedirects, + ...r.invidiousTorCustomRedirects, + + ...r.pipedNormalCustomRedirects, + ...r.pipedTorCustomRedirects + ]; + if (!list.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 }); + }) + }) } export default { + setRedirects, initPipedMaterialLocalStorage, setPipedLocalStorage, setPipedMaterialLocalStorage, initInvidiousCookies, setInvidiousCookies, - getRedirects, redirect, reverse, switchInstance, - isPipedorInvidious, - initPipedLocalStorage, initDefaults, - init, removeXFrameOptions, }; diff --git a/src/assets/javascripts/helpers/youtubeMusic.js b/src/assets/javascripts/helpers/youtubeMusic.js index 9854914..1b194c1 100644 --- a/src/assets/javascripts/helpers/youtubeMusic.js +++ b/src/assets/javascripts/helpers/youtubeMusic.js @@ -1,6 +1,6 @@ "use strict"; -import commonHelper from './common.js' +import utils from './utils.js' window.browser = window.browser || window.chrome; @@ -16,55 +16,61 @@ let redirects = { }, }; -const getRedirects = () => redirects; // youtubeMusicRedirects +function redirect(url, type, initiator) { + return new Promise(resolve => { + browser.storage.local.get( + [ + "disableYoutubeMusic", -let beatbumpNormalRedirectsChecks; -let beatbumpNormalCustomRedirects = []; -let disable; // disableYoutubeMusic + "beatbumpNormalRedirectsChecks", + "beatbumpNormalCustomRedirects", + ], + r => { + /* + Video + https://music.youtube.com/watch?v=_PkGiKBW-DA&list=RDAMVM_PkGiKBW-DA + https://beatbump.ml/listen?id=_PkGiKBW-DA&list=RDAMVM_PkGiKBW-DA -function isYoutubeMusic(url, initiator) { - if (disable) return false - return targets.some(rx => rx.test(url.href)); + Playlist + https://music.youtube.com/playlist?list=PLqxd0OMLeWy64zlwhjouj92ISc38FbOns + https://music.youtube.com/playlist?list=PLqxd0OMLeWy7lrJSzt9LnOJjbC1IaruPM + https://music.youtube.com/playlist?list=PLQod4DlD72ZMJmOrSNbmEmK_iZ1oXPzKd + https://beatbump.ml/playlist/VLPLqxd0OMLeWy64zlwhjouj92ISc38FbOns + + Channel + https://music.youtube.com/channel/UCfgmMDI7T5tOQqjnOBRe_wg + https://beatbump.ml/artist/UCfgmMDI7T5tOQqjnOBRe_wg + + Albums + https://music.youtube.com/playlist?list=OLAK5uy_n-9HVh3cryV2gREZM9Sc0JwEKYjjfi0dU + https://music.youtube.com/playlist?list=OLAK5uy_lcr5O1zS8f6WIFI_yxqVp2RK9Dyy2bbw0 + https://beatbump.ml/release?id=MPREb_3DURc4yEUtD + https://beatbump.ml/release?id=MPREb_evaZrV1WNdS + + https://music.youtube.com/playlist?list=OLAK5uy_n6OHVllUZUCnlIY1m-gUaH8uqkN3Y-Ca8 + https://music.youtube.com/playlist?list=OLAK5uy_nBOTxAc3_RGB82-Z54jdARGxGaCYlpngY + https://beatbump.ml/release?id=MPREb_QygdC0wEoLe + + https://music.youtube.com/watch?v=R6gSMSYKhKU&list=OLAK5uy_n-9HVh3cryV2gREZM9Sc0JwEKYjjfi0dU + */ + if (r.disableYoutubeMusic) { resolve(); return; } + if (!targets.some(rx => rx.test(url.href))) { resolve(); return; } + + let instancesList = [...r.beatbumpNormalRedirectsChecks, ...r.beatbumpNormalCustomRedirects]; + if (instancesList.length === 0) { resolve(); return; } + let randomInstance = utils.getRandomInstance(instancesList); + + resolve( + `${randomInstance}${url.pathname}${url.search}` + .replace("/watch?v=", "/listen?id=") + .replace("/channel/", "/artist/") + .replace("/playlist?list=", "/playlist/VL") + ); + } + ) + }) } -function redirect(url, type) { - // Video - // https://music.youtube.com/watch?v=_PkGiKBW-DA&list=RDAMVM_PkGiKBW-DA - // https://beatbump.ml/listen?id=_PkGiKBW-DA&list=RDAMVM_PkGiKBW-DA - - // Playlist - // https://music.youtube.com/playlist?list=PLqxd0OMLeWy64zlwhjouj92ISc38FbOns - // https://music.youtube.com/playlist?list=PLqxd0OMLeWy7lrJSzt9LnOJjbC1IaruPM - // https://music.youtube.com/playlist?list=PLQod4DlD72ZMJmOrSNbmEmK_iZ1oXPzKd - // https://beatbump.ml/playlist/VLPLqxd0OMLeWy64zlwhjouj92ISc38FbOns - - // Channel - // https://music.youtube.com/channel/UCfgmMDI7T5tOQqjnOBRe_wg - // https://beatbump.ml/artist/UCfgmMDI7T5tOQqjnOBRe_wg - - // Albums - // https://music.youtube.com/playlist?list=OLAK5uy_n-9HVh3cryV2gREZM9Sc0JwEKYjjfi0dU - // https://music.youtube.com/playlist?list=OLAK5uy_lcr5O1zS8f6WIFI_yxqVp2RK9Dyy2bbw0 - // https://beatbump.ml/release?id=MPREb_3DURc4yEUtD - // https://beatbump.ml/release?id=MPREb_evaZrV1WNdS - - // https://music.youtube.com/playlist?list=OLAK5uy_n6OHVllUZUCnlIY1m-gUaH8uqkN3Y-Ca8 - // https://music.youtube.com/playlist?list=OLAK5uy_nBOTxAc3_RGB82-Z54jdARGxGaCYlpngY - // https://beatbump.ml/release?id=MPREb_QygdC0wEoLe - - // https://music.youtube.com/watch?v=R6gSMSYKhKU&list=OLAK5uy_n-9HVh3cryV2gREZM9Sc0JwEKYjjfi0dU - - let instancesList = [...beatbumpNormalRedirectsChecks, ...beatbumpNormalCustomRedirects]; - if (instancesList.length === 0) return null; - let randomInstance = commonHelper.getRandomInstance(instancesList); - - return `${randomInstance}${url.pathname}${url.search}` - .replace("/watch?v=", "/listen?id=") - .replace("/channel/", "/artist/") - .replace("/playlist?list=", "/playlist/VL"); -} - - async function initDefaults() { await browser.storage.local.set({ disableYoutubeMusic: true, @@ -75,27 +81,7 @@ async function initDefaults() { }) } -async function init() { - browser.storage.local.get( - [ - "disableYoutubeMusic", - - "beatbumpNormalRedirectsChecks", - "beatbumpNormalCustomRedirects", - ], - r => { - disable = r.disableYoutubeMusic; - - beatbumpNormalRedirectsChecks = r.beatbumpNormalRedirectsChecks; - beatbumpNormalCustomRedirects = r.beatbumpNormalCustomRedirects; - }); -} - export default { - getRedirects, redirect, - isYoutubeMusic, - initDefaults, - init, }; diff --git a/src/manifest.json b/src/manifest.json index f826947..5101211 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -60,9 +60,21 @@ "default": "Alt+Shift+L" }, "description": "__MSG_switchInstance__" + }, + "copyRaw": { + "suggested_key": { + "default": "Alt+Shift+C" + }, + "description": "Copies the original link. Ex: Copies the original twitter link when in while nitter website" + }, + "unify": { + "suggested_key": { + "default": "Alt+Shift+U" + }, + "description": "Copies the preferences (cookies, localStorage) from the current opened instance and copy them to all the other selected instances" } }, "default_locale": "en", "update_url": "https://raw.githubusercontent.com/libredirect/libredirect/master/src/updates/updates.xml", "key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAroWDSoSRZ1scj+eJRrvnhJbrqXTKnhQuxs6+AJg16sqr0bsMdFV+MSY4i4xnK+K5WOYkBliWXgUyk/wzicoAjOnSJddrL/Md4FuWHI2NVIkrlsLOrYkygi5OLqGPajRH/w8Cdmg7KzEpXe/OnYV0/qS8li8huEdTzdeLdhfbiVl1j3DOr4OJALQ7mPeeNFHFo/oVQ+OkSezWLezA5jUGfhtzPYV6u1TXzX7lCi8E/BbDbwkvvXOMcjXCv08kjdLOY2djCA2a6zr0xAb3q8DlexAMZ8vMof7AQRFtBKhLc9n9VFoipMMdBOVQQj/eIcRILBrmkcZNnJxFKiHNJ+NcZQIDAQAB" -} +} \ No newline at end of file diff --git a/src/pages/background/background.js b/src/pages/background/background.js index d112e00..d8f0bb3 100644 --- a/src/pages/background/background.js +++ b/src/pages/background/background.js @@ -16,6 +16,7 @@ import tiktokHelper from "../../assets/javascripts/helpers/tiktok.js"; import sendTargetsHelper from "../../assets/javascripts/helpers/sendTargets.js"; import peertubeHelper from "../../assets/javascripts/helpers/peertube.js"; import lbryHelper from "../../assets/javascripts/helpers/lbry.js"; +import utils from "../../assets/javascripts/helpers/utils.js"; window.browser = window.browser || window.chrome; @@ -55,21 +56,9 @@ browser.runtime.onInstalled.addListener( ) async function wholeInit() { - await youtubeHelper.init(); - await youtubeMusicHelper.init(); - await twitterHelper.init(); - await instagramHelper.init(); await mapsHelper.init(); - await searchHelper.init(); - await translateHelper.init(); - await mediumHelper.init(); - await redditHelper.init(); - await wikipediaHelper.init(); - await imgurHelper.init(); - await tiktokHelper.init(); await sendTargetsHelper.init(); await peertubeHelper.init(); - await lbryHelper.init(); await generalHelper.init(); } @@ -97,22 +86,21 @@ browser.webRequest.onBeforeRequest.addListener( else if (details.initiator) initiator = new URL(details.initiator); - let newUrl = youtubeHelper.redirect(url, details, initiator) - if (youtubeMusicHelper.isYoutubeMusic(url, initiator)) newUrl = youtubeMusicHelper.redirect(url, details.type) - - if (!newUrl) newUrl = twitterHelper.redirect(url, initiator); - if (!newUrl) newUrl = instagramHelper.redirect(url, details.type, initiator); + let newUrl = await youtubeHelper.redirect(url, details, initiator) + if (!newUrl) newUrl = await youtubeMusicHelper.redirect(url, details.type) + if (!newUrl) newUrl = await twitterHelper.redirect(url, initiator); + if (!newUrl) newUrl = await instagramHelper.redirect(url, details.type, initiator); if (!newUrl) newUrl = await mapsHelper.redirect(url, initiator); - if (!newUrl) newUrl = redditHelper.redirect(url, details.type, initiator); - if (!newUrl) newUrl = mediumHelper.redirect(url, details.type, initiator); - if (!newUrl) newUrl = imgurHelper.redirect(url, details.type, initiator); - if (!newUrl) newUrl = tiktokHelper.redirect(url, details.type, initiator); - if (!newUrl) newUrl = sendTargetsHelper.redirect(url, details.type, initiator); - if (!newUrl) newUrl = peertubeHelper.redirect(url, details.type, initiator); - if (!newUrl) newUrl = lbryHelper.redirect(url, details.type, initiator); - if (!newUrl) newUrl = translateHelper.redirect(url); - if (!newUrl) newUrl = searchHelper.redirect(url) - if (!newUrl) newUrl = wikipediaHelper.redirect(url); + if (!newUrl) newUrl = await redditHelper.redirect(url, details.type, initiator); + if (!newUrl) newUrl = await mediumHelper.redirect(url, details.type, initiator); + if (!newUrl) newUrl = await imgurHelper.redirect(url, details.type, initiator); + if (!newUrl) newUrl = await tiktokHelper.redirect(url, details.type, initiator); + if (!newUrl) newUrl = await sendTargetsHelper.redirect(url, details.type, initiator); + if (!newUrl) newUrl = await peertubeHelper.redirect(url, details.type, initiator); + if (!newUrl) newUrl = await lbryHelper.redirect(url, details.type, initiator); + if (!newUrl) newUrl = await translateHelper.redirect(url); + if (!newUrl) newUrl = await searchHelper.redirect(url) + if (!newUrl) newUrl = await wikipediaHelper.redirect(url); if ( details.frameAncestors && details.frameAncestors.length > 0 && @@ -229,35 +217,13 @@ browser.tabs.onUpdated.addListener( } ); -async function changeWholeInstance(url) { - await wholeInit(); - let newUrl = youtubeHelper.switchInstance(url); - if (!newUrl) newUrl = twitterHelper.switchInstance(url); - if (!newUrl) newUrl = instagramHelper.switchInstance(url); - if (!newUrl) newUrl = redditHelper.switchInstance(url); - if (!newUrl) newUrl = searchHelper.switchInstance(url); - if (!newUrl) newUrl = translateHelper.switchInstance(url); - if (!newUrl) newUrl = mediumHelper.switchInstance(url); - if (!newUrl) newUrl = sendTargetsHelper.switchInstance(url); - if (!newUrl) newUrl = peertubeHelper.switchInstance(url); - if (!newUrl) newUrl = imgurHelper.switchInstance(url); - if (!newUrl) newUrl = wikipediaHelper.switchInstance(url); - return newUrl; -} + browser.commands.onCommand.addListener( command => { - if (command === 'switchInstance') - browser.tabs.query( - { active: true, currentWindow: true }, - tabs => { - let url; - try { url = new URL(tabs[0].url); } - catch (_) { return } - let newUrl = changeWholeInstance(url); - if (newUrl) browser.tabs.update({ url: newUrl }); - } - ); + if (command === 'switchInstance') utils.switchInstance(); + else if (command == 'copyRaw') utils.copyRaw(); + else if (command == 'unify') utils.unify(); } ) @@ -273,16 +239,24 @@ browser.contextMenus.create({ contexts: ["browser_action"] }); +browser.contextMenus.create({ + id: "copyRaw", + title: "Copy Raw", + contexts: ["browser_action"] +}); + +browser.contextMenus.create({ + id: "unify", + title: "Unify", + contexts: ["browser_action"] +}); + + browser.contextMenus.onClicked.addListener( - (info, tab) => { - if (info.menuItemId == 'switchInstance') { - let url; - try { url = new URL(tab.url); } - catch (_) { return } - let newUrl = changeWholeInstance(url); - if (newUrl) browser.tabs.update({ url: newUrl }); - } - else if (info.menuItemId == 'settings') - browser.runtime.openOptionsPage() + (info) => { + if (info.menuItemId == 'switchInstance') utils.switchInstance(); + else if (info.menuItemId == 'settings') browser.runtime.openOptionsPage() + else if (info.menuItemId == 'copyRaw') utils.copyRaw(); + else if (info.menuItemId == 'unify') utils.unify(); } ); diff --git a/src/pages/background/incognito.js b/src/pages/background/incognito.js index 6e02347..16eccf7 100644 --- a/src/pages/background/incognito.js +++ b/src/pages/background/incognito.js @@ -20,21 +20,10 @@ import youtubeMusicHelper from "../../assets/javascripts/helpers/youtubeMusic.js window.browser = window.browser || window.chrome; async function wholeInit() { - await youtubeHelper.init(); await youtubeMusicHelper.init(); - await twitterHelper.init(); - await instagramHelper.init(); await mapsHelper.init(); - await searchHelper.init(); - await translateHelper.init(); - await mediumHelper.init(); - await redditHelper.init(); - await wikipediaHelper.init(); - await imgurHelper.init(); - await tiktokHelper.init(); await sendTargetsHelper.init(); await peertubeHelper.init(); - await lbryHelper.init(); await generalHelper.init(); } await wholeInit(); diff --git a/src/pages/options/general/general.js b/src/pages/options/general/general.js index bcf7a52..c8a600c 100644 --- a/src/pages/options/general/general.js +++ b/src/pages/options/general/general.js @@ -1,7 +1,7 @@ "use strict"; window.browser = window.browser || window.chrome; -import commonHelper from "../../../assets/javascripts/helpers/common.js"; +import utils from "../../../assets/javascripts/helpers/utils.js"; import generalHelper from "../../../assets/javascripts/helpers/general.js"; @@ -10,7 +10,7 @@ let updateInstancesElement = document.getElementById("update-instances"); updateInstancesElement.addEventListener("click", () => { let oldHtml = updateInstancesElement.innerHTML updateInstancesElement.innerHTML = '...'; - if (commonHelper.updateInstances()) { + if (utils.updateInstances()) { updateInstancesElement.innerHTML = 'Done!'; new Promise(resolve => setTimeout(resolve, 1500)).then( // sleep 1500ms () => updateInstancesElement.innerHTML = oldHtml diff --git a/src/pages/options/imgur/imgur.js b/src/pages/options/imgur/imgur.js index 597d015..0b1343c 100644 --- a/src/pages/options/imgur/imgur.js +++ b/src/pages/options/imgur/imgur.js @@ -1,5 +1,5 @@ import imgurHelper from "../../../assets/javascripts/helpers/imgur.js"; -import commonHelper from "../../../assets/javascripts/helpers/common.js"; +import utils from "../../../assets/javascripts/helpers/utils.js"; let disableImgurElement = document.getElementById("disable-imgur"); let protocolElement = document.getElementById("protocol") @@ -45,26 +45,8 @@ browser.storage.local.get( } ); -commonHelper.processDefaultCustomInstances('imgur', 'rimgo', 'normal', document); -commonHelper.processDefaultCustomInstances('imgur', 'rimgo', 'tor', document); -commonHelper.processDefaultCustomInstances('imgur', 'rimgo', 'i2p', document); +utils.processDefaultCustomInstances('imgur', 'rimgo', 'normal', document); +utils.processDefaultCustomInstances('imgur', 'rimgo', 'tor', document); +utils.processDefaultCustomInstances('imgur', 'rimgo', 'i2p', document); - -let latencyElement = document.getElementById("latency"); -let latencyLabel = document.getElementById("latency-label"); -latencyElement.addEventListener("click", - async () => { - let reloadWindow = () => location.reload(); - latencyElement.addEventListener("click", reloadWindow); - await imgurHelper.init(); - let redirects = imgurHelper.getRedirects(); - const oldHtml = latencyLabel.innerHTML; - latencyLabel.innerHTML = '...'; - commonHelper.testLatency(latencyLabel, redirects.rimgo.normal).then(r => { - browser.storage.local.set({ rimgoLatency: r }); - latencyLabel.innerHTML = oldHtml; - commonHelper.processDefaultCustomInstances('imgur', 'rimgo', 'normal', document); - latencyElement.removeEventListener("click", reloadWindow) - }); - } -); +utils.latency('imgur', 'rimgo', document, location) \ No newline at end of file diff --git a/src/pages/options/init.js b/src/pages/options/init.js index 78ea11f..b9ff495 100644 --- a/src/pages/options/init.js +++ b/src/pages/options/init.js @@ -1,5 +1,5 @@ window.browser = window.browser || window.chrome; -import commonHelper from "../../assets/javascripts/helpers/common.js"; +import utils from "../../assets/javascripts/helpers/utils.js"; function changeTheme() { browser.storage.local.get( @@ -34,4 +34,4 @@ browser.storage.onChanged.addListener(changeTheme) window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", changeTheme) -if (commonHelper.isRtl()) document.getElementsByTagName("body")[0].classList.add("rtl"); \ No newline at end of file +if (utils.isRtl()) document.getElementsByTagName("body")[0].classList.add("rtl"); \ No newline at end of file diff --git a/src/pages/options/instagram/instagram.js b/src/pages/options/instagram/instagram.js index 39d1142..955aafc 100644 --- a/src/pages/options/instagram/instagram.js +++ b/src/pages/options/instagram/instagram.js @@ -1,5 +1,5 @@ import instagramHelper from "../../../assets/javascripts/helpers/instagram.js"; -import commonHelper from "../../../assets/javascripts/helpers/common.js"; +import utils from "../../../assets/javascripts/helpers/utils.js"; const disable = document.getElementById("disable-bibliogram"); const protocol = document.getElementById("protocol"); @@ -36,24 +36,7 @@ browser.storage.local.get( changeProtocolSettings(); }) -commonHelper.processDefaultCustomInstances('instagram', 'bibliogram', 'normal', document); -commonHelper.processDefaultCustomInstances('instagram', 'bibliogram', 'tor', document); +utils.processDefaultCustomInstances('instagram', 'bibliogram', 'normal', document); +utils.processDefaultCustomInstances('instagram', 'bibliogram', 'tor', document); -let latencyElement = document.getElementById("latency"); -let latencyLabel = document.getElementById("latency-label"); -latencyElement.addEventListener("click", - async () => { - let reloadWindow = () => location.reload(); - latencyElement.addEventListener("click", reloadWindow); - await instagramHelper.init(); - let redirects = instagramHelper.getRedirects(); - const oldHtml = latencyLabel.innerHTML; - latencyLabel.innerHTML = '...'; - commonHelper.testLatency(latencyLabel, redirects.bibliogram.normal).then(r => { - browser.storage.local.set({ bibliogramLatency: r }); - latencyLabel.innerHTML = oldHtml; - commonHelper.processDefaultCustomInstances('instagram', 'bibliogram', 'normal', document); - latencyElement.removeEventListener("click", reloadWindow); - }); - } -); \ No newline at end of file +utils.latency('instagram', 'bibliogram', document, location) \ No newline at end of file diff --git a/src/pages/options/lbry/lbry.js b/src/pages/options/lbry/lbry.js index fcac7bd..92cc21a 100644 --- a/src/pages/options/lbry/lbry.js +++ b/src/pages/options/lbry/lbry.js @@ -1,5 +1,5 @@ import lbryHelper from "../../../assets/javascripts/helpers/lbry.js"; -import commonHelper from "../../../assets/javascripts/helpers/common.js"; +import utils from "../../../assets/javascripts/helpers/utils.js"; let disable = document.getElementById("disable-lbry"); let protocol = document.getElementById("protocol") @@ -37,24 +37,7 @@ browser.storage.local.get( } ) -commonHelper.processDefaultCustomInstances('lbryTargets', 'librarian', 'normal', document); -commonHelper.processDefaultCustomInstances('lbryTargets', 'librarian', 'tor', document); +utils.processDefaultCustomInstances('lbryTargets', 'librarian', 'normal', document); +utils.processDefaultCustomInstances('lbryTargets', 'librarian', 'tor', document); -let latencyElement = document.getElementById("latency"); -let latencyLabel = document.getElementById("latency-label"); -latencyElement.addEventListener("click", - async () => { - let reloadWindow = () => location.reload(); - latencyElement.addEventListener("click", reloadWindow); - await lbryHelper.init(); - let redirects = lbryHelper.getRedirects(); - const oldHtml = latencyLabel.innerHTML; - latencyLabel.innerHTML = '...'; - commonHelper.testLatency(latencyLabel, redirects.librarian.normal).then(r => { - browser.storage.local.set({ librarianLatency: r }); - latencyLabel.innerHTML = oldHtml; - commonHelper.processDefaultCustomInstances('lbry', 'librarian', 'normal', document); - latencyElement.removeEventListener("click", reloadWindow); - }); - } -); \ No newline at end of file +utils.latency('lbryTargets', 'librarian', document, location) \ No newline at end of file diff --git a/src/pages/options/maps/maps.js b/src/pages/options/maps/maps.js index 7e18def..6db8c03 100644 --- a/src/pages/options/maps/maps.js +++ b/src/pages/options/maps/maps.js @@ -1,5 +1,5 @@ import mapsHelper from "../../../assets/javascripts/helpers/maps.js"; -import commonHelper from "../../../assets/javascripts/helpers/common.js"; +import utils from "../../../assets/javascripts/helpers/utils.js"; const disable = document.getElementById("disable-osm"); const frontend = document.getElementById("maps-frontend"); @@ -29,23 +29,4 @@ browser.storage.local.get( changeFrontendsSettings(); } ) -commonHelper.processDefaultCustomInstances('maps', 'facil', 'normal', document); - -let latencyElement = document.getElementById("latency"); -let latencyLabel = document.getElementById("latency-label"); -latencyElement.addEventListener("click", - async () => { - let reloadWindow = () => location.reload(); - latencyElement.addEventListener("click", reloadWindow); - await mapsHelper.init(); - let redirects = mapsHelper.getRedirects(); - const oldHtml = latencyLabel.innerHTML; - latencyLabel.innerHTML = '...'; - commonHelper.testLatency(latencyLabel, redirects.facil.normal).then(r => { - browser.storage.local.set({ facilLatency: r }); - latencyLabel.innerHTML = oldHtml; - commonHelper.processDefaultCustomInstances('maps', 'facil', 'normal', document); - latencyElement.removeEventListener("click", reloadWindow); - }); - } -); \ No newline at end of file +utils.processDefaultCustomInstances('maps', 'facil', 'normal', document); \ No newline at end of file diff --git a/src/pages/options/medium/medium.js b/src/pages/options/medium/medium.js index 786f8f1..9f00878 100644 --- a/src/pages/options/medium/medium.js +++ b/src/pages/options/medium/medium.js @@ -1,5 +1,5 @@ import mediumHelper from "../../../assets/javascripts/helpers/medium.js"; -import commonHelper from "../../../assets/javascripts/helpers/common.js"; +import utils from "../../../assets/javascripts/helpers/utils.js"; let disable = document.getElementById("disable-medium"); let protocol = document.getElementById("protocol") @@ -15,8 +15,8 @@ browser.storage.local.get( changeProtocolSettings(); } ) -commonHelper.processDefaultCustomInstances('medium', 'scribe', 'normal', document); -commonHelper.processDefaultCustomInstances('medium', 'scribe', 'tor', document); +utils.processDefaultCustomInstances('medium', 'scribe', 'normal', document); +utils.processDefaultCustomInstances('medium', 'scribe', 'tor', document); document.addEventListener("change", async () => { await browser.storage.local.set({ @@ -39,21 +39,4 @@ function changeProtocolSettings() { } } -let latencyElement = document.getElementById("latency"); -let latencyLabel = document.getElementById("latency-label"); -latencyElement.addEventListener("click", - async () => { - let reloadWindow = () => location.reload(); - latencyElement.addEventListener("click", reloadWindow); - await mediumHelper.init(); - let redirects = mediumHelper.getRedirects(); - const oldHtml = latencyLabel.innerHTML; - latencyLabel.innerHTML = '...'; - commonHelper.testLatency(latencyLabel, redirects.scribe.normal).then(r => { - browser.storage.local.set({ scribeLatency: r }); - latencyLabel.innerHTML = oldHtml; - commonHelper.processDefaultCustomInstances('medium', 'scribe', 'normal', document); - latencyElement.removeEventListener("click", reloadWindow); - }); - } -); \ No newline at end of file +utils.latency('medium', 'scribe', document, location) \ No newline at end of file diff --git a/src/pages/options/peertube/peertube.js b/src/pages/options/peertube/peertube.js index 818e3fe..0bf16cc 100644 --- a/src/pages/options/peertube/peertube.js +++ b/src/pages/options/peertube/peertube.js @@ -1,5 +1,5 @@ import peertubeHelper from "../../../assets/javascripts/helpers/peertube.js"; -import commonHelper from "../../../assets/javascripts/helpers/common.js"; +import utils from "../../../assets/javascripts/helpers/utils.js"; let disable = document.getElementById("disable-peertube"); let protocol = document.getElementById("protocol") @@ -14,8 +14,8 @@ browser.storage.local.get( changeProtocolSettings(); } ) -commonHelper.processDefaultCustomInstances('peertube', 'simpleertube', 'normal', document); -commonHelper.processDefaultCustomInstances('peertube', 'simpleertube', 'tor', document); +utils.processDefaultCustomInstances('peertube', 'simpleertube', 'normal', document); +utils.processDefaultCustomInstances('peertube', 'simpleertube', 'tor', document); document.addEventListener("change", async () => { await browser.storage.local.set({ @@ -37,22 +37,4 @@ function changeProtocolSettings() { torDiv.style.display = 'block'; } } - -let latencyElement = document.getElementById("latency"); -let latencyLabel = document.getElementById("latency-label"); -latencyElement.addEventListener("click", - async () => { - let reloadWindow = () => location.reload(); - latencyElement.addEventListener("click", reloadWindow); - await peertubeHelper.init(); - let redirects = peertubeHelper.getRedirects(); - const oldHtml = latencyLabel.innerHTML; - latencyLabel.innerHTML = '...'; - commonHelper.testLatency(latencyLabel, redirects.simpleertube.normal).then(r => { - browser.storage.local.set({ simpleertubeLatency: r }); - latencyLabel.innerHTML = oldHtml; - commonHelper.processDefaultCustomInstances('peertube', 'simpleertube', 'normal', document); - latencyElement.removeEventListener("click", reloadWindow); - }); - } -); \ No newline at end of file +utils.latency('peertube', 'simpleertube', document, location) \ No newline at end of file diff --git a/src/pages/options/reddit/reddit.js b/src/pages/options/reddit/reddit.js index 5239964..3661f73 100644 --- a/src/pages/options/reddit/reddit.js +++ b/src/pages/options/reddit/reddit.js @@ -1,5 +1,5 @@ import redditHelper from "../../../assets/javascripts/helpers/reddit.js"; -import commonHelper from "../../../assets/javascripts/helpers/common.js"; +import utils from "../../../assets/javascripts/helpers/utils.js"; let libredditDivElement = document.getElementById("libreddit") let tedditDivElement = document.getElementById("teddit") @@ -81,46 +81,10 @@ browser.storage.local.get( } ) -commonHelper.processDefaultCustomInstances('reddit', 'libreddit', 'normal', document); -commonHelper.processDefaultCustomInstances('reddit', 'libreddit', 'tor', document); -commonHelper.processDefaultCustomInstances('reddit', 'teddit', 'normal', document); -commonHelper.processDefaultCustomInstances('reddit', 'teddit', 'tor', document); +utils.processDefaultCustomInstances('reddit', 'libreddit', 'normal', document); +utils.processDefaultCustomInstances('reddit', 'libreddit', 'tor', document); +utils.processDefaultCustomInstances('reddit', 'teddit', 'normal', document); +utils.processDefaultCustomInstances('reddit', 'teddit', 'tor', document); - -let latencyLibredditElement = document.getElementById("latency-libreddit"); -let latencyLibredditLabel = document.getElementById("latency-libreddit-label"); -latencyLibredditElement.addEventListener("click", - async () => { - let reloadWindow = () => location.reload(); - latencyLibredditElement.addEventListener("click", reloadWindow); - await redditHelper.init(); - let redirects = redditHelper.getRedirects(); - const oldHtml = latencyLibredditLabel.innerHTML; - latencyLibredditLabel.innerHTML = '...'; - commonHelper.testLatency(latencyLibredditLabel, redirects.libreddit.normal).then(r => { - browser.storage.local.set({ libredditLatency: r }); - latencyLibredditLabel.innerHTML = oldHtml; - commonHelper.processDefaultCustomInstances('reddit', 'libreddit', 'normal', document); - latencyLibredditElement.removeEventListener("click", reloadWindow); - }); - } -); - -let latencyTedditElement = document.getElementById("latency-teddit"); -let latencyTedditLabel = document.getElementById("latency-teddit-label"); -latencyTedditElement.addEventListener("click", - async () => { - let reloadWindow = () => location.reload(); - latencyTedditElement.addEventListener("click", reloadWindow); - await redditHelper.init(); - let redirects = redditHelper.getRedirects(); - const oldHtml = latencyTedditLabel.innerHTML; - latencyTedditLabel.innerHTML = '...'; - commonHelper.testLatency(latencyTedditLabel, redirects.teddit.normal).then(r => { - browser.storage.local.set({ tedditLatency: r }); - latencyTedditLabel.innerHTML = oldHtml; - commonHelper.processDefaultCustomInstances('reddit', 'teddit', 'normal', document); - latencyTedditElement.removeEventListener("click", reloadWindow); - }); - } -); \ No newline at end of file +utils.latency('reddit', 'libreddit', document, location, true) +utils.latency('reddit', 'teddit', document, location, true) \ No newline at end of file diff --git a/src/pages/options/search/search.js b/src/pages/options/search/search.js index 47f27c0..fb92805 100644 --- a/src/pages/options/search/search.js +++ b/src/pages/options/search/search.js @@ -1,5 +1,5 @@ import searchHelper from "../../../assets/javascripts/helpers/search.js"; -import commonHelper from "../../../assets/javascripts/helpers/common.js"; +import utils from "../../../assets/javascripts/helpers/utils.js"; let searxDiv = document.getElementById("searx"); let searxngDiv = document.getElementById("searxng"); @@ -127,69 +127,16 @@ function changeProtocolSettings() { } } -commonHelper.processDefaultCustomInstances('search', 'searx', 'normal', document); -commonHelper.processDefaultCustomInstances('search', 'searx', 'tor', document); -commonHelper.processDefaultCustomInstances('search', 'searx', 'i2p', document); -commonHelper.processDefaultCustomInstances('search', 'searxng', 'normal', document); -commonHelper.processDefaultCustomInstances('search', 'searxng', 'tor', document); -commonHelper.processDefaultCustomInstances('search', 'searxng', 'i2p', document); -commonHelper.processDefaultCustomInstances('search', 'whoogle', 'normal', document); -commonHelper.processDefaultCustomInstances('search', 'whoogle', 'tor', document); -commonHelper.processDefaultCustomInstances('search', 'whoogle', 'i2p', document); +utils.processDefaultCustomInstances('search', 'searx', 'normal', document); +utils.processDefaultCustomInstances('search', 'searx', 'tor', document); +utils.processDefaultCustomInstances('search', 'searx', 'i2p', document); +utils.processDefaultCustomInstances('search', 'searxng', 'normal', document); +utils.processDefaultCustomInstances('search', 'searxng', 'tor', document); +utils.processDefaultCustomInstances('search', 'searxng', 'i2p', document); +utils.processDefaultCustomInstances('search', 'whoogle', 'normal', document); +utils.processDefaultCustomInstances('search', 'whoogle', 'tor', document); +utils.processDefaultCustomInstances('search', 'whoogle', 'i2p', document); -let latencySearxElement = document.getElementById("latency-searx"); -let latencySearxLabel = document.getElementById("latency-searx-label"); -latencySearxElement.addEventListener("click", - async () => { - let reloadWindow = () => location.reload(); - latencySearxElement.addEventListener("click", reloadWindow); - await searchHelper.init(); - let redirects = searchHelper.getRedirects(); - const oldHtml = latencySearxLabel.innerHTML; - latencySearxLabel.innerHTML = '...'; - commonHelper.testLatency(latencySearxLabel, redirects.searx.normal).then(r => { - browser.storage.local.set({ searxLatency: r }); - latencySearxLabel.innerHTML = oldHtml; - commonHelper.processDefaultCustomInstances('search', 'searx', 'normal', document); - latencySearxElement.removeEventListener("click", reloadWindow); - }); - } -); - -let latencySearxngElement = document.getElementById("latency-searxng"); -let latencySearxngLabel = document.getElementById("latency-searxng-label"); -latencySearxngElement.addEventListener("click", - async () => { - let reloadWindow = () => location.reload(); - latencySearxngElement.addEventListener("click", reloadWindow); - await searchHelper.init(); - let redirects = searchHelper.getRedirects(); - const oldHtml = latencySearxngLabel.innerHTML; - latencySearxngLabel.innerHTML = '...'; - commonHelper.testLatency(latencySearxngLabel, redirects.searxng.normal).then(r => { - browser.storage.local.set({ searxngLatency: r }); - latencySearxngLabel.innerHTML = oldHtml; - commonHelper.processDefaultCustomInstances('search', 'searxng', 'normal', document); - latencySearxngElement.removeEventListener("click", reloadWindow); - }); - } -); - -let latencyWhoogleElement = document.getElementById("latency-whoogle"); -let latencyWhoogleLabel = document.getElementById("latency-whoogle-label"); -latencyWhoogleElement.addEventListener("click", - async () => { - let reloadWindow = () => location.reload(); - latencyWhoogleElement.addEventListener("click", reloadWindow); - await searchHelper.init(); - let redirects = searchHelper.getRedirects(); - const oldHtml = latencyWhoogleLabel.innerHTML; - latencyWhoogleLabel.innerHTML = '...'; - commonHelper.testLatency(latencyWhoogleLabel, redirects.whoogle.normal).then(r => { - browser.storage.local.set({ whoogleLatency: r }); - latencyWhoogleLabel.innerHTML = oldHtml; - commonHelper.processDefaultCustomInstances('search', 'whoogle', 'normal', document); - latencyWhoogleElement.removeEventListener("click", reloadWindow); - }); - } -); \ No newline at end of file +utils.latency('search', 'searx', document, location, true) +utils.latency('search', 'searxng', document, location, true) +utils.latency('search', 'whoogle', document, location, true) diff --git a/src/pages/options/sendTargets/sendTargets.js b/src/pages/options/sendTargets/sendTargets.js index 057ae86..0cb1da0 100644 --- a/src/pages/options/sendTargets/sendTargets.js +++ b/src/pages/options/sendTargets/sendTargets.js @@ -1,5 +1,5 @@ import sendTargetsHelper from "../../../assets/javascripts/helpers/sendTargets.js"; -import commonHelper from "../../../assets/javascripts/helpers/common.js"; +import utils from "../../../assets/javascripts/helpers/utils.js"; let disable = document.getElementById("disable-sendTargets"); let protocol = document.getElementById("protocol") @@ -37,24 +37,7 @@ function changeProtocolSettings() { } } -commonHelper.processDefaultCustomInstances('sendTargets', 'send', 'normal', document); -commonHelper.processDefaultCustomInstances('sendTargets', 'send', 'tor', document); +utils.processDefaultCustomInstances('sendTargets', 'send', 'normal', document); +utils.processDefaultCustomInstances('sendTargets', 'send', 'tor', document); -let latencyElement = document.getElementById("latency"); -let latencyLabel = document.getElementById("latency-label"); -latencyElement.addEventListener("click", - async () => { - let reloadWindow = () => location.reload(); - latencyElement.addEventListener("click", reloadWindow); - await sendTargetsHelper.init(); - let redirects = sendTargetsHelper.getRedirects(); - const oldHtml = latencyLabel.innerHTML; - latencyLabel.innerHTML = '...'; - commonHelper.testLatency(latencyLabel, redirects.send.normal).then(r => { - browser.storage.local.set({ sendLatency: r }); - latencyLabel.innerHTML = oldHtml; - commonHelper.processDefaultCustomInstances('sendTargets', 'send', 'normal', document); - latencyElement.removeEventListener("click", reloadWindow) - }); - } -); \ No newline at end of file +utils.latency('sendTargets', 'send', document, location) \ No newline at end of file diff --git a/src/pages/options/tiktok/tiktok.js b/src/pages/options/tiktok/tiktok.js index 8055fc9..5a57b46 100644 --- a/src/pages/options/tiktok/tiktok.js +++ b/src/pages/options/tiktok/tiktok.js @@ -1,5 +1,5 @@ import tiktokHelper from "../../../assets/javascripts/helpers/tiktok.js"; -import commonHelper from "../../../assets/javascripts/helpers/common.js"; +import utils from "../../../assets/javascripts/helpers/utils.js"; let disable = document.getElementById("disable-tiktok"); let protocol = document.getElementById("protocol") @@ -49,24 +49,7 @@ function changeProtocolSettings() { } } -commonHelper.processDefaultCustomInstances('tiktok', 'proxiTok', 'normal', document); -commonHelper.processDefaultCustomInstances('tiktok', 'proxiTok', 'tor', document); +utils.processDefaultCustomInstances('tiktok', 'proxiTok', 'normal', document); +utils.processDefaultCustomInstances('tiktok', 'proxiTok', 'tor', document); -let latencyElement = document.getElementById("latency"); -let latencyLabel = document.getElementById("latency-label"); -latencyElement.addEventListener("click", - async () => { - let reloadWindow = () => location.reload(); - latencyElement.addEventListener("click", reloadWindow); - await tiktokHelper.init(); - let redirects = tiktokHelper.getRedirects(); - const oldHtml = latencyLabel.innerHTML; - latencyLabel.innerHTML = '...'; - commonHelper.testLatency(latencyLabel, redirects.proxiTok.normal).then(r => { - browser.storage.local.set({ proxiTokLatency: r }); - latencyLabel.innerHTML = oldHtml; - commonHelper.processDefaultCustomInstances('tiktok', 'proxiTok', 'normal', document); - latencyElement.removeEventListener("click", reloadWindow) - }); - } -); \ No newline at end of file +utils.latency('tiktok', 'proxiTok', document, location) \ No newline at end of file diff --git a/src/pages/options/translate/translate.js b/src/pages/options/translate/translate.js index 55a2d74..998ebc3 100644 --- a/src/pages/options/translate/translate.js +++ b/src/pages/options/translate/translate.js @@ -1,5 +1,5 @@ import translateHelper from "../../../assets/javascripts/helpers/translate/translate.js"; -import commonHelper from "../../../assets/javascripts/helpers/common.js"; +import utils from "../../../assets/javascripts/helpers/utils.js"; let disable = document.getElementById("disable-simplyTranslate"); let simplyTranslateDiv = document.getElementById("simplyTranslate"); @@ -66,45 +66,10 @@ document.addEventListener("change", () => { }) -commonHelper.processDefaultCustomInstances('translate', 'simplyTranslate', 'normal', document) -commonHelper.processDefaultCustomInstances('translate', 'simplyTranslate', 'tor', document); -commonHelper.processDefaultCustomInstances('translate', 'lingva', 'normal', document); -commonHelper.processDefaultCustomInstances('translate', 'lingva', 'tor', document); +utils.processDefaultCustomInstances('translate', 'simplyTranslate', 'normal', document) +utils.processDefaultCustomInstances('translate', 'simplyTranslate', 'tor', document); +utils.processDefaultCustomInstances('translate', 'lingva', 'normal', document); +utils.processDefaultCustomInstances('translate', 'lingva', 'tor', document); -let latencySimplyTranslateElement = document.getElementById("latency-simplyTranslate"); -let latencySimplyTranslateLabel = document.getElementById("latency-simplyTranslate-label"); -latencySimplyTranslateElement.addEventListener("click", - async () => { - let reloadWindow = () => location.reload(); - latencySimplyTranslateElement.addEventListener("click", reloadWindow); - await translateHelper.init(); - let redirects = translateHelper.getRedirects(); - const oldHtml = latencySimplyTranslateLabel.innerHTML; - latencySimplyTranslateLabel.innerHTML = '...'; - commonHelper.testLatency(latencySimplyTranslateLabel, redirects.simplyTranslate.normal).then(r => { - browser.storage.local.set({ simplyTranslateLatency: r }); - latencySimplyTranslateLabel.innerHTML = oldHtml; - commonHelper.processDefaultCustomInstances('translate', 'simplyTranslate', 'normal', document) - latencySimplyTranslateElement.removeEventListener("click", reloadWindow); - }); - } -); - -let latencyLingvaElement = document.getElementById("latency-lingva"); -let latencyLingvaLabel = document.getElementById("latency-lingva-label"); -latencyLingvaElement.addEventListener("click", - async () => { - let reloadWindow = () => location.reload(); - latencyLingvaElement.addEventListener("click", reloadWindow); - await translateHelper.init(); - let redirects = translateHelper.getRedirects(); - const oldHtml = latencyLingvaLabel.innerHTML; - latencyLingvaLabel.innerHTML = '...'; - commonHelper.testLatency(latencyLingvaLabel, redirects.lingva.normal).then(r => { - browser.storage.local.set({ lingvaLatency: r }); - latencyLingvaLabel.innerHTML = oldHtml; - commonHelper.processDefaultCustomInstances('translate', 'lingva', 'normal', document); - latencyLingvaElement.removeEventListener("click", reloadWindow); - }); - } -); \ No newline at end of file +utils.latency('translate', 'simplyTranslate', document, location, true) +utils.latency('translate', 'lingva', document, location, true) \ No newline at end of file diff --git a/src/pages/options/twitter/twitter.html b/src/pages/options/twitter/twitter.html index 2bd204a..95e0a0b 100644 --- a/src/pages/options/twitter/twitter.html +++ b/src/pages/options/twitter/twitter.html @@ -117,27 +117,6 @@
-
-
-

Default Instances

-
-
-
-
-

Custom Instances

-
-
-
- - -
-
-
-
diff --git a/src/pages/options/twitter/twitter.js b/src/pages/options/twitter/twitter.js index c23007f..72c3d92 100644 --- a/src/pages/options/twitter/twitter.js +++ b/src/pages/options/twitter/twitter.js @@ -1,5 +1,5 @@ import twitterHelper from "../../../assets/javascripts/helpers/twitter.js"; -import commonHelper from "../../../assets/javascripts/helpers/common.js"; +import utils from "../../../assets/javascripts/helpers/utils.js"; let disable = document.getElementById("disable-nitter"); let protocol = document.getElementById("protocol"); @@ -47,24 +47,7 @@ function changeProtocolSettings() { } } -commonHelper.processDefaultCustomInstances('twitter', 'nitter', 'normal', document); -commonHelper.processDefaultCustomInstances('twitter', 'nitter', 'tor', document) +utils.processDefaultCustomInstances('twitter', 'nitter', 'normal', document); +utils.processDefaultCustomInstances('twitter', 'nitter', 'tor', document) -let latencyElement = document.getElementById("latency"); -let latencyLabel = document.getElementById("latency-label"); -latencyElement.addEventListener("click", - async () => { - let reloadWindow = () => location.reload(); - latencyElement.addEventListener("click", reloadWindow); - await twitterHelper.init(); - let redirects = twitterHelper.getRedirects(); - const oldHtml = latencyLabel.innerHTML; - latencyLabel.innerHTML = '...'; - commonHelper.testLatency(latencyLabel, redirects.nitter.normal).then(r => { - browser.storage.local.set({ nitterLatency: r }); - latencyLabel.innerHTML = oldHtml; - commonHelper.processDefaultCustomInstances('twitter', 'nitter', 'normal', document); - latencyElement.removeEventListener("click", reloadWindow) - }); - } -); \ No newline at end of file +utils.latency('twitter', 'nitter', document, location) \ No newline at end of file diff --git a/src/pages/options/twitter/twitter.pug b/src/pages/options/twitter/twitter.pug index af7b7f1..517a272 100644 --- a/src/pages/options/twitter/twitter.pug +++ b/src/pages/options/twitter/twitter.pug @@ -28,9 +28,6 @@ body.option(dir="auto") .tor include ../../widgets/instances.pug +instances('https://nitter.onion') - .i2p - include ../../widgets/instances.pug - +instances('https://nitter.i2p') script(type="module" src="../init.js") diff --git a/src/pages/options/wikipedia/wikipedia.js b/src/pages/options/wikipedia/wikipedia.js index 8d9af92..c421c18 100644 --- a/src/pages/options/wikipedia/wikipedia.js +++ b/src/pages/options/wikipedia/wikipedia.js @@ -1,5 +1,5 @@ import wikipediaHelper from "../../../assets/javascripts/helpers/wikipedia.js"; -import commonHelper from "../../../assets/javascripts/helpers/common.js"; +import utils from "../../../assets/javascripts/helpers/utils.js"; let disableWikipediaElement = document.getElementById("disable-wikipedia"); let protocolElement = document.getElementById("protocol"); @@ -44,27 +44,10 @@ function changeProtocolSettings(protocol) { i2pDiv.style.display = 'block'; } } -commonHelper.processDefaultCustomInstances('wikipedia' ,'wikiless', 'normal', document); -commonHelper.processDefaultCustomInstances('wikipedia' ,'wikiless', 'tor', document); -commonHelper.processDefaultCustomInstances('wikipedia' ,'wikiless', 'i2p', document); +utils.processDefaultCustomInstances('wikipedia', 'wikiless', 'normal', document); +utils.processDefaultCustomInstances('wikipedia', 'wikiless', 'tor', document); +utils.processDefaultCustomInstances('wikipedia', 'wikiless', 'i2p', document); window.onblur = wikipediaHelper.initWikilessCookies; -let latencyElement = document.getElementById("latency"); -let latencyLabel = document.getElementById("latency-label"); -latencyElement.addEventListener("click", - async () => { - let reloadWindow = () => location.reload(); - latencyElement.addEventListener("click", reloadWindow); - await wikipediaHelper.init(); - let redirects = wikipediaHelper.getRedirects(); - const oldHtml = latencyLabel.innerHTML; - latencyLabel.innerHTML = '...'; - commonHelper.testLatency(latencyLabel, redirects.wikiless.normal).then(r => { - browser.storage.local.set({ wikilessLatency: r }); - latencyLabel.innerHTML = oldHtml; - commonHelper.processDefaultCustomInstances('wikipedia' ,'wikiless', 'normal', document); - latencyElement.removeEventListener("click", reloadWindow) - }); - } -); \ No newline at end of file +utils.latency('wikipedia', 'wikiless', document, location) \ No newline at end of file diff --git a/src/pages/options/youtube/youtube.html b/src/pages/options/youtube/youtube.html index 6dfb9f7..20d13bd 100644 --- a/src/pages/options/youtube/youtube.html +++ b/src/pages/options/youtube/youtube.html @@ -256,7 +256,4 @@ - - - \ No newline at end of file diff --git a/src/pages/options/youtube/youtube.js b/src/pages/options/youtube/youtube.js index ce42abd..4f98784 100644 --- a/src/pages/options/youtube/youtube.js +++ b/src/pages/options/youtube/youtube.js @@ -1,5 +1,5 @@ import youtubeHelper from "../../../assets/javascripts/helpers/youtube/youtube.js"; -import commonHelper from "../../../assets/javascripts/helpers/common.js"; +import utils from "../../../assets/javascripts/helpers/utils.js"; let disableYoutube = document.getElementById("disable-invidious"); let youtubeFrontend = document.getElementById("youtube-frontend"); @@ -149,67 +149,13 @@ invidiousForm.addEventListener('submit', async event => { // youtubeHelper.applyPipedLocalStorage(url); // }); -commonHelper.processDefaultCustomInstances('youtube', 'invidious', 'normal', document); -commonHelper.processDefaultCustomInstances('youtube', 'invidious', 'tor', document); -commonHelper.processDefaultCustomInstances('youtube', 'pipedMaterial', 'normal', document); -commonHelper.processDefaultCustomInstances('youtube', 'pipedMaterial', 'tor', document); -commonHelper.processDefaultCustomInstances('youtube', 'piped', 'normal', document); -commonHelper.processDefaultCustomInstances('youtube', 'piped', 'tor', document); +utils.processDefaultCustomInstances('youtube', 'invidious', 'normal', document); +utils.processDefaultCustomInstances('youtube', 'invidious', 'tor', document); +utils.processDefaultCustomInstances('youtube', 'pipedMaterial', 'normal', document); +utils.processDefaultCustomInstances('youtube', 'pipedMaterial', 'tor', document); +utils.processDefaultCustomInstances('youtube', 'piped', 'normal', document); +utils.processDefaultCustomInstances('youtube', 'piped', 'tor', document); - -let latencyInvidiousElement = document.getElementById("latency-invidious"); -let latencyInvidiousLabel = document.getElementById("latency-invidious-label"); -latencyInvidiousElement.addEventListener("click", - async () => { - let reloadWindow = () => location.reload(); - latencyInvidiousElement.addEventListener("click", reloadWindow); - await youtubeHelper.init(); - let redirects = youtubeHelper.getRedirects(); - const oldHtml = latencyInvidiousLabel.innerHTML; - latencyInvidiousLabel.innerHTML = '...'; - commonHelper.testLatency(latencyInvidiousLabel, redirects.invidious.normal).then(r => { - browser.storage.local.set({ invidiousLatency: r }); - latencyInvidiousLabel.innerHTML = oldHtml; - commonHelper.processDefaultCustomInstances('youtube', 'invidious', 'normal', document); - latencyInvidiousElement.removeEventListener("click", reloadWindow); - }); - } -); - -let latencyPipedMaterialElement = document.getElementById("latency-pipedMaterial"); -let latencyPipedMaterialLabel = document.getElementById("latency-pipedMaterial-label"); -latencyPipedMaterialElement.addEventListener("click", - async () => { - let reloadWindow = () => location.reload(); - latencyPipedMaterialElement.addEventListener("click", reloadWindow); - await youtubeHelper.init(); - let redirects = youtubeHelper.getRedirects(); - const oldHtml = latencyPipedMaterialLabel.innerHTML; - latencyPipedMaterialLabel.innerHTML = '...'; - commonHelper.testLatency(latencyPipedMaterialLabel, redirects.pipedMaterial.normal).then(r => { - browser.storage.local.set({ pipedMaterialLatency: r }); - latencyPipedMaterialLabel.innerHTML = oldHtml; - commonHelper.processDefaultCustomInstances('youtube', 'pipedMaterial', 'normal', document); - latencyPipedMaterialElement.removeEventListener("click", reloadWindow); - }); - } -); - -let latencyPipedElement = document.getElementById("latency-piped"); -let latencyPipedLabel = document.getElementById("latency-piped-label"); -latencyPipedElement.addEventListener("click", - async () => { - let reloadWindow = () => location.reload(); - latencyPipedElement.addEventListener("click", reloadWindow); - await youtubeHelper.init(); - let redirects = youtubeHelper.getRedirects(); - const oldHtml = latencyPipedLabel.innerHTML; - latencyPipedLabel.innerHTML = '...'; - commonHelper.testLatency(latencyPipedLabel, redirects.piped.normal).then(r => { - browser.storage.local.set({ pipedLatency: r }); - latencyPipedLabel.innerHTML = oldHtml; - commonHelper.processDefaultCustomInstances('youtube', 'piped', 'normal', document); - latencyPipedElement.removeEventListener("click", reloadWindow); - }); - } -); +utils.latency('youtube', 'invidious', document, location, true) +utils.latency('youtube', 'piped', document, location, true) +utils.latency('youtube', 'pipedMaterial', document, location, true) diff --git a/src/pages/options/youtube/youtube.pug b/src/pages/options/youtube/youtube.pug index 45c7f17..39235b0 100644 --- a/src/pages/options/youtube/youtube.pug +++ b/src/pages/options/youtube/youtube.pug @@ -78,6 +78,3 @@ body.option(dir="auto") script(type="module" src="../init.js") script(type="module" src="./youtube.js") - script(type="module" src="./invidious.js") - script(type="module" src="./piped.js") - script(type="module" src="./pipedMaterial.js") diff --git a/src/pages/options/youtubeMusic/youtubeMusic.js b/src/pages/options/youtubeMusic/youtubeMusic.js index 518c651..167630e 100644 --- a/src/pages/options/youtubeMusic/youtubeMusic.js +++ b/src/pages/options/youtubeMusic/youtubeMusic.js @@ -1,5 +1,5 @@ import youtubeMusicHelper from "../../../assets/javascripts/helpers/youtubeMusic.js"; -import commonHelper from "../../../assets/javascripts/helpers/common.js"; +import utils from "../../../assets/javascripts/helpers/utils.js"; let disableYoutubeMusicElement = document.getElementById("disable-beatbump"); @@ -18,23 +18,6 @@ document.addEventListener("change", async () => { }) }) -commonHelper.processDefaultCustomInstances('youtubeMusic', 'beatbump', 'normal', document); +utils.processDefaultCustomInstances('youtubeMusic', 'beatbump', 'normal', document); -let latencyElement = document.getElementById("latency"); -let latencyLabel = document.getElementById("latency-label"); -latencyElement.addEventListener("click", - async () => { - let reloadWindow = () => location.reload(); - latencyElement.addEventListener("click", reloadWindow); - await youtubeMusicHelper.init(); - let redirects = youtubeMusicHelper.getRedirects(); - const oldHtml = latencyLabel.innerHTML; - latencyLabel.innerHTML = '...'; - commonHelper.testLatency(latencyLabel, redirects.beatbump.normal).then(r => { - browser.storage.local.set({ beatbumpLatency: r }); - latencyLabel.innerHTML = oldHtml; - commonHelper.processDefaultCustomInstances('youtubeMusic', 'beatbump', 'normal', document); - latencyElement.removeEventListener("click", reloadWindow) - }); - } -); \ No newline at end of file +utils.latency('youtubeMusic', 'beatbump', document, location) \ No newline at end of file diff --git a/src/pages/popup/popup.html b/src/pages/popup/popup.html index b4086ff..b815bc1 100644 --- a/src/pages/popup/popup.html +++ b/src/pages/popup/popup.html @@ -70,7 +70,7 @@

Translate

-
+ -
+ + \ No newline at end of file diff --git a/src/pages/popup/popup.js b/src/pages/popup/popup.js index c6399ae..e465a7e 100644 --- a/src/pages/popup/popup.js +++ b/src/pages/popup/popup.js @@ -1,22 +1,7 @@ "use strict"; window.browser = window.browser || window.chrome; -import commonHelper from "../../assets/javascripts/helpers/common.js"; -import youtubeHelper from "../../assets/javascripts/helpers/youtube/youtube.js"; -import youtubeMusicHelper from "../../assets/javascripts/helpers/youtubeMusic.js"; -import twitterHelper from "../../assets/javascripts/helpers/twitter.js"; -import instagramHelper from "../../assets/javascripts/helpers/instagram.js"; -import mapsHelper from "../../assets/javascripts/helpers/maps.js"; -import redditHelper from "../../assets/javascripts/helpers/reddit.js"; -import searchHelper from "../../assets/javascripts/helpers/search.js"; -import translateHelper from "../../assets/javascripts/helpers/translate/translate.js"; -import wikipediaHelper from "../../assets/javascripts/helpers/wikipedia.js"; -import mediumHelper from "../../assets/javascripts/helpers/medium.js"; -import imgurHelper from "../../assets/javascripts/helpers/imgur.js"; -import tiktokHelper from "../../assets/javascripts/helpers/tiktok.js"; -import sendTargetsHelper from "../../assets/javascripts/helpers/sendTargets.js"; -import peertubeHelper from "../../assets/javascripts/helpers/peertube.js"; -import lbryHelper from "../../assets/javascripts/helpers/lbry.js"; +import utils from "../../assets/javascripts/helpers/utils.js"; import generalHelper from "../../assets/javascripts/helpers/general.js"; let disableTwitterElement = document.getElementById("disable-nitter"); @@ -35,9 +20,6 @@ let disableSendTargetsElement = document.getElementById("disable-sendTargets"); let disableImgurElement = document.getElementById("disable-imgur"); let disableTiktokElement = document.getElementById("disable-tiktok"); -let copyRawElement = document.getElementById('copy_raw'); -let unifyElement = document.getElementById('unify'); - browser.storage.local.get( [ "disableTwitter", @@ -95,109 +77,13 @@ document.addEventListener("change", () => { }); }) -let changeInstanceElement = document.getElementById("change-instance") -changeInstanceElement.addEventListener("click", switchInstance); -copyRawElement.addEventListener("click", copyRaw); +document.getElementById("change_instance").addEventListener("click", utils.switchInstance); +document.getElementById('copy_raw').addEventListener("click", utils.copyRaw); document.getElementById("more-options").addEventListener("click", () => browser.runtime.openOptionsPage()); -unifyElement.addEventListener("click", unify) +document.getElementById('unify').addEventListener("click", unify); -function unify() { - browser.tabs.query( - { active: true, currentWindow: true }, - async tabs => { - let currTab = tabs[0] - if (currTab) { - let url = new URL(currTab.url); - - let result = await youtubeHelper.initInvidiousCookies(url); - if (!result) result = await youtubeHelper.initPipedLocalStorage(url, currTab.id); - if (!result) result = await youtubeHelper.initPipedMaterialLocalStorage(url, currTab.id); - - if (!result) result = await twitterHelper.initNitterCookies(url); - - if (!result) result = await redditHelper.initLibredditCookies(url); - if (!result) result = await redditHelper.initTedditCookies(url); - - if (!result) result = await searchHelper.initSearxCookies(url); - if (!result) result = await searchHelper.initSearxngCookies(url); - - if (!result) result = await tiktokHelper.initProxiTokCookies(url); - - if (!result) result = await wikipediaHelper.initWikilessCookies(url); - - if (!result) result = await translateHelper.initSimplyTranslateCookies(url); - if (!result) result = await translateHelper.initLingvaLocalStorage(url); - - if (result) { - const textElement = unifyElement.getElementsByTagName('h4')[0] - const oldHtml = textElement.innerHTML; - textElement.innerHTML = 'Unified'; - setTimeout(() => textElement.innerHTML = oldHtml, 1000); - } - } - } - ) -} - -function switchInstance() { - browser.tabs.query({ active: true, currentWindow: true }, async tabs => { - let currTab = tabs[0]; - if (currTab) { - let url = new URL(currTab.url); - let newUrl; - - // newUrl = youtubeHelper.switchInstance(url); - // if (!newUrl) newUrl = twitterHelper.switchInstance(url); - // if (!newUrl) newUrl = instagramHelper.switchInstance(url); - if (!newUrl) newUrl = await redditHelper.switchInstance(url); - // if (!newUrl) newUrl = searchHelper.switchInstance(url); - // if (!newUrl) newUrl = translateHelper.switchInstance(url); - // if (!newUrl) newUrl = mediumHelper.switchInstance(url); - // if (!newUrl) newUrl = sendTargetsHelper.switchInstance(url); - // if (!newUrl) newUrl = peertubeHelper.switchInstance(url); - // if (!newUrl) newUrl = lbryHelper.switchInstance(url); - // if (!newUrl) newUrl = imgurHelper.switchInstance(url); - // if (!newUrl) newUrl = wikipediaHelper.switchInstance(url); - - if (newUrl) { - browser.tabs.update({ url: newUrl }); - return true; - } - } - }) - return false; -} - -function copyRaw() { - browser.tabs.query( - { active: true, currentWindow: true }, tabs => { - let currTab = tabs[0]; - if (currTab) { - let url = new URL(currTab.url); - let newUrl; - newUrl = youtubeHelper.reverse(url); - - if (!newUrl) newUrl = twitterHelper.reverse(url); - if (!newUrl) newUrl = instagramHelper.reverse(url); - if (!newUrl) newUrl = tiktokHelper.reverse(url); - if (!newUrl) newUrl = imgurHelper.reverse(url); - - if (newUrl) { - navigator.clipboard.writeText(newUrl); - const textElement = copyRawElement.getElementsByTagName('h4')[0] - const oldHtml = textElement.innerHTML; - textElement.innerHTML = 'Copied'; - setTimeout(() => textElement.innerHTML = oldHtml, 1000); - } - } - } - ) -} - -let popupFrontends; generalHelper.init().then(() => { - popupFrontends = generalHelper.getPopupFrontends(); - + let popupFrontends = generalHelper.getPopupFrontends(); for (const frontend of generalHelper.allPopupFrontends) if (!popupFrontends.includes(frontend)) document.getElementById(frontend).classList.add("hide") diff --git a/src/pages/popup/popup.pug b/src/pages/popup/popup.pug index 31354a4..97f6d04 100644 --- a/src/pages/popup/popup.pug +++ b/src/pages/popup/popup.pug @@ -87,7 +87,7 @@ html(lang="en") input#disable-simplyTranslate(type="checkbox") #maps.some-block - a.title(href="https://maps.com") + a.title(href="https://www.openstreetmap.org") +maps h4 Maps input#disable-osm(type="checkbox") @@ -99,7 +99,7 @@ html(lang="en") input#disable-sendTargets(type="checkbox") .some-block - a#change-instance.title.button + a#change_instance.title.button +change_instance h4 Change Instance @@ -120,4 +120,4 @@ html(lang="en") script(type="module" src="../options/init.js") script(type="module" src="./popup.js") - //- script(src="../../assets/javascripts/localise.js") \ No newline at end of file + script(src="../../assets/javascripts/localise.js") \ No newline at end of file diff --git a/src/pages/popup/style.css b/src/pages/popup/style.css index 6cd91b9..4cc5614 100644 --- a/src/pages/popup/style.css +++ b/src/pages/popup/style.css @@ -9,11 +9,4 @@ html, body { .hide { display: none !important; -} - -.button { - margin: auto; - width: 50%; - border: 3px rgb(180, 201, 180); - padding: 10px; - } \ No newline at end of file +} \ No newline at end of file