about summary refs log tree commit diff stats
path: root/src/assets/javascripts/utils.js
diff options
context:
space:
mode:
authorSoispha <soispha@vhack.eu>2023-12-09 12:55:10 +0100
committerSoispha <soispha@vhack.eu>2023-12-09 12:55:10 +0100
commit5f2088c0bb7ea5ee1f18bc5b3ce15d707e043751 (patch)
tree0aa576cf0559dbdca25fa69f5b3a664365e6a428 /src/assets/javascripts/utils.js
parentchore(manifest.json): Bump version number (diff)
parentAdded toggle for bookmarks menu in settings https://github.com/libredirect/br... (diff)
downloadlibredirect-5f2088c0bb7ea5ee1f18bc5b3ce15d707e043751.zip
chore(Merge): remote-tracking branch 'origin/master'
Diffstat (limited to 'src/assets/javascripts/utils.js')
-rw-r--r--src/assets/javascripts/utils.js65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/assets/javascripts/utils.js b/src/assets/javascripts/utils.js
index e85b1115..fe08e576 100644
--- a/src/assets/javascripts/utils.js
+++ b/src/assets/javascripts/utils.js
@@ -1,18 +1,68 @@
 window.browser = window.browser || window.chrome
 
+/**
+ * @param {Array.<T>} instances 
+ * @returns {T}
+ */
 function getRandomInstance(instances) {
 	return instances[~~(instances.length * Math.random())]
 }
 
+/**
+ * @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) {
 	return str.charAt(0).toUpperCase() + str.slice(1)
 }
 
+/**
+ * @param {URL} url
+ */
 function protocolHost(url) {
 	if (url.username && url.password) return `${url.protocol}//${url.username}:${url.password}@${url.host}`
+	if (url.pathname == "/TekstoLibre/" && url.host.endsWith("github.io")) // workaround
+		return `${url.protocol}//${url.host}${url.pathname.slice(0, -1)}`
 	return `${url.protocol}//${url.host}`
 }
 
+/**
+ * @typedef FrontendInfo
+ * @prop {boolean} instanceList
+ * @prop {string} name
+ * @prop {string} url
+ */
+
+/**
+ * @typedef {Object} Service
+ * @prop {Object.<string, FrontendInfo>} frontends
+ * @prop {Object} options
+ */
+
+/**
+ * @typedef {Object} Config
+ * @prop {Object.<string, Service>} services
+ */
+
+/**
+ * @returns {Promise<Config>}
+ */
 function getConfig() {
 	return new Promise(resolve => {
 		fetch("/config.json")
@@ -24,6 +74,14 @@ function getConfig() {
 	})
 }
 
+/**
+ * @typedef {Object} Option
+ * @prop {string} frontend
+ */
+
+/**
+ * @returns {Promise<Object.<string, Option | string[]>>}
+ */
 function getOptions() {
 	return new Promise(resolve =>
 		browser.storage.local.get("options", r => {
@@ -106,6 +164,9 @@ function getList(options) {
 	})
 }
 
+/**
+ * @param {string} href
+ */
 function pingOnce(href) {
 	return new Promise(async resolve => {
 		let started
@@ -130,6 +191,9 @@ function pingOnce(href) {
 	})
 }
 
+/**
+ * @param {string} href
+ */
 function ping(href) {
 	return new Promise(async resolve => {
 		let average = 0
@@ -150,6 +214,7 @@ function ping(href) {
 
 export default {
 	getRandomInstance,
+	getNextInstance,
 	protocolHost,
 	getList,
 	getBlacklist,