about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorManeraKai <manerakai@protonmail.com>2023-12-08 20:29:16 +0000
committerGitHub <noreply@github.com>2023-12-08 20:29:16 +0000
commit5a105da61ee38e5322cc8389e47138eaf1c351df (patch)
treed377eb7ef809b30d7eef20ff152fa562a07c255d
parentAdded superuser.com to AnonymousOverflow https://github.com/libredirect/brows... (diff)
parentswitchInstance returns next instance instead of random (diff)
downloadlibredirect-5a105da61ee38e5322cc8389e47138eaf1c351df.zip
Merge pull request #857 from orfins/feat/switch-to-next-instead-of-random
switchInstance returns next instance instead of random
-rw-r--r--src/assets/javascripts/services.js4
-rw-r--r--src/assets/javascripts/utils.js18
2 files changed, 20 insertions, 2 deletions
diff --git a/src/assets/javascripts/services.js b/src/assets/javascripts/services.js
index 13219abc..d431369d 100644
--- a/src/assets/javascripts/services.js
+++ b/src/assets/javascripts/services.js
@@ -618,7 +618,7 @@ function switchInstance(url, customService) {
 		if (customService) {
 			const instancesList = options[options[customService].frontend]
 			if (instancesList !== undefined) {
-				resolve(`${utils.getRandomInstance(instancesList)}${url.pathname}${url.search}`)
+				resolve(`${utils.getNextInstance(url.origin, instancesList)}${url.pathname}${url.search}`)
 			}
 		} else {
 			for (const service in config.services) {
@@ -631,7 +631,7 @@ function switchInstance(url, customService) {
 					resolve()
 					return
 				}
-				resolve(`${utils.getRandomInstance(instancesList)}${url.pathname}${url.search}`)
+				resolve(`${utils.getNextInstance(url.origin, instancesList)}${url.pathname}${url.search}`)
 				return
 			}
 		}
diff --git a/src/assets/javascripts/utils.js b/src/assets/javascripts/utils.js
index 4026e181..fe08e576 100644
--- a/src/assets/javascripts/utils.js
+++ b/src/assets/javascripts/utils.js
@@ -9,6 +9,23 @@ function getRandomInstance(instances) {
 }
 
 /**
+ * @param {string} currentInstanceUrl
+ * @param {Array.<T>} instances
+ * @returns {T}
+ */
+function getNextInstance(currentInstanceUrl, instances) {
+	const currentInstanceIndex = instances.indexOf(currentInstanceUrl);
+
+	if (currentInstanceIndex === -1){
+		return getRandomInstance(instances);
+	}
+
+	const nextInstanceIndex = (currentInstanceIndex + 1) % instances.length;
+
+	return instances[nextInstanceIndex];
+}
+
+/**
  * @param {string} str
  */
 function camelCase(str) {
@@ -197,6 +214,7 @@ function ping(href) {
 
 export default {
 	getRandomInstance,
+	getNextInstance,
 	protocolHost,
 	getList,
 	getBlacklist,