From 5ca505080e1f6674fe81785c68a25ec78ce0af50 Mon Sep 17 00:00:00 2001 From: ManeraKai Date: Sun, 17 Sep 2023 20:52:35 +0300 Subject: [PATCH] Added Custom service function for https://github.com/libredirect/browser_extension/issues/767 --- src/assets/javascripts/services.js | 31 ++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/assets/javascripts/services.js b/src/assets/javascripts/services.js index 3f75014..cdaf76f 100644 --- a/src/assets/javascripts/services.js +++ b/src/assets/javascripts/services.js @@ -565,28 +565,31 @@ function computeService(url, returnFrontend) { }) } -function switchInstance(url) { +function switchInstance(url, customService) { return new Promise(async resolve => { let options = await utils.getOptions() let config = await utils.getConfig() const protocolHost = utils.protocolHost(url) - for (const service in config.services) { - let frontend = options[service].frontend - let instancesList = options[frontend] - if (instancesList === undefined) continue - if (!instancesList.includes(protocolHost)) continue + if (customService) { + const instancesList = options[options[customService].frontend] + if (instancesList !== undefined) { + resolve(`${utils.getRandomInstance(instancesList)}${url.pathname}${url.search}`) + } + } else { + for (const service in config.services) { + let instancesList = options[options[service].frontend] + if (instancesList === undefined) continue + if (!instancesList.includes(protocolHost)) continue - instancesList.splice(instancesList.indexOf(protocolHost), 1) - if (instancesList.length === 0) { - resolve() + instancesList.splice(instancesList.indexOf(protocolHost), 1) + if (instancesList.length === 0) { + resolve() + return + } + resolve(`${utils.getRandomInstance(instancesList)}${url.pathname}${url.search}`) return } - - const randomInstance = utils.getRandomInstance(instancesList) - const newUrl = `${randomInstance}${url.pathname}${url.search}` - resolve(newUrl) - return } resolve() })