diff options
author | ManeraKai <manerakai@protonmail.com> | 2022-06-12 18:51:47 +0300 |
---|---|---|
committer | ManeraKai <manerakai@protonmail.com> | 2022-06-12 18:51:47 +0300 |
commit | cc8b5e4e5f4d865df2f58805cbe19ec06e21c195 (patch) | |
tree | 346fd7b3665bb32d5c9817059cebc9690f825234 /src/assets/javascripts | |
parent | Bypassing homepage for quora, medium, imgur #327 (diff) | |
download | libredirect-cc8b5e4e5f4d865df2f58805cbe19ec06e21c195.zip |
Added libremdb #230
Diffstat (limited to 'src/assets/javascripts')
-rw-r--r-- | src/assets/javascripts/general.js | 1 | ||||
-rw-r--r-- | src/assets/javascripts/imdb.js | 159 | ||||
-rw-r--r-- | src/assets/javascripts/utils.js | 4 |
3 files changed, 164 insertions, 0 deletions
diff --git a/src/assets/javascripts/general.js b/src/assets/javascripts/general.js index 50d6ee33..4e8b7a7d 100644 --- a/src/assets/javascripts/general.js +++ b/src/assets/javascripts/general.js @@ -64,6 +64,7 @@ const allPopupFrontends = [ "wikipedia", "medium", "quora", + "imdb", "reuters", "peertube", "lbry", diff --git a/src/assets/javascripts/imdb.js b/src/assets/javascripts/imdb.js new file mode 100644 index 00000000..9b0efe42 --- /dev/null +++ b/src/assets/javascripts/imdb.js @@ -0,0 +1,159 @@ +window.browser = window.browser || window.chrome; + +import utils from './utils.js' + +const targets = [ + /^https?:\/{2}(www\.|)imdb\.com.*/ +]; + +let redirects = { + "libremdb": { + "normal": [], + "tor": [] + } +} +function setRedirects(val) { + browser.storage.local.get('cloudflareBlackList', r => { + redirects.libremdb = val; + libremdbNormalRedirectsChecks = [...redirects.libremdb.normal]; + for (const instance of r.cloudflareBlackList) { + const a = libremdbNormalRedirectsChecks.indexOf(instance); + if (a > -1) libremdbNormalRedirectsChecks.splice(a, 1); + } + browser.storage.local.set({ + imdbRedirects: redirects, + libremdbNormalRedirectsChecks + }) + }) +} + +let + disableImdb, + imdbProtocol, + imdbRedirects, + libremdbNormalRedirectsChecks, + libremdbNormalCustomRedirects, + libremdbTorRedirectsChecks, + libremdbTorCustomRedirects; + +function init() { + return new Promise(async resolve => { + browser.storage.local.get( + [ + "disableImdb", + "imdbProtocol", + "imdbRedirects", + "libremdbNormalRedirectsChecks", + "libremdbNormalCustomRedirects", + "libremdbTorRedirectsChecks", + "libremdbTorCustomRedirects", + ], + r => { + disableImdb = r.disableImdb; + imdbProtocol = r.imdbProtocol; + imdbRedirects = r.imdbRedirects; + libremdbNormalRedirectsChecks = r.libremdbNormalRedirectsChecks; + libremdbNormalCustomRedirects = r.libremdbNormalCustomRedirects; + libremdbTorRedirectsChecks = r.libremdbTorRedirectsChecks; + libremdbTorCustomRedirects = r.libremdbTorCustomRedirects; + resolve(); + } + ) + }) +} + +init(); +browser.storage.onChanged.addListener(init) + +function redirect(url, type, initiator) { + if (disableImdb) return; + if (url.pathname == "/") return; + if (type != "main_frame") return; + const all = [ + ...imdbRedirects.libremdb.normal, + ...libremdbNormalCustomRedirects + ]; + if (initiator && (all.includes(initiator.origin) || targets.includes(initiator.host))) return; + if (!targets.some(rx => rx.test(url.href))) return; + + let instancesList; + if (imdbProtocol == 'normal') instancesList = [...libremdbNormalRedirectsChecks, ...libremdbNormalCustomRedirects]; + if (imdbProtocol == 'tor') instancesList = [...libremdbTorRedirectsChecks, ...libremdbTorCustomRedirects]; + if (instancesList.length === 0) return; + + const randomInstance = utils.getRandomInstance(instancesList); + return `${randomInstance}${url.pathname}`; +} + +function reverse(url) { + return new Promise(async resolve => { + await init(); + let protocolHost = utils.protocolHost(url); + const all = [ + ...imdbRedirects.libremdb.normal, + ...imdbRedirects.libremdb.tor, + ...libremdbNormalCustomRedirects, + ...libremdbTorCustomRedirects + ]; + if (!all.includes(protocolHost)) { resolve(); return; } + + resolve(`https://imdb.com${url.pathname}${url.search}`); + }) +} + +function switchInstance(url) { + return new Promise(async resolve => { + await init(); + let protocolHost = utils.protocolHost(url); + const all = [ + ...imdbRedirects.libremdb.tor, + ...imdbRedirects.libremdb.normal, + + ...libremdbNormalCustomRedirects, + ...libremdbTorCustomRedirects, + ]; + if (!all.includes(protocolHost)) { resolve(); return; } + + let instancesList; + if (imdbProtocol == 'normal') instancesList = [...libremdbNormalCustomRedirects, ...libremdbNormalRedirectsChecks]; + else if (imdbProtocol == 'tor') instancesList = [...libremdbTorCustomRedirects, ...libremdbTorRedirectsChecks]; + + const i = instancesList.indexOf(protocolHost); + if (i > -1) instancesList.splice(i, 1); + if (instancesList.length === 0) { resolve(); return; } + + const randomInstance = utils.getRandomInstance(instancesList); + resolve(`${randomInstance}${url.pathname}${url.search}`); + }) +} + +function initDefaults() { + return new Promise(async resolve => { + fetch('/instances/data.json').then(response => response.text()).then(async data => { + let dataJson = JSON.parse(data); + redirects.libremdb = dataJson.libremdb; + browser.storage.local.set({ + disableImdb: false, + imdbProtocol: "normal", + + imdbRedirects: redirects, + + libremdbNormalRedirectsChecks: [...redirects.libremdb.normal], + libremdbNormalCustomRedirects: [], + + libremdbTorRedirectsChecks: [...redirects.libremdb.tor], + libremdbTorCustomRedirects: [], + }, () => resolve()); + }); + }) +} + +export default { + setRedirects, + + redirect, + reverse, + switchInstance, + + initDefaults +}; diff --git a/src/assets/javascripts/utils.js b/src/assets/javascripts/utils.js index 53a5bb2e..0b8bfe41 100644 --- a/src/assets/javascripts/utils.js +++ b/src/assets/javascripts/utils.js @@ -12,6 +12,7 @@ import lbryHelper from "./lbry.js"; import sendTargetsHelper from "./sendTargets.js"; import tiktokHelper from "./tiktok.js"; import quoraHelper from "./quora.js" +import libremdbHelper from "./imdb.js"; import imgurHelper from "./imgur.js"; import localise from './localise.js' @@ -49,6 +50,7 @@ function updateInstances() { wikipediaHelper.setRedirects(instances.wikiless); mediumHelper.setRedirects(instances.scribe); quoraHelper.setRedirects(instances.query); + libremdbHelper.setRedirects(instances.libremdb); sendTargetsHelper.setRedirects(instances.send); tiktokHelper.setRedirects(instances.proxiTok); @@ -366,6 +368,7 @@ function copyRaw(test, copyRawElement) { if (!newUrl) newUrl = await instagramHelper.reverse(url); if (!newUrl) newUrl = await tiktokHelper.reverse(url); if (!newUrl) newUrl = await quoraHelper.reverse(url); + if (!newUrl) newUrl = await libremdbHelper.reverse(url); if (!newUrl) newUrl = await imgurHelper.reverse(url); if (newUrl) { @@ -439,6 +442,7 @@ function switchInstance(test) { if (!newUrl) newUrl = await translateHelper.switchInstance(url); if (!newUrl) newUrl = await mediumHelper.switchInstance(url); if (!newUrl) newUrl = await quoraHelper.switchInstance(url); + if (!newUrl) newUrl = await libremdbHelper.switchInstance(url); if (!newUrl) newUrl = await tiktokHelper.switchInstance(url); if (!newUrl) newUrl = await sendTargetsHelper.switchInstance(url); if (!newUrl) newUrl = await peertubeHelper.switchInstance(url); |